首页 > 解决方案 > 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);
}

标签: phplaravel

解决方案


通过 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

推荐阅读