Ilham Guezzoula
Ilham Guezzoula

Reputation: 145

How to add if condition inside eloquent laravel 5.4

I'm using Laravel 5.4 and this code work properly without if conditions, when i try to add it i got errors and this message appear :

syntax error, unexpected 'if' (T_IF) OR syntax error, unexpected '->' (T_OBJECT_OPERATOR)

any help please?

   $moviesearch = DB::table('movies')
     -> join('qualities', 'movies.mov_ID', '=', 'qualities.mov_ID')
     -> join('genres', 'movies.mov_ID', '=', 'genres.mov_ID')
         if (isset($age)) {
             ->where('movies.name', '=', '1')
         }
         if (isset($jkk)) {
             ->where('qualitie.link', '=', '233')
         }
     ->get();

 echo json_encode($moviesearch);

Upvotes: 1

Views: 1587

Answers (1)

AgeValed
AgeValed

Reputation: 453

You can do the following

$query = DB::table('movies')
     ->join('qualities', 'movies.mov_ID', '=', 'qualities.mov_ID')
     ->join('genres', 'movies.mov_ID', '=', 'genres.mov_ID');
if (isset($age)) {
    $query->where('movies.name', '=', '1');
}
if (isset($jkk)) {
    $query->where('qualitie.link', '=', '233');
}
$moviesearch= $query->get();

echo json_encode($moviesearch);

I hope this works for you

Upvotes: 4

Related Questions