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.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3a7Tp26
via IFTTT

Aucun commentaire:

Enregistrer un commentaire