問題描述
我有一個關于在 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
fromProduct
Modelclass 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_id
或productable_type
添加條目.您需要確保為此目的使用morph
關系.First of all, please understand that you can not add an entry to
productable_id
orproductable_type
from ahasMany()
relation. You need to make sure you are using amorph
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 變形關系的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!