我有 1 个表:“交易”。
1 表“员工”。
我正在尝试从 Transactions 上下文中的表“Employee”中检索一些信息,以便使用“Employee”中的 id 填充下拉菜单(因为每个事务都需要链接到 Employee)。此时,当我运行我的代码时,我没有得到任何结果。
我有一个引用“id”的外键“codigo_referido_id”(来自员工表)。
交易模型
class Transaction extends Model
{
use HasFactory;
public function employees(){
return $this->belongsTo(Employee::class , 'codigo_referido_id');
}
}
员工模型
class Employee extends Model
{
use HasFactory;
protected $table = 'employees';
public $timestamps = true;
protected $fillable = [
'nombre',
'codigo_referido'
];
}
事务控制器
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Transaction $transaction)
{
$transactions = Transaction::all();
return view('transaction.create' , compact('transactions'));
}
刀片模板(目前我只有这个来检查结果):
{{ dd($transactions->employees()) }}
尽管如此,它会用员工填充 Select 字段。
此时我收到此错误:
BadMethodCallException 方法 Illuminate\Database\Eloquent\Collection::employees 不存在。
有人能帮我吗?
问候