Свойство полиморфной модели отношений «многие ко многим» не возвращает коллекцию, а ->get() возвращает. ⇐ Php
Свойство полиморфной модели отношений «многие ко многим» не возвращает коллекцию, а ->get() возвращает.
I have two models Product and ProductType which both have a relationship with the Usp model. I have set up the models like this.
class Product extends Model { use HasFactory, SoftDeletes; protected $guarded = []; public function usps(): MorphToMany { return $this->morphToMany(Usp::class, 'uspable'); } } class ProductType extends Model { use HasFactory, SoftDeletes; protected $guarded = []; public function usps(): MorphToMany { return $this->morphToMany(Usp::class, 'uspable'); } } class Usp extends Model { use HasFactory, SoftDeletes; protected $guarded = []; public function products(): MorphToMany { return $this->morphedByMany(Product::class, 'uspable'); } public function productTypes(): MorphToMany { return $this->morphedByMany(ProductType::class, 'uspable'); } } As far as I can see I have everything setup correctly but for some strange reason, when I call the property of the ProductType model it returns null (i.e. ProductType::find(1)->usps). But when I call the Query Builder method ProductType::find(1)->usps()->get() it gives me the correct results. For the Product model both ->usps and ->usps()->get() seem to work just fine. I have no idea what I am doing wrong here.
Источник: https://stackoverflow.com/questions/780 ... llection-b
I have two models Product and ProductType which both have a relationship with the Usp model. I have set up the models like this.
class Product extends Model { use HasFactory, SoftDeletes; protected $guarded = []; public function usps(): MorphToMany { return $this->morphToMany(Usp::class, 'uspable'); } } class ProductType extends Model { use HasFactory, SoftDeletes; protected $guarded = []; public function usps(): MorphToMany { return $this->morphToMany(Usp::class, 'uspable'); } } class Usp extends Model { use HasFactory, SoftDeletes; protected $guarded = []; public function products(): MorphToMany { return $this->morphedByMany(Product::class, 'uspable'); } public function productTypes(): MorphToMany { return $this->morphedByMany(ProductType::class, 'uspable'); } } As far as I can see I have everything setup correctly but for some strange reason, when I call the property of the ProductType model it returns null (i.e. ProductType::find(1)->usps). But when I call the Query Builder method ProductType::find(1)->usps()->get() it gives me the correct results. For the Product model both ->usps and ->usps()->get() seem to work just fine. I have no idea what I am doing wrong here.
Источник: https://stackoverflow.com/questions/780 ... llection-b
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Создайте данные для таблицы отношений «многие ко многим» в ASP.NET Core MVC.
Anonymous » » в форуме C# - 0 Ответы
- 39 Просмотры
-
Последнее сообщение Anonymous
-