【问题标题】:Laravel Eloquent group by with pivot table & relationsLaravel Eloquent group by 与数据透视表和关系
【发布时间】:2016-04-18 08:29:58
【问题描述】:

我正在为食品卡车活动创建这个平台。有许多参展商参加了几个活动。每辆餐车都有自己的菜品菜单,分几类。

问题/我想要实现的目标

我想为每个事件制作一个菜单,循环遍历所有事件 参展商(参加者),然后按类别展示菜肴。

类似的东西;

/menu/{eventid}

菜品类别 1

  • 参展商A的菜
  • 参展商 B 的菜

菜肴类别 2

  • exh A 中的菜
  • C 出品的菜
  • exh D 中的菜

...

模型

事件模型

class Event extends Model
{
    protected $table = "events";

    public function exhibitors()
    {
        return $this->belongsToMany('App\Exhibitor', 'events_exhibitors');
    }

碟型

class Dish extends Model
{
    //

    protected $table = "dishes";    


    public function category()
    {
        return $this->hasOne('App\Category', 'id', 'category_id');
    }

    public function exhibitor()
    {
        return $this->belongsTo('App\Exhibitor');
    }
}

参展型号

class Exhibitor extends Model
{
    protected $table = "exhibitors";

    public function events()
    {
        return $this->belongsToMany('App\Event', 'events_exhibitors');
    }

    public function dishes()
    {
        return $this->hasMany('App\Dish');
    }
}

数据库结构

有一个数据透视表来记录哪些食品卡车去哪些活动。到目前为止,我认为(希望)我的关系正常。我希望这张图片足够显示;

我的尝试

我已经尝试了几件事,但我认为我对 Laravel eloquent 的洞察力缺乏理解这个问题背后的逻辑。

$dishes = Event::where('id', $id)
            ->with(['exhibitors.dishes' => function($q) {
            $q->select('dishes.dish_data');
        }])->get();

或者

$dishes = Event::with(array('exhibitor.dish') => function($query) use ($sub){
            $query->where('name',$sub);}))->get();

我完全不知道如何通过 Eloquent 完成此操作,也不知道这将如何在视图中工作。

【问题讨论】:

    标签: laravel eloquent pivot-table


    【解决方案1】:

    试试这样的:

    它将获取与该活动相关的所有参展商的ID。 然后它将获取所有类别,其中连接的菜肴具有在参展商 ID 数组中的 existor_id。

    类别模型

    class Category extends Model
    {
        protected $table = "categories";    
    
    
        public function dishes()
        {
            return $this->hasMany('App\Dish', 'category_id');
        }
    }
    

    控制器动作

    $event = Event::findOrFail($id);
    
    $exhibitorIds = $event->exhibitors()->select('exhibitors.id')->get()->pluck('id')->all();
    
    $categories = Category::whereHas('dishes', function($query) use ($exhibitorIds) {
        $query->whereIn('exhibitor_id', $exhibitorIds);
    })->with(['dishes' => function($query) use ($exhibitorIds) {
        $query->whereIn('exhibitor_id', $exhibitorIds);
    }])->get();
    
    return view('events.menu', compact('event', 'categories') );
    

    查看

    @foreach($categories as $category)
    <h2>{{ $category->name }}</h2>
    <ul>
       @foreach($category->dishes as $dish)
           <li>{{ $dish->dish_data }}</li>
       @endforeach
    </ul>
    @endforeach
    

    【讨论】:

    • 感谢您的帮助。这会在“$categories...”处引发构建器错误“explode() 期望参数 2 是字符串,对象给定”
    • 好的,Laravel 集合所在的参展商,现在使用 ->all() 它返回一个数组。并且秒查询没有使用exportorids
    • 仍然抛出相同的错误...“explode() 期望参数 2 是字符串,给定对象”。当我 dd($exhibitorIds) 它确实是一个包含 id 的数组。
    • 试试不带 ->with(...)
    • 现在再试一次,在with语句中,函数应该是value,关系字符串是key。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2023-02-05
    • 2019-01-25
    • 1970-01-01
    • 2017-07-22
    • 2022-01-23
    相关资源
    最近更新 更多