Laravel 6 Set Extra where condition Examples

Today, We want to share with you Laravel 6 Set Extra where condition.In this post we will show you Adding conditions to relationships in Laravel 6, hear for How to Create Multiple Where Clause Query Using Laravel 6 Eloquent? we will give you demo and example for implement.In this post, we will learn about Reusing query with extra where condition with an example.

Laravel 6 Set Extra where condition

There are the Following The simple About laravel search query with multiple conditions Full Information With Example and source code.

As I will cover this Post with live Working example to develop Adding extra conditions in default Laravel 6 Authentication, so the how to use if condition in laravel query is used for this example is following below.

Advanced Where and When Eloquent using Laravel 6

Conditionally adding instructions to laravel’s query builder

$movies = DB::table('movies')
    ->where('song_id', Auth::user()->song_id)

laravel’s query builder to get all movies that belong to a specific songs

$movies = DB::table('movies')
    ->where('song_id', Auth::user()->song_id);

    $movies->where('movie_id', $request->movie_id);

$movies = $movies->get();

$movies = DB::table('movies')
    ->where('song_id', Auth::user()->song_id)
    ->when($request->movie_id, function($query) use ($request){
        return $query->where('movie_id', $request->movie_id);

more readable in Laravel query

$movies = DB::table('movies')
    ->where('song_id', Auth::user()->song_id)
    ->if($request->movie_id, 'movie_id', '=', $request->movie_id)

The if() method is not part of the core in macro

use Illuminate\Database\Query\Builder;

Builder::macro('if', function ($condition, $column, $operator, $value) {
    if ($condition) {
        return $this->where($column, $operator, $value);

    return $this;

Web Programming Tutorials Example with Demo

Read :


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

I hope you get an idea about conditional where laravel eloquent.
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. Return success message to laravel view