Home > Net >  Laravel Get Data On Conditions
Laravel Get Data On Conditions

Time:09-16

I have 2 tables called movies and movie_dates.

In movie_dates table these are the fields,

id | movie_id | year | month

My Question: I'm trying to get data from the movies table where year is greater than or equal to 1 and month is greater than or equal to 2 in the movie_dates table using movie_id column.

What I tried so far,

$months = 2;
$years = 1;

return DB::table('movie_dates')
  ->join('movies', 'movie_dates.movie_id', '=', 'movies.id')
  ->where(['movie_dates.year', '>=', $years], ['movie_dates.month', '>=', $months])
  ->get();

Error I'm getting : SQLSTATE[42S22]: Column not found: 1054 Unknown column

Really appreciate it if somebody could help me. Thanks have a nice day.

CodePudding user response:

Use array on where

$months = 2;
$years = 1;

return DB::table('movie_dates')
  ->join('movies', 'movie_dates.movie_id', '=', 'movies.id')
  ->where([['movie_dates.year', '>=', $years], ['movie_dates.month', '>=', $months]])
  ->get();

CodePudding user response:

use multi where condition or use [] array for it

$months = 2;
$years = 1;

return DB::table('movie_dates')
  ->join('movies', 'movie_dates.movie_id', '=', 'movies.id')
  ->where([['movie_dates.year', '>=', $years], ['movie_dates.month', '>=', $months]])
  ->get();

OR

$months = 2;
$years = 1;

return DB::table('movie_dates')
  ->join('movies', 'movie_dates.movie_id', '=', 'movies.id')
  ->where('movie_dates.year', '>=', $years)
  ->where('movie_dates.month', '>=', $months)
  ->get();
  • Related