Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 产品与形象的拉维多态关系_Php_Laravel - Fatal编程技术网

Php 产品与形象的拉维多态关系

Php 产品与形象的拉维多态关系,php,laravel,Php,Laravel,我对Laravel不太熟悉,我试图弄清楚如何使用产品及其图像之间的多态关系 我为产品和图像设计了模型,并相应地定义了关系,但在我的案例中,我使用了对象id和对象类型,而不是可成像id和可成像类型 这是我的产品结构和形象模型 class Product extends Model { /** * The attributes that are mass assignable. * * @var array */ protected $fil

我对Laravel不太熟悉,我试图弄清楚如何使用产品及其图像之间的多态关系

我为产品和图像设计了模型,并相应地定义了关系,但在我的案例中,我使用了对象id对象类型,而不是可成像id可成像类型

这是我的产品结构和形象模型

class Product extends Model {

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'category_id', 'added_by', 'title',
        'product_unique_code', 'description', 'currency', 'product_status'
    ];

    /**
     * Get category this product belongs to
     * @return Category
     */
    public function category() {
        return $this->belongsTo(Category::class);
    }

    /**
     * Get user who added this product
     * @return User
     */
    public function addedByUser() {
        return $this->belongsTo(User::class);
    }

    /**
     * List of images
     * @return type
     */
    public function images() {
        return $this->morphMany(App\Image::class, 'object');
    }
}

这是要求,在创建新产品时将上载图像,如在保存产品之前上载图像,类似于在存储到数据库之前输入产品的详细信息,那么如何使用上述模型结构执行此任务

还有一件事,我想使用自定义多态类型,而不是在对象类型字段中使用完整的类名,比如App\Product,正如这里建议的那样,我已经在我的AppServiceProvider的引导功能中注册了morphMap,但它仍在使用App\Product而不是“产品”

use Illuminate\Database\Eloquent\Relations\Relation;

Relation::morphMap([
    'products' => App\Product::class,
]);
use Illuminate\Database\Eloquent\Relations\Relation;

Relation::morphMap([
    'products' => App\Product::class,
]);