php - Laravel 一对多关系不再起作用
问题描述
我的这种关系上周还有效,但它停止了工作。
当我在刀片模板中使用它时,它说:尝试获取非对象的属性“名称”。
奇怪的是,当我在 dd() 中使用它时,它完美显示,但是当我尝试在刀片中打印它时,它没有。
代码支付控制器
<?php
namespace App\Http\Controllers;
use App\Betaling;
use App\Settings;
use Illuminate\Http\Request;
class PaymentsController extends Controller
{
public function index(){
$user_id = auth()->user('id');
$role = auth()->user()->role_id;
$settings = Settings::where('user_id', $user_id->id)->first();
if ($role === 1){
$totaalaantalbetalingen = Betaling::all();
} else {
$totaalaantalbetalingen = Betaling::where('user_id', $user_id->id)->get();
}
return view('payments.index', compact(['totaalaantalbetalingen','role', 'user_id', 'settings']));
}
}
代码 index.blade.php
@foreach($totaalaantalbetalingen as $betaling)
<tr>
<th scope="row">{{ $betaling->id }}</th>
@if($role === 1)
<td>{{ $betaling->user->name }} </td>
@endif
<td>{{ $betaling->customer->name }}</td>
<td>{{ $betaling->reference}}</td>
<td>€ {{ $betaling->amount}}</td>
<td>{{ $betaling->time_of_invoice }}</td>
<td>{{ $betaling->time_of_payment }}</td>
<td></td>
<td>{{ $betaling->user->location }}</td>
</tr>
@endforeach
代码用户模型:
public function betalingen(){
return $this->hasMany(Betaling::class);
}
代码Betaling模型:
public function customer(){
return $this->belongsTo(User::class);
}
public function user(){
return $this->belongsTo(User::class);
}
代码客户型号:
public function betaling(){
return $this->hasMany(Betaling::class);
}
解决方案
通过 auth()->user()->id
并在刀片文件中检查!empty($totaalaantalbetalingen)
代码用户模型:
public function betalingen(){
return $this->hasMany('App\Betaling','betalingenid','id'); // pass foreign key for ex.
}
代码Betaling模型:
public function customer(){
return $this->belongsTo('App\User','customer_id','id');// pass foreign key for ex.
}
public function user(){
return $this->belongsTo('App\User','user_id','id');// pass foreign key for ex.
}
代码客户型号:
public function betaling(){
return $this->hasMany('App\Betaling','customer_id','id');// pass foreign key for ex.
}
控制器
public function index(){
$user_id = auth()->user()->id;
$role = auth()->user()->role_id;
$settings = Settings::where('user_id', $user_id)->first();
if ($role == 1){
$totaalaantalbetalingen = Betaling::all();
} else {
$totaalaantalbetalingen = Betaling::where('user_id', $user_id)->get();
}
return view('payments.index', compact(['totaalaantalbetalingen','role', 'user_id', 'settings']));
}
刀片.php
@if(!empty($totaalaantalbetalingen) && isset($totaalaantalbetalingen))
@foreach($totaalaantalbetalingen as $betaling)
<tr>
<th scope="row">{{ $betaling->id ?? '' }}</th>
@if($role === 1)
<td> {{ $betaling->user->name ?? '' }} </td>
@endif
<td> {{ $betaling->customer->name ?? '' }} </td>
<td> {{ $betaling->reference ?? '' }} </td>
<td> € {{ $betaling->amount ?? '' }} </td>
<td> {{ $betaling->time_of_invoice ?? '' }} </td>
<td> {{ $betaling->time_of_payment ?? '' }} </td>
<td> </td>
<td> {{ $betaling->user->location ?? '' }} </td>
</tr>
@endforeach
@endif
推荐阅读
- python - comparing elements of a list of list and removing
- flutter - Flutter:如何在 PageView 中嵌入多个水平 Webview
- printing - ZPL 命令条码 3 个零会产生问题
- asynchronous - 如何将 Future 转换为 Stream?
- javascript - 如何对多个 vuex 模块使用扩展运算符?
- c# - 如何使用 C# 获取频道数据
- xml - VB.NET DataGrid 列绑定
- html - 当元素使用 `renderer2.removeChild()` 从 DOM 中删除时,有什么方法可以取消订阅子组件的订阅?
- windows-subsystem-for-linux - 是否可以通过“wsl --import”制作distro.exe?
- javascript - 将字符串转换为数组,其中有很多空格