我正在尝试从表Customer,Vouchers和Bills中获取数据,并将其显示在视图上。客户与Bills和Vouchers有很多关系。这是我的模型代码:
客户:
public function Bills(){
return $this->hasMany('App\Bill', 'customer', 'id');
}
public function Vouchers(){
return $this->hasMany('App\Voucher', 'customer_name', 'id');
}
以下是我的优惠券模型代码:
public function Customer(){
return $this->belongsTo('App\Customers', 'id', 'customer_name');
}
以下是Bills模型的代码:
public function Customer(){
return $this->belongsTo('App\Customers', 'id', 'customer');
}
这是我为此目的所做的控制器功能:
public function client_search(Request $request){
$date_from = $request['from-date-city'];
$date_to = $request['to-date-city'];
$client = $request['client'];
$results = Customers::with('Bills', 'Vouchers')
->where('id', $client)->get();
return view('pages.prints.ledger-report-clientwise')->with('results', $results);
}
这就是我试图在视图中显示的方式:
@foreach($results as $result)
<tr>
<td>{{$result->Vouchers->date}}</td>
<td>{{$results->Vouchers->narration}}</td>
<td>{{$result->Vouchers->amount}}</td>
<td>{{$result->Bill->total_amount}}</td>
<td>{{$result->balance}}</td>
</tr>
@endforeach
以下是我遇到的错误:
(2/2) ErrorException
Property [date] does not exist on this collection instance. (View: D:\Code\PHP\Code\CrownBillingSystem\resources\views\pages\prints\ledger-report-clientwise.blade.php)
in Collection.php (line 1661)
at CompilerEngine->handleViewException(object(Exception), 1)
in PhpEngine.php (line 44)
at PhpEngine->evaluatePath('D:\\Code\\PHP\\Code\\CrownBillingSystem\\storage\\framework\\views/f61b2b4b91bb544a7ee3c5532f7b257b0da1b8fb.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'results' => object(Collection)))
in CompilerEngine.php (line 59)
at CompilerEngine->get('D:\\Code\\PHP\\Code\\CrownBillingSystem\\resources\\views/pages/prints/ledger-report-clientwise.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'results' => object(Collection)))
in View.php (line 137)
at View->getContents()
in View.php (line 120)
at View->renderContents()
in View.php (line 85)
at View->render()
in Response.php (line 38)
at Response->setContent(object(View))
in Response.php (line 201)
at Response->__construct(object(View))
in Router.php (line 617)
at Router->prepareResponse(object(Request), object(View))
in Router.php (line 574)
at Router->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in SubstituteBindings.php (line 41)
at SubstituteBindings->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in VerifyCsrfToken.php (line 65)
at VerifyCsrfToken->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ShareErrorsFromSession.php (line 49)
at ShareErrorsFromSession->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in StartSession.php (line 64)
at StartSession->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in AddQueuedCookiesToResponse.php (line 37)
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in EncryptCookies.php (line 59)
at EncryptCookies->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Router.php (line 576)
at Router->runRouteWithinStack(object(Route), object(Request))
in Router.php (line 535)
at Router->dispatchToRoute(object(Request))
in Router.php (line 513)
at Router->dispatch(object(Request))
in Kernel.php (line 176)
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ValidatePostSize.php (line 27)
at ValidatePostSize->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in CheckForMaintenanceMode.php (line 46)
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Kernel.php (line 151)
at Kernel->sendRequestThroughRouter(object(Request))
in Kernel.php (line 116)
at Kernel->handle(object(Request))
in index.php (line 53)
at require_once('D:\\Code\\PHP\\Code\\CrownBillingSystem\\public\\index.php')
in server.php (line 21)
答案 0 :(得分:1)
嗯,这是你的问题。 Vouchers
是hasMany
关系,这意味着当使用Collection
调用时,它将返回$result->Vouchers
对象,这意味着它本质上是一个数组而不是单个对象。如果您想访问$result->Vouchers->date
,则必须循环遍历每个Vouchers
并一次回显一个,或使用第一个:
@foreach($results as $result)
@foreach($result->Vouchers AS $voucher)
<span>The date is {{ $voucher->date }}</span>
@endforeach
@endforeach
下一个问题是你有<td>{{ $results->Vouchers->narration }}</td>
,这与以前的问题相同;尝试访问Collection
的属性,而不是单个$result
或Voucher
对象。 (这看起来像是一个错字)
你有正确的想法,但是当你访问Collection
时,你需要知道何时访问Object
vs,所以请阅读Laravel的馆藏{{3}和Eloquent https://laravel.com/docs/5.4/collections