存在將關係數據輸出到查看的問題。 錯誤消息是:「嘗試獲取非對象視圖的屬性」。要查看的關係數據 - Laravel 5.2
解釋所有任務取決於一份工作。 所以任務belongsTo Job和Job有很多任務。 我在我的模型中擁有所有關係,並在Tinker中測試一切正常。
在我看來,我輸出的每個任務名稱和作業名稱
@foreach ($tasks as $task)
<div class="list-item">
<span class="item-name">{{ $task->job->name }}
<span class="item-name">{{ $task->name}} </span>
</div>
@endforeach
我TaskController的指數函數:叮叮
id: 1,
assigned_user_id: 1,
team_id: 4,
name: "Label many to many ",
duration: 2,
created_at: "2016-06-16 14:50:57",
updated_at: "2016-07-05 09:10:34",
job_id: 1,
duedate: "0000-00-00 00:00:00",
status_id: 3,
job: App\Job {#702
id: 1,
name: "quia",
jobnumber: "8076",
customer_id: 2,
status_id: 0,
created_at: null,
updated_at: null,
},
user: null,
public function index(Request $request)
{
$label = Label::all();
$user = User::all();
$task = Task::orderBy('duedate')->get();
$team = Team::all();
$customer = Customer::all();
$status = Status::all();
$job = Job::all();
return view('tasks.index')->with([
'tasks' => $task,
'teams' => $team,
'customers' => $customer,
'labels' => $label,
'users' => $user,
'jobs' => $job,
'statuses' => $status,
]);
}
表架構/輸出關係
**示範工作**
class Job extends Model
{
protected $fillable = ['name', 'jobnumber', 'customer_id', 'status_id'];
/**
* Get all Task for Job
*/
public function task()
{
return $this->hasMany(Task::class);
}
任務模式
public function job()
{
return $this->belongsTo(Job::class);
}
希望你能幫助我,謝謝!
請分享您的表格架構和關係代碼 – smartrahat
如果一個Job有多個Task,那麼這個關係應該叫做tasks而不是task。它可能不會解決問題。 – AntoineB