此集合实例

时间:2017-10-20 09:32:06

标签: php laravel eloquent laravel-eloquent laravel-query-builder

我在DB中有3个表(users,areas,area_user),

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

区域表: ID 名称

area_user表: ID 用户身份 AREA_ID

我正在尝试创建一个(列出用户页面),它将显示具有用户指定区域的所有用户表列。

User.php模型文件

  <?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User 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('App\Area','area_user');
    }

Area.php模型文件:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

UserController @ index file:

<?php

namespace App\Http\Controllers;

use App\Areas;
use App\Roles;
use App\User;
use Illuminate\Http\Request;

class UserController extends Controller{

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

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

最后是表格视图文件: -

@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-&gt;区域)键入用户表格视图(指定区域),它将显示所有区域列: -

[
  {
    "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

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

此集合实例上不存在Property [name]。 (查看:C:\ xampp \ htdocs

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

3 个答案:

答案 0 :(得分:3)

因为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

答案 1 :(得分:2)

由于它是多对多关系,因此您不能只显示区域属性。你需要遍历区域:

@foreach($users as $u)
    @foreach ($u->areas as $area)
        {{ $area->name }}
    @endforeach
@endforeach

答案 2 :(得分:1)

正如提到的所有答案一样,areas是一个集合。

您可以使用<td>{{ $user->areas->pluck('name')->implode(' ') }}</td>

连接名称