L
L
Li2016-06-03 21:29:09
Laravel
Li, 2016-06-03 21:29:09

How to correctly formulate the sampling conditions for the linking model?

Good evening everyone. I am using Laravel 5.2. It is necessary to display events from a specific city.
There are 3 models: Event, Place, City
with the following relationships:
b9fb7ceefb2c44d8be20a3bbc5f8ad6e.png
Relationships in models:
Event

public function place()
{
    return $this->belongsTo('App\Models\Place');
}

place
public function city()
{
    return $this->belongsTo('App\Models\City');
}

The Event model has a method in which I am trying to display the Events from the specified city.
public function getExpectedEvents()
{
$events = Event::expected()->with([
        'cover' => function ($query) {
            $query->where('is_cover', 1);
        },
        'place.city' => function ($query) {
            // например так
            $query->where('slug', 'название города');
        }
    ])->get();
}

Is it possible to implement this without using the Query Builder and is it possible to use query blanks ( eg scopeCover ) in the closure function
so as not to repeat
->with(['cover' => function ($query) {
        $query->where('is_cover', 1);
    },

Thank you for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Plisko, 2016-06-04
@cubaPro

You have some kind of iodine-style. There is a city slug, so we dance from this side
City::where('slug', 'city name')->with('place.events')->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question