Navigate back to the homepage

Laravel query examples

Infinitbility
Laravel
May 3rd, 2021 · 1 min read
Laravel query examples

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.

1DB::table('users')->insert(
2 array('email' => '[email protected]', 'votes' => 0)
3);

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

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

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

1DB::table('users')->insert(array(
2 array('email' => '[email protected]', 'votes' => 0),
3 array('email' => '[email protected]', 'votes' => 0),
4));

Update query

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

1DB::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
1$users = DB::table('users')->get();
  • select only first row
1$user = DB::table('users')->where('name', 'John')->first();
  • select with condition
1$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
1DB::table('users')->delete();
  • delete with condition
1DB::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.

1$users = DB::table('users')
2 ->join('contacts', 'users.id', '=', 'contacts.user_id')
3 ->join('orders', 'users.id', '=', 'orders.user_id')
4 ->select('users.*', 'contacts.phone', 'orders.price')
5 ->get();

Left join

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

1$users = DB::table('users')
2 ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
3 ->get();

Right join

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

1$users = DB::table('users')
2 ->rightJoin('posts', 'users.id', '=', 'posts.user_id')
3 ->get();

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.

1$users = DB::table('users')
2 ->join('contacts', 'users.id', '=', 'contacts.user_id', 'left outer')
3 ->join('orders', 'users.id', '=', 'orders.user_id', 'left outer')
4 ->select('users.*', 'contacts.phone', 'orders.price')
5 ->get();

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.

1$sizes = DB::table('sizes')
2 ->crossJoin('colors')
3 ->get();

Thanks for reading…

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Tutorials

React Native

Categories

More articles from Infinitbility

React native net info example

React native net info example

how to handle No internet connection in React Native

May 2nd, 2021 · 1 min read
integrate Mustache nodejs example

integrate Mustache nodejs example

Mustache html template use for email content

May 1st, 2021 · 1 min read
© 2020–2021 Infinitbility
Disclaimer
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]