Tagged: Debugging Queries in Laravel

Print Debugging Queries in Laravel Example 0

Print Debugging Queries in Laravel Example

Today, We want to share with you Print Debugging Queries in Laravel Example.In this post we will show you Display all SQL executed in Eloquent, hear for How to get last executed query in Larave 5? we will give you demo and example for implement.In this post, we will learn about How to print query or debug queries in Laravel with an example.

Print Debugging Queries in Laravel Example

There are the Following The simple About Print Debugging Queries in Laravel Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Debugging Queries in Laravel, so the Using toSql Function to Print SQL Query in Laravel for this example is following below.

Laravel Debugging Queries with Example

How to print query or debug queries in Laravel

$results = Member::where(function($q) use ($request) {
    $q->orWhere('email', 'like', '%[email protected]%');
    $q->orWhere('member_fname', 'like', '%jaydeep%');
    $q->orWhere('member_lname', 'like', '%Gondaliya%');
})->toSql();
dd($results);

How to get last executed query in Larave 5?

\DB::listen(function($sql, $bindings, $time) {
	//SQL query
    var_dump($sql);
    var_dump($bindings);
    var_dump($time);
});

Debugging Queries in Laravel

$results = Member::where(function($q) use ($request) {
    $q->where('email', 'like', '%[email protected]%');
    $q->where('member_fname', 'like', '%jaydeep%');
    $q->where('member_lname', 'like', '%Gondaliya%');
})->get();
dd($results);

How to get last executed query in Larave 5?

Example 1: laravel tosql with parameters

DB::enableQueryLog();
$member = Member::get();
$sql_qry = DB::getQueryLog();
$sql_qry = end($sql_qry);
print_r($sql_qry);

Example 2: laravel tosql with parameters

DB::enableQueryLog();
$member = Member::get();
$sql_qry = DB::getQueryLog();
print_r($sql_qry);

Example 3: laravel 5.4 get last query

$member = Member::where('id',1)->toSql();
print_r($member);

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Print Debugging Queries in Laravel Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Laravel MySQL Advanced Query Techniques with Example 0

Laravel MySQL Advanced Query Techniques with Example

Today, We want to share with you Laravel MySQL Advanced Query Techniques with Example.In this post we will show you Laravel Advance Database Query Builder, hear for Laravel advanced Eloquent search query filters we will give you demo and example for implement.In this post, we will learn about mysql – How to run complex sql query in Larave with an example.

Laravel MySQL Advanced Query Techniques with Example

There are the Following The simple About Laravel MySQL Advanced Query Techniques with Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop MySQL functions in insert query using Laravel Query Builder, so the some major files and Directory structures for this example is following below.

Laravel 5.6 Advance More Where Clauses

$employees = DB::table('employees')
->whereBetween('total_likes', [1, 100])->get();

$employees = DB::table('employees')
->whereNotBetween('total_likes', [1, 100])
->get();

$employees = DB::table('employees')
->whereIn('id', [1, 2, 3])
->get();

$employees = DB::table('employees')
->whereNotIn('id', [1, 2, 3])
->get();

$employees = DB::table('employees')
->whereNull('updated_at')
->get();

$employees = DB::table('employees')
->whereNotNull('updated_at')
->get();

$employees = DB::table('employees')
->whereDate('created_at', '2016-12-31')
->get();

$employees = DB::table('employees')
->whereMonth('created_at', '12')
->get();

$employees = DB::table('employees')
->whereDay('created_at', '31')
->get();

$employees = DB::table('employees')
->whereYear('created_at', '2016')
->get();

$employees = DB::table('employees')
->whereTime('created_at', '=', '11:20:45')
->get();

$employees = DB::table('employees')
->whereColumn('first_name', 'last_name')
->get();

$employees = DB::table('employees')
->whereColumn('updated_at', '>', 'created_at')
->get();

$employees = DB::table('employees')
->whereColumn([
['first_name', '=', 'last_name'],
['updated_at', '>', 'created_at']
])->get();

Laravel 5.6 Parameter Grouping

DB::table('employees')
->where('name', '=', 'Jaydeep')
->where(function ($query) {
$query->where('total_likes', '>', 100)
	->orWhere('title', '=', 'Admin');
})
->get();

select * from employees where name = 'Jaydeep' and (total_likes > 100 or title = 'Admin')

Laravel 5.6 Where Exists Clauses

DB::table('employees')
->whereExists(function ($query) {
    $query->select(DB::raw(1))
          ->from('memberships')
          ->whereRaw('memberships.employee_id = employees.id');
})
->get();

select * from employees
where exists (
select 1 from memberships where memberships.employee_id = employees.id
)

Laravel 5.6 JSON Where Clauses

$employees = DB::table('employees')
                ->where('options->language', 'en')
                ->get();

$employees = DB::table('employees')
                ->where('preferences->dining->meal', 'salad')
                ->get();

                $employees = DB::table('employees')
                ->whereJsonContains('options->languages', 'en')
                ->get();

$employees = DB::table('employees')
                ->whereJsonContains('options->languages', ['en', 'de'])
                ->get();  

Laravel 5.6 Ordering, Grouping, Limit, & Offset Query

$employees = DB::table('employees')
->orderBy('name', 'desc')
->get();

$employee = DB::table('employees')
->latest()
->first();

$randomUser = DB::table('employees')
->inRandomOrder()
->first();

$employees = DB::table('employees')
->groupBy('account_id')
->having('account_id', '>', 100)
->get();

$employees = DB::table('employees')
->groupBy('first_name', 'type_of_status')
->having('account_id', '>', 100)
->get();

$employees = DB::table('employees')->skip(10)->take(5)->get();

$employees = DB::table('employees')
->offset(10)
->limit(5)
->get();

Laravel 5.6 for Conditional Clauses

$role = $request->input('role');

$employees = DB::table('employees')
->when($role, function ($query, $role) {
return $query->where('role_id', $role);
})
->get();

or

$sortBy = null;

$employees = DB::table('employees')
->when($sortBy, function ($query, $sortBy) {
    return $query->orderBy($sortBy);
}, function ($query) {
    return $query->orderBy('name');
})
->get();

Inserts Data using Laravel 5.6

DB::table('employees')->insert(
    ['email' => '[email protected]', 'total_likes' => 0]
);

DB::table('employees')->insert([
    ['email' => '[email protected]', 'total_likes' => 0],
    ['email' => '[email protected]', 'total_likes' => 0]
]);

Auto-Incrementing IDs in Laravel 5.6

$id = DB::table('employees')->insertGetId(
    ['email' => '[email protected]', 'total_likes' => 0]
);

Laravel 5.6 Updates Data

DB::table('employees')
->where('id', 1)
->update(['total_likes' => 1]);

Laravel 5.6 Updating JSON Columns

DB::table('employees')
->where('id', 1)
->update(['options->enabled' => true]);

Laravel 5.6 Increment & Decrement

DB::table('employees')->increment('total_likes');
DB::table('employees')->increment('total_likes', 5);
DB::table('employees')->decrement('total_likes');
DB::table('employees')->decrement('total_likes', 5);

DB::table('employees')->increment('total_likes', 1, ['name' => 'Jaydeep']);

Laravel 5.6 Deletes Query

DB::table('employees')->delete();
DB::table('employees')->where('total_likes', '>', 100)->delete();

DB::table('employees')->truncate();

Laravel 5.6 Pessimistic Locking

DB::table('employees')->where('total_likes', '>', 100)->sharedLock()->get();
DB::table('employees')->where('total_likes', '>', 100)->lockForUpdate()->get();

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel MySQL Advanced Query Techniques with Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.