Skip to main content

Laravel Advance Database Query Builder

Today, We want to share with you Laravel Advance Database Query Builder.In this post we will show you Database: Query Builder – Laravel, hear for Advanced query using Eloquent we will give you demo and example for implement.In this post, we will learn about Laravel 5.6 Database: Query Builder with an example.

Laravel Advance Database Query Builder

There are the Following The simple About Laravel Advance Database Query Builder Full Information With Example and source code.

As I will cover this Post with live Working example to develop php – Laravel Delete Query Builder, so the laravel query builder for this example is following below.

Also Read :  Laravel 5.4 Paypal integration - Paypal Payment Gateway

Getting All Rows From A Table using Laravel 5.6

public function index()
    $employees = DB::table('employees')->get();
    return view('employee.index', ['employees' => $employees]);

Getting A Single Row or Column From A Table

$employee = DB::table('employees')->where('name', 'Jaydeep')->first();
echo $employee->name;


$email = DB::table('employees')->where('name', 'Jaydeep')->value('email');

Getting A List Of Column Values using Laravel 5.6 pluck

$members = DB::table('roles')->pluck('title');

foreach ($members as $title) {
    echo $title;

$roles = DB::table('roles')->pluck('title', 'name');

foreach ($roles as $name => $title) {
    echo $title;

Laravel 5.6 Aggregates

$employees = DB::table('employees')->count();
$rate = DB::table('memberships')->max('rate');

$rate = DB::table('memberships')
->where('finalized', 1)

Laravel 5.6 Determining If Records Exist

return DB::table('memberships')->where('finalized', 1)->exists();
return DB::table('memberships')->where('finalized', 1)->doesntExist();

Laravel 5.6 Specifying A Select Clause

$employees = DB::table('employees')->select('name', 'email as employee_email')->get();
$employees = DB::table('employees')->distinct()->get();

$query = DB::table('employees')->select('name');
$employees = $query->addSelect('age')->get();

Laravel 5.6 Raw Expressions

$employees = DB::table('employees')
->select(DB::raw('count(*) as employee_count, type_of_status'))
->where('type_of_status', '<>', 1)

Laravel 5.6 selectRaw

$memberships = DB::table('memberships')
->selectRaw('rate * ? as rate_with_tax', [1.0825])

Laravel 5.6 whereRaw / orWhereRaw

$memberships = DB::table('memberships')
->whereRaw('rate > IF(state = "TX", ?, 100)', [200])

Laravel 5.6 havingRaw / orHavingRaw

$memberships = DB::table('memberships')
->select('department', DB::raw('SUM(rate) as total_sales'))
->havingRaw('SUM(rate) > ?', [2500])

Laravel 5.6 orderByRaw

$memberships = DB::table('memberships')
->orderByRaw('updated_at - created_at DESC')

Laravel 5.6 Inner Join Clause

$employees = DB::table('employees')
->join('contacts', '', '=', 'contacts.employee_id')
->join('memberships', '', '=', 'memberships.employee_id')
->select('employees.*', '', 'memberships.rate')

Left Join Clause using Laravel 5.6

$employees = DB::table('employees')
->leftJoin('posts', '', '=', 'posts.employee_id')

<h3>Cross Join Clause</h3>

$employees = DB::table('sizes')

Laravel 5.6 Advanced Join Clauses

->join('contacts', function ($join) {
	$join->on('', '=', 'contacts.employee_id')->orOn(...);

->join('contacts', function ($join) {
	$join->on('', '=', 'contacts.employee_id')
     ->where('contacts.employee_id', '>', 5);

Laravel 5.6 Sub-Query Joins

$latestPosts = DB::table('posts')
->select('employee_id', DB::raw('MAX(created_at) as last_post_created_at'))
->where('is_published', true)

$employees = DB::table('employees')
->joinSub($latestPosts, 'latest_posts', function($join) {
	$join->on('', '=', 'latest_posts.employee_id');

Laravel 5.6 Unions

$first = DB::table('employees')

$employees = DB::table('employees')

Laravel 5.6 Where Clauses

$employees = DB::table('employees')->where('total_likes', '=', 100)->get();
$employees = DB::table('employees')->where('total_likes', 100)->get();

$employees = DB::table('employees')
->where('total_likes', '>=', 100)

$employees = DB::table('employees')
->where('total_likes', '<>', 100)

$employees = DB::table('employees')
->where('name', 'like', 'T%')

$employees = DB::table('employees')->where([
    ['type_of_status', '=', '1'],
    ['subscribed', '<>', '1'],

Laravel 5.6 Or Statements

$employees = DB::table('employees')
->where('total_likes', '>', 100)
->orWhere('name', 'Jaydeep')

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  jQuery Dynamically addClass and removeClass


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

I hope you get an idea about Laravel Advance Database Query Builder.
I would like to have feedback on my 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.

Gondaliya Jaydeep

Hi, I’m Jaydeep Gondaliya, PHP is the most popular Programming & Web Development blog. Our mission is to provide the best online resources on programming and web development.Any visitors of this site are free to browse our tutorials, live demos, Examples and download scripts.If you have a project that you want to get started, launch quickly, contact me. Email : [email protected]