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.

Leave a Comment