Свойство полиморфной модели отношений «многие ко многим» не возвращает коллекцию, а ->get() возвращает.Php

Кемеровские программисты php общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Свойство полиморфной модели отношений «многие ко многим» не возвращает коллекцию, а ->get() возвращает.

Сообщение Anonymous »


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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Php»