@Hfnas

Как сделать вывод картинок товара?

Есть модели Product и Image (связаны отношением многие к одному Image to Product).
Привожу файл миграции:
Schema::create('images', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string("name",  100);
            $table->string("path");
            $table->string("entity");
            $table->foreignUuid("product_id")->nullable()->index()->constrained("products");
}


Модель Product
class Product extends Model
{
    /**
     * Indicates if the model's ID is auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The data type of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';
    protected $table = 'products';
    protected $guarded = false;

   public function images()
    {
        return $this->hasMany(Image::class, 'id', 'product_id');
    }
}


Модель Image
class Image extends Model
{
    /**
     * Indicates if the model's ID is auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The data type of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';
    protected $table = 'images';
    protected $guarded = false;

    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'product_id');
    }
}

Задача: вывести все картинки всех продуктов в шаблоне blade,
Далее, в контролере я делаю запрос:
$products = Product::with(['images'])->get();
но , к сожалению у каждого товара нулевой массив images. Почему?
  • Вопрос задан
  • 81 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы