V
V
Vitaly2018-06-05 10:42:49
Laravel
Vitaly, 2018-06-05 10:42:49

Laravel. How to properly chain condition chain in ORM?

there are several conditions like

->where('status',1)
 ->whereDate('start_time', "<=", Carbon::now()->addWeeks($week)->format('Y-m-d'))

Now there is enough knowledge
Program::orderBy('start_time', 'ASC')
            ->where('status',1)
            ->whereDate('start_time', "<=", Carbon::now()->addWeeks($week)->format('Y-m-d'))
            ->with('league')
            ->get();

How to put conditions into separate methods?
To get something like this:
publick function activ(){
    $this->where('status',1)
}
publick function addWeeks($week = 1){
    $this->whereDate('start_time', "<=", Carbon::now()->addWeeks($week)->format('Y-m-d'))
}
Program::orderBy('start_time', 'ASC')
            ->activ()
            ->addWeeks()
            ->with('league')
            ->get();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hakkol, 2018-06-05
@kiukishenkaec

https://laravel.com/docs/5.6/eloquent#local-scopes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question