【问题标题】:Property [id] does not exist on this collection instance laravel此集合实例 laravel 上不存在属性 [id]
【发布时间】:2018-04-12 08:06:52
【问题描述】:

在修复我刚刚输入的新代码后,我不断收到此错误,我不知道为什么它给我这个错误“属性 [id] 在此集合实例上不存在”,它指向 id我在 home.blade.php 中的路线。谁能帮我看看 非常感谢

我添加的部分是hire_status部分,在修复它之后,它给了我这个错误。 home.blade.php

    <table class="table table-bordered">
      <tr>
        <th><strong><big>Name: </big></strong></th>     
        <th><strong><big>Hire Status: </big></strong></th>  
        <th><strong><big>Action: </big></strong></th>
      </tr>
      <td>
      <tr>
        @foreach($data as $value)
      <tr>    
      <th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
    <th> 
      @foreach($data1 as $value1)

      {{$value1->hire_status}}

   </th>
      <th><form action="{{  url('/home/'.$value->id.'/delete')  }}" method="get">
        {{ csrf_field() }}
        <button type="submit">Delete</button>
      </form></th>              
      </tr>
      @endforeach
         @endforeach
      </tr>
      </tr>
    </table>

家庭控制器:

    public function getData(){
        $data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
        $data1 = DB::table('hires')->where('hire_status','Yes')->get();

        if(count($data)>0){
        return view('home',compact('data','data1'));
    }else{
    return view('home');
}
}

【问题讨论】:

  • print_r 你的 $data,我们可以看到你的 $data 包含吗?
  • @Rits 对不起,我把 print_r 放在哪里,对不起,我第一次使用它
  • 只是,print_r() 在这个$data['data'] = DB::table('personal_infos')-&gt;where('deleted_at',NULL)-&gt;get()-&gt;sortByDesc('created_at'); print_r($data) 之后;退出;
  • 你为什么要把数组(键)数据删除,然后只用 $data = DB::table('personal_infos')->where('deleted_at',NULL) 检查它->get()‌​->sortByDesc('create‌​d_at');
  • @Rits 我已经完成了 print_r,它包含了我保存在数据库中的所有数据,其中包含多个数组,例如数组 [0] 和数组 [1]

标签: php laravel


【解决方案1】:

只需从数组中删除键, 您正在尝试直接在视图中获取数据,因此直接获取数据而不是数据...

$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()‌​->sortByDesc('create‌​d_at');

改成,

$data = DB::table('personal_infos')->where('deleted_at',NULL)->get()‌​->sortByDesc('create‌​d_at');

【讨论】:

  • 很抱歉再次打扰您,但为什么我的 home.blade.php 只在我的一个数据为 no 时为hire_status 显示 yes
  • 对不起,我吃了午饭。因为您只检索那些仅具有hire_status 的记录,所以$data1 = DB::table('hires')-&gt;where('hire_status','Yes')-&gt;get(); 请查看您的where 条件where('hire_status','Yes'),这样它就不会获取具有雇用状态编号的数据。
  • 是的,这就是我想要的。嗯,也许我给你一个发生的例子,杰克被雇用了,所以他是,但简没有被雇用,所以她没有,但是当我看到整个视图时,他们告诉我简和杰克都被雇用了(两者都是是的)
  • 是的,因为查询是分开的。这就是为什么。您可以使用连接查询仅检索那些有的。在一个查询中加入两个表
  • 等等你明白我的意思吗?我只想看到是结果,但其中一个用户是否,结果显示该用户并且处于是状态,即使它原来是否
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 2020-07-11
  • 2019-07-10
  • 2018-07-09
相关资源
最近更新 更多