此集合实例上不存在属性 [名称]

Property [name] does not exist on this collection instance(此集合实例上不存在属性 [名称])
本文介绍了此集合实例上不存在属性 [名称]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在数据库中有 3 个表(用户、区域、区域用户),

用户表:ID名称用户名密码

区域表:ID姓名

area_user 表:ID用户身份area_id

我正在尝试创建一个(列出用户页面),它将显示所有用户表列以及用户分配的区域.

User.php 模型文件

 belongsToMany('AppArea','area_user');}

Area.php 模型文件:

belongsToMany('AppUser','area_user','area_id','user_id');}}

UserController@index 文件:

middleware('auth');}/*** 显示资源列表.** @return IlluminateHttpResponse*/公共函数索引(){$users = User::get();return view('users._list',['用户'=>$用户]);}

最后是表格视图文件:-

@foreach($users as $u)<tr role="row" class="odd"><td class="sorting_1">{{$u->name}}</td><td></td><td>{{$u->areas]}}</td><td>{{route('show_user', ['id' => $u->id])}}</td></tr></tbody>@endforeach

如果我只使用区域属性 ($u->areas) 在用户表视图(指定区域)中输入,它将显示所有区域列:-

<预><代码>[{身份证":3,"name": "C",location_id":1,created_at":空,updated_at":空,枢轴":{"user_id": 4,area_id":3}},{身份证":4,"name": "D",location_id":2,created_at":空,updated_at":空,枢轴":{"user_id": 4,area_id":4}}]

<小时>

 @foreach($users as $u)<tr role="row" class="odd"><td class="sorting_1">{{$u->name}}</td><td></td><td>{{$u->areas->name]}}</td><td>{{route('show_user', ['id' => $u->id])}}</td></tr></tbody>@endforeach

请注意,如果我在上面的视图 ($u->areas->name) 中指定区域关系中的列名,则显示错误:-

此集合实例上不存在属性 [名称].(查看:C:xampphtdocs

如何在视图文件中只显示(areas.name)列

解决方案

因为 areas 是一个集合,所以你应该像这样循环:

@foreach($users as $u)<tr role="row" class="odd"><td class="sorting_1">{{$u->name}}</td><td></td><td>@foreach($u->areas as $area){{$area->name}},@endforeach</td><td>{{route('show_user', ['id' => $u->id])}}</td></tr></tbody>@endforeach

如果你想用逗号分隔它们,你可以这样做而无需循环:

 @foreach($users as $u)<tr role="row" class="odd"><td class="sorting_1">{{$u->name}}</td><td></td><td>{{ $u->areas->pluck('name')->implode(', ') }}</td><td>{{route('show_user', ['id' => $u->id])}}</td></tr></tbody>@endforeach

I have 3 tables in DB (users , areas , area_user),

Users table: id name username password

areas table: id name

area_user table: id user_id area_id

I am trying to create a (List users page) which will show all user table column with user assigned areas.

User.php Model file

  <?php

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;

class User extends Authenticatable{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password','role_id',];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token',];

    public function areas(){
        return $this->belongsToMany('AppArea','area_user');
    }

Area.php Model file:

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Area extends Model{
    protected $fillable = ['name'];

    public function users(){
        return $this->belongsToMany('AppUser','area_user','area_id','user_id');
    }
}

UserController@index file :

<?php

namespace AppHttpControllers;

use AppAreas;
use AppRoles;
use AppUser;
use IlluminateHttpRequest;

class UserController extends Controller{

    public function __construct(){
        // $this->middleware('auth');
    }

    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index(){
        $users = User::get();
        return view('users._list',
        ['users'=> $users]
    );
    }

Finally the table view file:-

@foreach($users as $u)
            <tr role="row" class="odd">
                <td class="sorting_1">{{$u->name}}</td>
                <td></td>
                <td>{{$u->areas]}}</td>
                <td>{{route('show_user', ['id' => $u->id])}}</td>
            </tr></tbody>
@endforeach

If i typed in user table view (assigned areas) with only using the areas property ($u->areas), it will show all the areas column:-

[
  {
    "id": 3,
    "name": "C",
    "location_id": 1,
    "created_at": null,
    "updated_at": null,
    "pivot": {
      "user_id": 4,
      "area_id": 3
    }
  },
  {
    "id": 4,
    "name": "D",
    "location_id": 2,
    "created_at": null,
    "updated_at": null,
    "pivot": {
      "user_id": 4,
      "area_id": 4
    }
  }
]


 @foreach($users as $u)
                <tr role="row" class="odd">
                    <td class="sorting_1">{{$u->name}}</td>
                    <td></td>
                    <td>{{$u->areas->name]}}</td>
                    <td>{{route('show_user', ['id' => $u->id])}}</td>
                </tr></tbody>
    @endforeach

Notice that if i specify the column name in Areas relationship in the above view ($u->areas->name) , error showing:-

Property [name] does not exist on this collection instance. (View: C:xampphtdocs

How can i show only the (areas.name) column in view file

解决方案

Because the areas is a collection so you should loop over it like this :

@foreach($users as $u)
    <tr role="row" class="odd">
        <td class="sorting_1">{{$u->name}}</td>
        <td></td>
        <td>
        @foreach($u->areas as $area)
            {{$area->name}}, 
        @endforeach
        </td>
        <td>{{route('show_user', ['id' => $u->id])}}</td>
    </tr></tbody>
@endforeach

And if you want to separate them whith comma for example you can do it like that without looping :

    @foreach($users as $u)
    <tr role="row" class="odd">
        <td class="sorting_1">{{$u->name}}</td>
        <td></td>
        <td>
            {{ $u->areas->pluck('name')->implode(', ') }}
        </td>
        <td>{{route('show_user', ['id' => $u->id])}}</td>
    </tr></tbody>
@endforeach

这篇关于此集合实例上不存在属性 [名称]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)