I am trying to convert the a MySQL query into a Laravel Eloquent query.
The following code will select a row of result where then it return the value of the weekNum column
function currentWeek() {
$sql = "SELECT DISTINCT weekNum FROM schedule WHERE DATE_ADD(NOW(), INTERVAL -50 HOUR) < gameTimeEastern ORDER BY weekNum LIMIT 1 ";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$row = $query->fetch_assoc();
return $row['weekNum'];
}
}
In Laravel in my SchedulesController this is what I am trying to achieve.
public function index()
{
$schedules = Schedule::where('week', '=', 4)->get();
return view('schedules.index', compact('schedules'));
}
Obviously I don't want to hard code the week in. This is my attempt.
public function index()
{
$currentWeek = Schedule::distinct()
->where('gameTime', '<', Carbon::now()->addHours(100))
->orderBy('gameTime', 'desc')
->take(1)
->get();
$schedules = Schedule::where('week', '=', $currentWeek->week)->get();
return view('schedules.index', compact('schedules'));
}
But I get this error
ErrorException in SchedulesController.php line 25:
Undefined property: Illuminate\Database\Eloquent\Collection::$week