• <bdo id='1Yii7'></bdo><ul id='1Yii7'></ul>

      <legend id='1Yii7'><style id='1Yii7'><dir id='1Yii7'><q id='1Yii7'></q></dir></style></legend>
      <i id='1Yii7'><tr id='1Yii7'><dt id='1Yii7'><q id='1Yii7'><span id='1Yii7'><b id='1Yii7'><form id='1Yii7'><ins id='1Yii7'></ins><ul id='1Yii7'></ul><sub id='1Yii7'></sub></form><legend id='1Yii7'></legend><bdo id='1Yii7'><pre id='1Yii7'><center id='1Yii7'></center></pre></bdo></b><th id='1Yii7'></th></span></q></dt></tr></i><div id='1Yii7'><tfoot id='1Yii7'></tfoot><dl id='1Yii7'><fieldset id='1Yii7'></fieldset></dl></div>

    1. <small id='1Yii7'></small><noframes id='1Yii7'>

    2. <tfoot id='1Yii7'></tfoot>
    3. 此集合实例上不存在属性 [名称]

      时间:2023-09-22

        <small id='YbqnQ'></small><noframes id='YbqnQ'>

            <i id='YbqnQ'><tr id='YbqnQ'><dt id='YbqnQ'><q id='YbqnQ'><span id='YbqnQ'><b id='YbqnQ'><form id='YbqnQ'><ins id='YbqnQ'></ins><ul id='YbqnQ'></ul><sub id='YbqnQ'></sub></form><legend id='YbqnQ'></legend><bdo id='YbqnQ'><pre id='YbqnQ'><center id='YbqnQ'></center></pre></bdo></b><th id='YbqnQ'></th></span></q></dt></tr></i><div id='YbqnQ'><tfoot id='YbqnQ'></tfoot><dl id='YbqnQ'><fieldset id='YbqnQ'></fieldset></dl></div>
                <bdo id='YbqnQ'></bdo><ul id='YbqnQ'></ul>

              • <tfoot id='YbqnQ'></tfoot>
                <legend id='YbqnQ'><style id='YbqnQ'><dir id='YbqnQ'><q id='YbqnQ'></q></dir></style></legend>
                  <tbody id='YbqnQ'></tbody>

              • 本文介绍了此集合实例上不存在属性 [名称]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我在数据库中有 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
                

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

                上一篇:当同一模型也存在 HasMany 关系时,如何更新 Has 下一篇:如何在 Laravel Eloquent 中计算价值?

                相关文章

                最新文章

                <legend id='4pv63'><style id='4pv63'><dir id='4pv63'><q id='4pv63'></q></dir></style></legend>
                    • <bdo id='4pv63'></bdo><ul id='4pv63'></ul>
                  1. <i id='4pv63'><tr id='4pv63'><dt id='4pv63'><q id='4pv63'><span id='4pv63'><b id='4pv63'><form id='4pv63'><ins id='4pv63'></ins><ul id='4pv63'></ul><sub id='4pv63'></sub></form><legend id='4pv63'></legend><bdo id='4pv63'><pre id='4pv63'><center id='4pv63'></center></pre></bdo></b><th id='4pv63'></th></span></q></dt></tr></i><div id='4pv63'><tfoot id='4pv63'></tfoot><dl id='4pv63'><fieldset id='4pv63'></fieldset></dl></div>

                    <small id='4pv63'></small><noframes id='4pv63'>

                    <tfoot id='4pv63'></tfoot>