Home Laravel Laravel lifesaver Database Queries

Laravel lifesaver Database Queries

by therichpost
0 comments
Laravel 7.2 routing with route group auth guard check with prefix

Hi guys, welcome back on therichpost.com. In this post, I will share, Laravel lifesaver Database Queries.

Today I was working with laravel and I faced some problem in database queries but I found some lifesaver database queries which really helped me lot and save my time and I will share that queries today in this post.

I can also tell them smart database queries.

My main purpose is to share good and smart code which will help others during coding. 

Here are the Laravel lifesaver Database Queries:

 

1. Retrieving A Single Column From Table:

If you don’t even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:

$email = DB::table('users')->where('name', 'John')->value('email'); //Output will be email address

 

2. Retrieving A List Of Column Values From Table:

If you would like to retrieve a Collection containing the values of a single column, you may use the pluck method. In this example, we’ll retrieve a Collection of role titles:

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

foreach ($titles as $title) {
echo $title;
}

//Output will be all the titles

 

3. Determining If Records Exist in table:

Instead of using the count method to determine if any records exist that match your query’s constraints, you may use the exists and doesn’t Exist methods:

return DB::table('orders')->where('finalized', 1)->exists(); //Output will be finalized = 1
return DB::table('orders')->where('finalized;, 1)->doesntExist(); //Output will be finalized != 1

 

4. Raw Expressions In Database Query:

Sometimes you may need to use a raw expression in a query. To create a raw expression, you may use the DB::raw method:

$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>' , 1)
->groupBy('status')
->get();

Raw statements will be injected into the query as strings, so you should be extremely careful to not create SQL injection vulnerabilities.

 

5. latest / oldest Record in Table:

The latest and oldest methods allow you to easily order results by date. By default, result will be ordered by the created_at column. Or, you may pass the column name that you wish to sort by:

$user = DB::table('users')
->latest()
->first();

//Output will be latest added record

 

6. Skip / Take data from Table:

To limit the number of results returned from the query, or to skip a given number of results in the query, you may use the skip and take methods:

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

//I will Skip 1 to 10 number Records and Take 11 to 15 number Records

These are the lifesaver queries according to me. I also want to know your views on it.

Thank you,

Jassa Jatt,

TheRichPost

 

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.