你好我想查詢從我的客戶端控制器三個表,我的數據庫的快速概覽,users
clients
projects
tasks
一個user
hasMany
clients
,projects
和tasks
這些projects
和tasks
也belongTo
a client
。Laravel查詢3個表
所以我在客戶機控制器,我想查詢登錄users
clients
projects
,但是當我嘗試這樣做我拋出一個未定義的方法錯誤:
BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::user()
我m不知道爲什麼會發生這種情況,我單獨查詢客戶端項目並且它工作正常,但是當我添加一個附加層時,它會引發上述錯誤。
我是Laravel 4的新手,所以希望能得到一些幫助糾正錯誤的指導,並幫助我瞭解哪裏出錯。
我的代碼如下:
ClientController.php
public function show($id)
{
$client = Client::find($id);
$client->load(array('projects' => function($query)
{
// With the clients for each project
$query->with('user');
}));
// Create an empty array
$associated = array();
// Loop through client projects
foreach($client->projects as $project):
// Loop through project users
foreach($project->user as $user):
// Check if the user is the same as the logged in user
if($user->id == Auth::user()->id){
// If yes add the $project to the $associated array
array_push($associated, $project);
}
endforeach;
endforeach;
// show the view
return View::make('clients.show')
->with('client', $client);
}
客戶/ show.blade.php
<?php $clients = $client->projects; ?>
@if (Auth::check())
@if (count($clients) > 0)
@foreach ($clients as $project)
<div class="one-third column">
<div class="projects">
<ul class="data">
<li><label>Project Name: </label><a class="btn btn-small btn-success" href="{{ URL::to('project/' . $project->id.'/show') }}"> {{ $project->project_name }}</a></li>
<li><label class="titletoggle">Project Brief <p>(click to toggle)</p></label><p class="brief">{{ $project->project_brief }}</p></li>
</ul>
<ul class='buttonslist'>
<li><button><a href="{{ URL::to('project/' . $project->id . '/edit') }}">Edit Project</a></button></li>
<li><button><a href="/task/create">Create Task</a></button></li>
<li><button><a href="/task/view/">View Tasks</a></button></li>
</ul>
</div>
</div>
@endforeach
@else
<h3>You have no projects click <a href="/project/create">here to create a project</a></h3>
@endif
@endif
非常感謝,工作!在將來加載時會考慮到這一點 – 001221