This post is focused on the laravel group by with order by desc. if you want to see an example of a laravel eloquent group by order by desc then you are in the right place. you can understand the concept of the laravel group by orderby. I would like to show you the laravel eloquent group by and order by. it will also be usable with the laravel app.

In this post, I will give you some solutions for how to get records with an order by desc with the group by in laravel application. when you use laravel…


We may sometimes require to use a select query with where in or where not in the clause in laravel. we can simply make a query using MySQL. But we don’t know how to use it on laravel query builder. However, In this example, we will learn how to convert MySQL query into a laravel query builder of wherein with a subquery. This example is from scratch, so I will give you the first MySQL query, and then we will convert it into a laravel query builder.

In this example I have to tables as listed below

1) users

2)…


In this post, we are going to see whereKey method in Laravel Eloquent. whereKey is a very simple way to use and a pretty interesting feature. laravel provides us where condition with add key(column) name and then value, But in this method, you have to write Key = name of the column.

So I am going to give one example for whereKey() that way you can understand how it is working. So first we have on “posts” table with some dummy data like as below screenshot.

posts table Laravel Eloquent Query | Laravel whereKey method

Ok, now we require to get the only status = ‘PUBLISHED’ records. …


In this example, I will show you how to use whereHas and orWhereHas in laravel 6, laravel 7, and laravel 8 applications.

Many times we add more than two relations with the database model and you have to add where condition with both of that relation model than how you will add where condition with both models.

If you don’t understand clearly then I will explain to you by example. I have a User Model with some records. When I create a new user at that time I have two more fields for city and state. I basically have City…


If you need to use SQL wherenotin query in laravel then you can use it with the array. Laravel provides wherenotin() to use SQL wherenotin query. in wherenotin() we just need to pass two arguments one is the column name and another is an array of ids or anything that you want.

You can see below syntax on wherenotin query in laravel:

whereNotIn(Coulumn_name, Array);

Now I will give you three examples of how to use wherenotin query in the laravel application. So let’s see those examples of how it works.

SQL Query

SELECT *
FROM users
WHERE id NOT IN (4, 5…

If you need to use SQL wherein query in laravel then you can use with the array. Laravel provides wherein() to use SQL wherein query. in wherein() we just need to pass two arguments one is the column name and another is an array of ids or anything that you want.

You can see below syntax on wherein query in laravel:

whereIn(Coulumn_name, Array);

Now I will give you three examples of how to use wherein query in laravel application. So let’s see those examples how works.

Example 1

SQL Query

SELECT *
FROM users
WHERE id IN (4, 5, 6)

Laravel Query


If you need to see the example of the laravel collection first example. let’s discuss the laravel collection first where example. you will learn how to get the first element from the collection in laravel. I would like to share with you the laravel collection first element.

I will give you some examples of how to get the first element from the collection in laravel. you can easily add an array in laravel 5, laravel 6, laravel 7, and laravel 8.

Let’s see an example:

public function index(){$collection = collect([['id'=>1, 'name'=>'Hardik'],['id'=>2, 'name'=>'Vimal'],['id'=>3, 'name'=>'Harshad'],['id'=>4, 'name'=>'Harsukh'],

Laravel is a very popular PHP framework. Laravel was released a few days ago and there are several new features added by Laravel. you can also use laravel 6, laravel 7, and laravel 8 versions.

Laravel introduces several new “where” conditions like whereDate(), whereMonth(), etc in Query Builder. Today, I am going to show you how to use it in your Laravel Application.

I give you one example table and understand how works these four query builder functions one by one. So, first, you can see bellow table with some records.

products table

Laravel — whereDate(), whereMonth(), whereDay() and whereYear() examples products table

whereDate()

whereDate() through we can make condition…


Laravel 5 introduces “whereColumn()” in Query Builder, that way we can compare two column-like simples where condition. We sometimes require to check this type of condition.

you can use the “wherecolumn()” eloquent function in laravel 6, laravel 7, and laravel 8 applications.

In this example code, I have simple “items” table and I want to get the only created_at and updated_at column should equal. So you can also check bellow laravel eloquent as bellow:

Example:

$data = DB::table("items")
->whereColumn('created_at','updated_at')
->get();
print_r($data);

Example 2:

$data = DB::table("items")
->whereColumn('created_at','>','updated_at')
->get();
print_r($data);

In this post, I share with you that how to use date_format() with where the condition of the Laravel query builder. When I was working on my project, I require to search with getting only selected month and year records from the created_at(timestamp) column. I was thinking about how can I but I know the date_format() of MySQL. but don’t know how to use in laravel where clause. At last, I use DB::raw() of Laravel and its work.

If you have also need to search this way then you can do it this way:

Example:

$data = DB::table("items")
->select("id","title","created_at")
->where(DB::raw("(DATE_FORMAT(created_at,'%Y-%m'))"),"2016-07")
->get();
print_r($data);

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store