Ad

Laravel 7 Relationships

- 1 answer

Hi guys i’m new at using Laravel, now i’m working with Laravel 7.2.1.

I have three tables:

Table users: id, name, email.

Table shops: id, nome_negozio.

Table user_has_shops: shop_id, user_id.

I want to visualize the users and the related shops in which them are registered in.

I have already set the relations in the user model:

public function shops()
    {

        return $this->belongsToMany(Shop::class);
    }     

Instead in the model shop I put this:

public function users()
    {
        return $this->belongsToMany(User::class);
    }

I don’t understand how to use them properly, I have set my user controller:

public function index(Request $request)
{


    if ($request->ajax()) {
        $data = User::latest()
                  ->leftJoin('user_has_shops', 'user_has_shops.user_id', '=', 'users.id')
                  ->leftJoin('shops', 'shops.id', '=', 'user_has_shops.shop_id')
                  ->select('users.*', 'shops.nome_negozio')
                  ->get();

        return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('roles', function($row){

                    $btn = '<label class="badge bg-orange">Nessun Ruolo Assegnato</label>';                                        

                        foreach($row->getRoleNames() as $ruoli_assegnati){

                        $btn = '<label class="badge bg-teal">'.$row->getRoleNames()->implode(' , ').'</label>';


                        }

                    return $btn;
                })
                ->addColumn('shops', function($row){

                    //$btn = '<label class="badge bg-orange">Nessun Negozio Assegnato</label>'; 

                        $btn = '<label class="badge bg-teal">'.$row->nome_negozio
                        .'</label>';



                    return $btn;
                })
                ->addColumn('action', function($row){

                    $btn = '<div class="btn-group" role="group">
                                            <div class="btn-group" role="group">
                                                <button type="button" class="btn btn-danger waves-effect dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                    Azioni
                                                    <span class="caret"></span>
                                                </button>
                                                <ul class="dropdown-menu">                                                       
                                                    <li><a target="_blank" rel="nofollow noreferrer" href="#" class="show-modal" data-id="'.$row->id.'" data-nome="'.$row->name.'" data-email="'.$row->email.'"  data-ruoli="'.$row->getRoleNames()->implode(' , ').'">Visualizza</a></li> 
                                                    <li><a target="_blank" rel="nofollow noreferrer" href="'.route('users.edit',$row->id).'">Modifica</a></li>
                                                    <li><a target="_blank" rel="nofollow noreferrer" href="#" class="delete-modal" data-id="'.$row->id.'" data-title="'.$row->name.' - '.$row->email.'">Elimina</a></li>                                                           
                                                </ul>
                                            </div>
                                        </div>';    


                        return $btn;
                }) 
                ->rawColumns(['roles','shops','action'])
                ->make(true);
    }

    return view('users.index');
}

The result is this: enter image description here

As you can see ID 38 has two shops, but it is written in two different rows, instead of one. I want to better understand how to use this kind of relations.

Thank you for your help.

Ad

Answer

First of all, try to use laravel naming convention. Your pivot table name should be user_shop.

Then you can get user's shops like this:

$user->shops
Ad
source: stackoverflow.com
Ad