第一个原因你没有写他们的主从关系

第一个原因你没有写他们的主从关系

<?php

namespace App\Models;
use App\Models\Audience;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PromotionTask extends Model
{
    use HasFactory;
    public function audiences(): BelongsTo
    {
        return $this->belongsTo(Audience::class);
    }
}

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Models\PromotionTask;
class Audience extends Model
{
    use HasFactory;
    public function promotiontask(): HasMany
    {
        return $this->hasMany(PromotionTask::class);
    }
}

这样写 id依据 显示出来是name

 // ->options(Audience::all()->pluck('name', 'id'))
                                    ->relationship('audiences', 'name')
                                    ->preload(),
posted @ 2023-09-01 16:11  yjxQWQ  阅读(8)  评论(0编辑  收藏  举报