我有 Todo 和 Tbl_leads 模型以及相应的表 todos 和 tbl_leads。当我尝试使用潜在客户名称时,它会给我一个错误。
#This is Tbl_leads model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tbl_leads extends Model
{
//Table Name
protected $table = 'tbl_leads';
//Primary key
public $primaryKey = 'ld_id';
//Timestamps
public $timestamps = true;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'ld_id', 'first_name', 'last_name', 'email',
];
public function tasks() {
$this->hasMany('App\Todo', 'lead_id','ld_id');
}
}
这是待办事项模型
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Todo extends Model
{
use SoftDeletes;
protected $fillable = [
'title',
'description',
'user_id',
'outcome_id',
'lead_id',
'tasktype_id',
'due_time',
'complete_time',
];
protected $casts = [
'due_time' => 'datetime',
'complete_time',
];
public function lead() {
$this->belongsTo('App\Tbl_leads', 'lead_id');
}
}
这是我的索引方法
public function index()
{
$tasks = Todo::latest()->paginate(5);
return view('taskmanagement.cruds.index',compact('tasks'));
}
这是刀片,我想从 Tbl_leads 模型中获取名字
div class="card-body">
<ul class="todo-list" data-widget="todo-list">
@forelse($tasks as $task)
<div class="col-3">
@if(!empty($task->lead_id))
<div>
<small>Related to</small>
</div>
<div class="mt-0">
<a href="#">
<strong class="mx-4">{{$task->lead->first_name}}</strong>
</a>
</div>
@endif
</div>
</div>
</div>
</li>
@empty
<p class="text-center">No Tasks is available</p>
@endforelse
Plz 谁能告诉我我做错了什么。而且我知道型号名称和主键略有不同未相应地设置为 laravel。我的模型关系是否正确?
Smart猫小萌