Laravel变形关系 [英] Laravel morph relationship

查看:75
本文介绍了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
    • 名称
    • shops
      • id
      • user_id
      • name
      • 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('App\Product');
          }
      }
      
      class Product extends Model{
          public function productable(){
              return $this->morphTo();   
          }
      }
      
      class Event extends Model{
          public function product(){
              return $this->morphOne('App\Product','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:

      Illuminate\Database\QueryException 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 模型

      class Shop extends Model
      {
        public function products()
        {
          return $this->hasMany('App\Product');
        }
      }
      
      class Product extends Model
      {
        public function shop()
        {
          return $this->belongsTo('App\Shop');
        }
      
        public function productable()
        {
          return $this->morphTo();
        }
      }
      
      class Event extends Model
      {
        public function product()
        {
          return $this->morphOne('App\Product', '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变形关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆