问题描述
我在数据库中有3个表(用户,区域,区域用户),
I have 3 tables in DB (users , areas , area_user),
用户表:ID名称用户名密码
Users table:idnameusernamepassword
区域表:ID名称
area_user表:ID用户身份area_id
area_user table:iduser_idarea_id
我正在尝试创建一个(列出用户页面),该页面将显示具有用户分配区域的所有用户表列.
I am trying to create a (List users page) which will show all user table column with user assigned areas.
User.php模型文件
User.php Model file
<?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模型文件:
Area.php Model file:
<?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文件:
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]
);
}
最后是表格视图文件:-
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
如果仅使用area属性($ u-> areas)在用户表视图(分配的区域)中键入,它将显示所有area列:-
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
请注意,如果我在以上视图的区域"关系中指定列名($ u-> areas-> name),则会显示错误:-
Notice that if i specify the column name in Areas relationship in the above view ($u->areas->name) , error showing:-
属性[名称]在此集合实例上不存在.(查看:C:\ xampp \ htdocs
Property [name] does not exist on this collection instance. (View: C:\xampp\htdocs
我如何只显示视图文件中的(areas.name)列
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
这篇关于该集合实例上不存在属性[name]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!