简体   繁体   English

试图使用Laravel Blade foreach获得非对象错误的属性

[英]Trying to get property of non-object error with laravel blade foreach

Hi I have a Trying to get property of non-object error which I just can't seem to solve. 嗨,我有一个Trying to get property of non-object错误的Trying to get property of non-object但我似乎无法解决。 In my view I have a @foreach , using relationship tables. 在我看来,我有一个@foreach ,使用关系表。 The relationships seems to be fine when I check them. 当我检查它们时,这种关系似乎很好。 The are also no null values in my banks table. 我的bank表中也没有空值。 I have a user principles table, banks table that I used as relationships to the banking accounts table. 我有一个用户原则表,银行表,用作与银行帐户表的关系。 With the user principles I do not use a @foreach and data displays {{ $bankAcc->userprinciple->principle }} correctly. 根据用户原则,我不会使用@foreach并且数据显示正确{{ $bankAcc->userprinciple->principle }}

Here is my Bank Account Model (the relationship with banks): 这是我的银行帐户模型(与银行的关系):

public function banks(){
    return $this->belongsTo('App\Models\Banks', 'bank_detail_id');
}

Here is my Controller: 这是我的控制器:

public function edit($id){
    $bankAcc = BankingAccount::where('id', $id)->with('userprinciple')->with('banks')->first();
    return view('admin.administration.banking.accounts.edit', compact('bankAcc'));
}

My view: 我的观点:

<select class="form-control" name="name_of_bank">
    @foreach($bankAcc->banks as $bankA)
        <option value="{{ $bankA->id }}">{{ $bankA->name }}</option>
    @endforeach
</select>

Try This 尝试这个

<select class="form-control" name="name_of_bank">
        @foreach($bankAcc['banks'] as $bankA)
          <option value="{{ $bankA['id'] }}">{{ $bankA['name'] }}</option>
        @endforeach
      </select>

try dump the result $bankAcc->banks and if your getting an eloquent collection then below code will fix the issue 尝试转储结果$bankAcc->banks ,如果您得到一个雄辩的集合,那么下面的代码将解决此问题

<select class="form-control" name="name_of_bank">
    @foreach($bankAcc->banks as $bankA)
         @if($bankA)
            <option value="{{ $bankA->id }}">{{ $bankA->name }}</option>
         @endif
    @endforeach
</select>

if your getting a single model then you dont need a loop 如果您得到一个模型,那么您不需要循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM