laravel雄辩的关系不检索结果

vmdwslir  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(397)

我正在努力使表关系与拉威尔(5.6)雄辩。我无法从模型中检索关系表结果。下面我提到了这种关系。
关系:项有一个类别
这是我的商品型号

class InventoryItems extends Model
{
    /**
     * table row delete
     */
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'inventory_items';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    protected $fillable = ['name','type_id','category_id','sub_category_id','description','cost_price','sale_price','image_path','image','store_id','status'];

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

这是我的分类模型

class ItemCategories extends Model
{
    /**
     * table row delete
     */
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'item_categories';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    protected $fillable = ['name'];
}

这是我的控制器

public function index(Request $request)
    {
        $items = InventoryItems::all();

        dd($items);

    }

这就是结果

Collection {#722 ▼
  #items: array:2 [▼
    0 => InventoryItems {#719 ▼
      #table: "inventory_items"
      #guarded: array:1 [▶]
      #fillable: array:11 [▼
        0 => "name"
        1 => "type_id"
        2 => "category_id"
        3 => "sub_category_id"
        4 => "description"
        5 => "cost_price"
        6 => "sale_price"
        7 => "image_path"
        8 => "image"
        9 => "store_id"
        10 => "status"
      ]
      #connection: "mysql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:15 [▼
        "id" => 1
        "name" => "Dell Keyboard"
        "type_id" => 1
        "category_id" => 1
        "sub_category_id" => 1
        "description" => "<p>test key</p>"
        "cost_price" => 100.0
        "sale_price" => 500.0
        "image_path" => null
        "image" => null
        "store_id" => 1
        "status" => 1
        "created_at" => "2018-06-02 14:31:32"
        "updated_at" => "2018-06-02 14:31:32"
        "deleted_at" => null
      ]
      #original: array:15 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #forceDeleting: false
    }

我是一个新的雄辩的关系,我参考了laravel文件和其他一些教程,但它没有解决。一定是个愚蠢的错误。

tkqqtvp1

tkqqtvp11#

首先,尝试添加到控制器 with('category') 所以雄辩的人知道你在说这段关系。所以你的控制器应该像

public function index(Request $request)
    {
        $items = InventoryItems::with('category')->get();

        dd($items);

    }

第二,我认为外键的顺序是错误的

return $this->belongsTo('YourModel','foreing_key','other_model_key')

所以你们的关系应该是这样的

return $this->belongsTo('App\ItemCategories', 'category_id', 'id');
soat7uwm

soat7uwm2#

有一些事情正在发生。
首先,laravel在处理模型关系时使用了“急切加载”。
从文件中(https://laravel.com/docs/5.6/eloquent-relationships#eager-装载)
这意味着在您首次访问属性之前,关系数据不会被实际加载。但是,雄辩者可以在查询父模型时“急切地加载”关系。
这意味着拉威尔不会加载你的关系,直到你尝试访问它。
例如,下面的代码将延迟加载类别。

foreach (InventoryItems::all() as $inventoryItem) {
    dump($inventoryItem->category); // Laravel loads the category here and not before
}

还可以在检索模型本身时加载关系。

$items = InventoryItems::all()->load('category'); // Laravel loads the category here.

dd($items);

这里唯一需要注意的是,在进行快速加载时,应该使用关系函数的名称,而不是检索的模型的名称。
如果您的类别关系定义为

public function itemCategory()

你会用

InventoryItems::all()->load('itemCategory');

值得一提的是->load()只适用于集合,而不适用于构建器。
对于构建器,您需要使用->with()
例如。

InventoryItems::all()->load('category'); // all() has already returned a collection so we have to 'load' additional data

InventoryItems::where('condition','=','something')->with('category')->get(); // This is a builder (until we call get() or first()) so we can ask for the data to be delivered 'with' the category to the collection

相关问题