0
votes

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

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:\xampp\htdocs

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

3

3 Answers

4
votes

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
3
votes

Since it's a many-to-many relationship, you can't just display areas property. You need to iterate over areas:

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

As all the answers mentioned, areas is a collection.

You can concatenate the names with <td>{{ $user->areas->pluck('name')->implode(' ') }}</td>