代码之家  ›  专栏  ›  技术社区  ›  Beresyus

我可以在Laravel多态关系中更改类型列吗?

  •  0
  • Beresyus  · 技术社区  · 1 年前

    我在Laravel项目中有三个不同的模型;车队、用户、司机。驱动程序与用户的型号不同,因为它们无法进行身份验证。不管怎样,还有一个模型:地址。

    我正在将地址值存储在 addresses 表,地址模型。列如下:;

    地址 : addressable_type (int) , addressable_id (int) ...

    还有一个名为Type的模型,它存储项目中所有内容的类型。例如,它存储授权类型(超级用户、用户、管理员等)、在Models\path下给出的雄辩模型类型(App\Models\User、App\Models\Driver等)、支付类型等。。。

    我想你明白了,我该问你什么,但让我们继续吧。如果你弄清楚了,我没有设置类型 地址 . addressable_type 作为 varchar(255) 。通常,多晶型模型可以读取 App\Models\* 格式,但我想存储雄辩模型的id types . id 桌子。

    addressable_type    | 10 // this refers to `types`.`id`
    addressable_id      | 1
    country             | 225
    state               | 2175
    city                | 108155
    address             | NULL
    status              | 1
    created_at          | 2024-08-31 14:50:13
    updated_at          | 2024-08-31 14:50:13
    
    id                  | 10
    type                | App\Models\Convoy // so this is record in the types table
    label               | 2
    created_at          | 2024-08-31 14:50:12
    updated_at          | 2024-08-31 14:50:12
    

    Convoy.php如下所示;

    class Convoy extends Model
    {
        use HasFactory;
        protected $guarded = [];
    
        public function address() : MorphOne
        {
            return $this->morphOne(Address::class, 'addressable');
        }
    }
    

    下面给出的Address.php;

    class Address extends Model
    {
        use HasFactory;
        protected $guarded = [];
    
        public function addressable() : MorphTo
        {
            return $this->morphTo();
        }
    
        public function status() : BelongsTo
        {
            return $this->belongsTo(Status::class);
        }
    }
    

    下面给出了HomeController.php;

    public function convoys() : View
        {
            $test = Convoy::with('address')->find(6);
            dd($test);
            return view('convoys', compact('convoys'));
        }
    

    结果;

    App\Models\Convoy {#1228 ▼ // app/Http/Controllers/HomeController.php:50
      ...
      #relations: array:1 [▼
        "address" => null
      ]
      ...
    }
    

    我找不到任何地址。

    2 回复  |  直到 1 年前
        1
  •  1
  •   Hamdallah    1 年前

    您可以更新AppServiceProvider并添加以下内容

    use Illuminate\Database\Eloquent\Relations\Relation;
    
    Relation::morphMap([
            10 => App\Models\Convoy::class,
            11 => App\Models\User::class,
            12 => App\Models\Driver::class,
        ]);
    
        2
  •  0
  •   LewBlu    1 年前

    我建议您按预期使用多态关系。
    您将拥有以下多态关系:

    <?php
    class Address extends Model
    {
        public function parent(): MorphTo
        {
            return $this->morphTo();
        }
    }
    

    然后,我会进一步建议您使用trait将其附加到任何具有关联地址的模型上(这将有助于防止重复代码)

    <?php
    trait HasAddress {
        public function address(): MorphOne
        {
            return $this->morphOne(Address::class);
        }
    }
    

    然后,在你的车队、用户、司机或其他任何有地址的人中,你可以简单地在开头添加以下内容。

    use HasAddress;