第一个原因你没有写他们的主从关系
第一个原因你没有写他们的主从关系
主
<?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(),