【问题标题】:Polymorphic OneToMany images-products relationship, how to know which images is which?多态OneToMany图像-产品关系,如何知道哪些图像是哪些?
【发布时间】:2020-05-29 15:33:36
【问题描述】:

对于个人项目,我正在尝试使用 laravel 创建一个网上商店。 我有一个产品表和一个图像表,它们具有一对多多态关系。 有些产品是不同的,可能只需要一个图像,而其他产品则需要能够有多个。 我正在努力寻找一种方法来知道哪个图像是哪个。例如,一张图片可能是产品的缩略图,另一张可能是产品一部分的图片等。我应该创建一个新表 image_types 并将 id 存储在我的 images 表中,还是这无效?

产品表

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->text('description')->nullable();
            $table->decimal('stock', 8, 2)->nullable();
            $table->decimal('price', 8, 2)->nullable();
            $table->unsignedBigInteger('product_category_id');
            $table->foreign('product_category_id')->references('id')->on('product_categories')->onDelete('cascade');
            $table->unsignedBigInteger('account_type_id');
            $table->foreign('account_type_id')->references('id')->on('account_types')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

图片表

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateImagesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('images', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('url');
            $table->morphs('imageable');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('images');
    }
}

【问题讨论】:

  • 这不是多态关系的用途。您的产品可以有 1 个或多个图像,这只是一个标准的非多态 hasMany 关系。一个特定的图像可以用于多个产品吗?
  • 所有产品都有独特的图像。
  • 那么这只是hasMany的关系。
  • 好的,那么我应该如何为此设计我的数据库,以便能够将缩略图和另一张图片添加到一个产品,而仅将缩略图添加到另一个产品?
  • 在模型中设置关系。

标签: laravel polymorphism


【解决方案1】:

要为一个产品保存多张图片,您可以将一列中的图片网址保存为数组字符串 为此,您必须在 Image 模型中转换 url 列,如下所示:

protected $casts = [
    'url' => 'array' // when set the url it convert to string and when get data it convert to array
];

然后,当您要保存图像 url 时,为任何类型的图像设置密钥,例如设置缩略图和原始图像,请执行以下操作:

$urlArray = [];
$urlArray['thumbnail'] = $thumbnailUrl;
$urlArray['original'] = $originalUrl;

这是一个简单的例子,你可以用这种方式为任何类型的图像设置键,即使你想为缩略图保存几个大小,你可以执行以下操作:

$urlArray['thumbnail']['240'] = $thumbnailUrlLow; // or set key ['thumbnailLow']
$urlArray['thumbnail']['320'] = $thumbnailUrlMed;
$urlArray['thumbnail']['480'] = $thumbnailUrlHigh;

要获取产品的缩略图网址,请执行以下操作:

$image->url['thumbnail']['320']

获取原始图片大小:

$iamge->url['original'];

您可以为任何类型的产品图像设置很多键。

不要忘记为目标列定义protected $casts,并设置转换为数组的时间。

【讨论】:

    【解决方案2】:
    # images migration file
    public function up()
    {
        Schema::create('images', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('product_id');
            $table->string('url');
            // Maybe add the following column if you really need to be able
            // to tell the difference between what is and isn't a thumbnail.
            // $table->enum('type', ['thumbnail', 'other']);
    
            $table->foreign('product_id')->references('id')->on('products');
        });
    }
    
    # Image model
    class Image extends Model
    {
        public function product()
        {
            // an Image belongs to a Product, using the foreign key 'product_id'
            return $this->belongsTo(Product::class, 'product_id');
        }
    }
    
    # Product model
    class Product extends Model
    {
        public function images()
        {
            // a Product has one or many images (Image), using the foreign key 'product_id'
            return $this->hasMany(Image::class, 'product_id');
        }
    }
    
    $product = Product::find($product_id);
    // get a collection of all the Product's images
    $product->images
    
    $image = Image::find($image_id);
    // get THE product this image belongs to
    $image->product
    

    更多阅读:

    【讨论】:

    • 感谢您的回答,只有某些产品需要能够拥有多达 7 张不同的图片,例如汽车,我需要一些内部、轮胎等的图片,我需要在产品详细信息页面上对这些图片进行分类
    • 这些限制应该是您业务逻辑的一部分。在存储新产品(及其图像)时进行适当的验证。
    猜你喜欢
    • 1970-01-01
    • 2017-11-06
    • 2011-04-06
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2021-02-13
    • 2014-07-09
    • 1970-01-01
    相关资源
    最近更新 更多