Hello Friends 👋,

Welcome To Infinitbility! ❤️

This article helps you write code of query in laravel, here you get examples of Insert, select, update, delete, and join query examples to use in your laravel project.

Before the start article, I have shared some articles’ link may you help check out below links

Okey, Let’s start today topic Laravel query examples

Table of content

  1. Insert query
  2. Update query
  3. Select or retrieve query
  4. Delete query
  5. join query

Insert query

Laravel provide insert() method to add record on your Table, you have to pass array with key-value pair.

    array('email' => '[email protected]', 'votes' => 0)

Laravel provide insertGetId() method also to insert details but deffrance is insertGetId() method provide inserted record id.

$id = DB::table('users')->insertGetId(
    array('email' => '[email protected]', 'votes' => 0)

well if you want to insert multiple row then you have to pass multi demansional array with key-value pair.

    array('email' => '[email protected]', 'votes' => 0),
    array('email' => '[email protected]', 'votes' => 0),

Update query

Laravel provide update() method to update record on your table, you have to pass array with key-value pair.

DB::table('users')->where('id', 1)->update(array('votes' => 1));

Select or retrieve query

Laravel provide get() method to retrieve or select record from table.

  • select all records from table
$users = DB::table('users')->get();
  • select only first row
$user = DB::table('users')->where('name', 'John')->first();
  • select with condition
$users = DB::table('users')->where('votes', '>', 100)->get();

Delete query

Laravel provide delete() method to delete record from table, let’s understand with example.

  • delete all records
  • delete with condition
DB::table('users')->where('votes', '<', 100)->delete();

join query

join query have five types and laravel provide diffrent diffrent method to joins query let’s start with inner join query.

join query types

  1. Inner join
  2. Left join
  3. Right join
  4. Outer join
  5. Cross join

Inner join

Laravel provide join() method to do inner join, here have example.

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')

Left join

Laravel provide leftJoin() method to do left join betwee tables.

$users = DB::table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')

Right join

Laravel provide rightJoin() method to do left join betwee tables.

$users = DB::table('users')
            ->rightJoin('posts', 'users.id', '=', 'posts.user_id')

Outer join

Laravel provide fourth parameter of the join method is $type, which when not specified, defaults to the value inner.

For inner or outer you can use join() method.

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id', 'left outer')
            ->join('orders', 'users.id', '=', 'orders.user_id', 'left outer')
            ->select('users.*', 'contacts.phone', 'orders.price')

Cross join

Laravel provide crossJoin () method to do cross join betwee tables, Cross joins generate a cartesian product between the first table and the joined table.

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

Thanks for reading…