lundi 14 décembre 2020

How to sort result while using relationship table in laravel? [duplicate]

This is my Task model.

public function sevenDaysTaskTiming()
    {
        return $this->hasMany(TaskTiming::class,'task_id')
            ->whereBetween('schedule_time',
                [Carbon::today()->toDateTimeString(), Carbon::today()->addDays(7)->toDateTimeString()]);
    }

This is the controller function

public function getNextSevenDaysTask(){
        $sevenDayTask = Task::with(['sevenDaysTaskTiming'=>function ($q){
            $q->whereBetween('schedule_time', [Carbon::today()->toDateTimeString(), Carbon::today()->addDays(7)->toDateTimeString()]);

        }])->whereHas('sevenDaysTaskTiming')
            ->where('user_id',Auth::user()->id)
            ->get();
        if ($sevenDayTask === NULL){
            throw new CustomValidationException('No Records Found');
        }
        return $sevenDayTask;
    }

This function is returning the result which is orderBy id. But I want the result to be ordered according to the schedule_time which is a column of relationship table. It would be great help for me If I could achieve this.

Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire