【问题标题】:Fetch child and parent - Laravel Eloquent获取孩子和父母 - Laravel Eloquent
【发布时间】:2016-01-15 00:06:21
【问题描述】:

laravel我有以下2个模型; 这是parent

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class RestaurantClass extends Model
{
    /**
     * Get the restaurants for the restaurant class.
     */
    public function restaurants()
    {
        return $this->hasMany('App\Models\Restaurant');
    }
}

这是child

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Restaurant extends Model
{    
    /**
     * Get the restaurant class that owns the restaurant.
     */
    public function restaurantClass()
    {
        return $this->belongsTo('App\Models\RestaurantClass');
    }
}

如何检索孩子及其父母,反之亦然(检索父母及其所有孩子)

任何指导表示赞赏。

【问题讨论】:

    标签: laravel laravel-5 eloquent


    【解决方案1】:

    要在 Laravel 中查询关系,请使用 with($relation) 方法 在你想要的模型类上。

    将此应用于您的示例就像:

    检索父级及其子级:

    $restaurantClasses = RestaurantClass::with('restaurants')->get();
    
    // to access "children" 
    foreach($restaurantClasses as $restaurantClass) {
      $restaurantClass->restaurants;
    }
    

    用他们的父母检索孩子:

    $restaurants = Restaurant::with('restaurantClass')->get();
    
    // to access "parent" models
    foreach($restaurants as $restaurant) {
      $restaurant->restaurantClass;
    }
    

    【讨论】:

    • 当我使用all() 时,我得到了Call to undefined method Illuminate\Database\Query\Builder::all(),但是当我使用get() 时它可以工作
    • 那么我可以这样做吗? grandparent-parent-child-grandchild?
    • @moh.ABK 我想我不明白你想要达到什么目的,但是通过这种设置,你只能在一个级别访问实例,parent =&gt; childrenchild =&gt; parents跨度>
    • 好的。是的,我的问题是我也可以访问多个级别吗?使用不同的设置? parent =&gt; children =&gt; grandchildren?
    • 我认为 Eloquent 不可能,据我所知,唯一的解决方案是创建 3 个类 parentchildrengrandchildren 并定义所示的关系在我的回应中。类层次结构中缺乏表达是由于 Eloquent 实现的 Active Record 模式而不是 Data Mapper 模式(例如 Doctrine)
    【解决方案2】:

    假设您的 RestaurantClass 模型如下所示:

    class RestaurantClass extends Model
    {
        protected $fillable = [
            'ID',
            'NAME',    
        ];
    
        /**
         * Get the restaurants for the restaurant class.
         */
        public function restaurants()
        {
            return $this->hasMany('App\Models\Restaurant');
        }
    }
    

    您的 Restaurant 模型如下所示:

    class Restaurant extends Model
    {   
        protected $fillable = [
            'ID',
            'NAME',
            'RestaurantClassId',    
        ];
        /**
         * Get the restaurant class that owns the restaurant.
         */
        public function restaurantClass()
        {
            // Associating RestaurantClass and Restauran through their ids..
            return $this->belongsTo('App\Models\RestaurantClass', 'RestaurantClassId', 'ID');
        }
    }
    

    现在在您的控制器中,您可以通过多种方式使用这种关系,例如:

    RestaurantController::with('restaurantClass')->findOrFail($id); // This will bring up all the restaurant classes and you can later use it in your view like $data->restaurantClass->NAME ..
    

    或者您可以在控制器的创建/编辑函数中使用它,如下例所示,然后在您的视图中进一步使用它:

    $dataSet = array(
                'restaurantClass' => RestaurantClass::lists('NAME', 'ID')
                );
    

    我希望这个解决方案对您有所帮助,并为您提供一些关于关系如何工作的见解,但是您可以将关系反过来,使其以相反的方向工作,例如,RestaurantClass 将具有此 belongsTo 方法,您将能够检索控制器内的所有餐馆。干杯,,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-30
      • 2018-01-29
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2019-01-14
      • 2013-06-04
      • 1970-01-01
      相关资源
      最近更新 更多