沃梦达 / 编程问答 / php问题 / 正文

Laravel 变形关系

Laravel morph relationship(Laravel 变形关系)

本文介绍了Laravel 变形关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在 Laravel 中保存多态关系的问题.这是我想在 Laravel 中创建的模型.

I have a question regarding saving polymorphic relationships in Laravel. This is the model i would like to create in laravel.

一家商店有很多产品,一个产品可以是物品"、事件"或服务".

A shop has many products, and a product can be either an "item" an "event" or a "service".

我有以下表格:

  • 商店
    • id
    • user_id
    • 姓名
    • id
    • 公开
    • 标题
    • 说明
    • id
    • shop_id
    • productable_id
    • productable_type

    这是我设置模型的方式:

    This is how i set up the models:

    class Shop extends Model{
        public function products(){
            return $this->hasMany('AppProduct');
        }
    }
    
    class Product extends Model{
        public function productable(){
            return $this->morphTo();   
        }
    }
    
    class Event extends Model{
        public function product(){
            return $this->morphOne('AppProduct','productable');
        }
    }
    

    我希望能够做到以下几点:

    I want to be able to do the following:

    $shop = Shop::first()
    $event = Event::create(['title'=>'Some event']);
    $service = Service::create(['title' => 'Some service']);
    $shop->products()->save($event);
    $shop->products()->save($service);
    

    但它不起作用!当我尝试保存我得到的关系时:

    But it doesn't work! When i try to save the relation i get:

    IlluminateDatabaseQueryException with message 'SQLSTATE[HY000]: General error: 1 no such column: shop_id (SQL: update "accendo_events" set "updated_at" = 2016-11-26 10:11:02, "shop_id" = 1 where "id" = 1)'
    

    有人知道这是哪里出了问题吗?我可能对这种关系有些误解...

    Anyone have an idea of where this is going wrong? I probably misunderstood something about this type of relationship...

    推荐答案

    首先从 Product Model 添加到 Shop 的后台关系

    First of all add a back relation to Shop from Product Model

        class Shop extends Model
        {
          public function products()
          {
            return $this->hasMany('AppProduct');
          }
        }
    
        class Product extends Model
        {
          public function shop()
          {
            return $this->belongsTo('AppShop');
          }
    
          public function productable()
          {
            return $this->morphTo();
          }
        }
    
        class Event extends Model
        {
          public function product()
          {
            return $this->morphOne('AppProduct', 'productable');
          }
        }
    
    

    现在,我不确定您为什么要尝试创建一个空事件并将其添加到所有产品中,但是如果您想为任何用例执行此操作...请遵循以下方法...:)

    Now, I am not sure why you are trying to make an empty event and add it to all the products, but still if you want to do it for whatever use cases... please follow the below approach... :)

    $shop = Shop::first();            //or $shop = Shop::find(1);
    
    foreach($shop->products as $product) {
      $event = Event::create([]);
      $service = Service::create([]);
    
      $product->productable()->saveMany([
        $event, $service
      ]);
    }
    

    如果有什么不起作用,请在下面的评论中告诉我:)

    Let me know in the comments below if something doesn't work :)

    首先,请理解您不能从 hasMany() 关系中向 productable_idproductable_type 添加条目.您需要确保为此目的使用 morph 关系.

    First of all, please understand that you can not add an entry to productable_id or productable_type from a hasMany() relation. You need to make sure you are using a morph relation for such purposes.

    其次,由于您尝试先添加产品而不是先添加事件,因此插入方法对您不起作用.请注意,您必须先尝试创建活动或服务,然后再尝试与商店关联.

    Secondly, since you are trying to add products first and not events first, the insertion method is not working out for you. Please note that you must try to create an Event or Service first and then try to associate with a shop.

    最简单的方法是

    $shop = Shop::first();
    
    $event = Event::create(['title' => 'Some Event']);
    $event->product()->create(['shop_id' => $shop->id]);
    
    $service = Service::create(['title' => 'Some Service']);
    $service->product()->create(['shop_id' => $shop->id]);
    

    您也可以尝试遵循我的第一种方法,但我刚才提到的方法肯定可以工作...实际上这就是插入/创建的方式.

    You can also, try to follow my first approach, but the one I just mentioned is definitely supposed to work... and actually that is how it is meant to be inserted/created.

    这篇关于Laravel 变形关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 变形关系

基础教程推荐