App\Todo::lead 必须 return 一个关系实例,但是 "null" 是 returned。是否使用了 "return" 关键字?
App\Todo::lead must return a relationship instance, but "null" was returned. Was the "return" keyword used?
我有 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');
}
}
这是 Todo 模型
<?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'));
}
这是 blade 我想从 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
请问谁能告诉我我做错了什么。而且我知道型号名称和主键略有不同未相应设置为 laravel。我的模型关系正确吗?
Let me clarify few points to you. Firstly you need to put a return
in each relationship. For example:
public function lead() {
return $this->belongsTo('App\Tbl_leads', 'lead_id');
}
Next thing you are showing this portion {{$task->lead->first_name}}
which is called lazy loading. It means if you are displaying 100
records of Todo
then you are querying database 101
times. 1
for Todo
and 100
times for lead->first_name
. Which is not good. So what you can do in your index
method pass your relation in with()
so that it will be eager loaded. Means it will become just one or two query or simply a join. So it will be fast. Example of your index
method...
public function index()
{
$tasks = Todo::with('lead')->latest()->paginate(5);
return view('taskmanagement.cruds.index',compact('tasks'));
}
我有 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');
}
}
这是 Todo 模型
<?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'));
}
这是 blade 我想从 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
请问谁能告诉我我做错了什么。而且我知道型号名称和主键略有不同未相应设置为 laravel。我的模型关系正确吗?
Let me clarify few points to you. Firstly you need to put a
return
in each relationship. For example:
public function lead() {
return $this->belongsTo('App\Tbl_leads', 'lead_id');
}
Next thing you are showing this portion
{{$task->lead->first_name}}
which is called lazy loading. It means if you are displaying100
records ofTodo
then you are querying database101
times.1
forTodo
and100
times forlead->first_name
. Which is not good. So what you can do in yourindex
method pass your relation inwith()
so that it will be eager loaded. Means it will become just one or two query or simply a join. So it will be fast. Example of yourindex
method...
public function index()
{
$tasks = Todo::with('lead')->latest()->paginate(5);
return view('taskmanagement.cruds.index',compact('tasks'));
}