Driver laravel moficar to search with more than one product

2

I have the following driver:

public function show($id)
       {
         // Busco  el perfil
         $perfil = lista::where('di', $id)->first();

        //con el resultado de la busqueda muestro el perfil
        $mostrarperfil = Perfiles::where('id', $perfil['di'])->get();

           return view('products.show', compact('mostrarperfil'));
       }

This driver works well for me, but what I need is to do this but with several profiles at once, to see if I explain. I want to change this:

$perfil = lista::where('di', $id)->first();

Because of this:

$perfil = lista::where('di', $id)->get();

This would give me several results but I do not know what changes I would have to make to this to be able to show all the profiles that I throw $ profile:

$mostrarperfil = Perfiles::where('id', $perfil['di'])->get();

I hope it is understood try to do a foreach but I do not get it besides I do not know if it is the correct way ..

I am trying to do it in the following way but it still shows me only one result ..

public function show($id)
       {
         // Busco  el perfil
         $perfil = lista::where('di', $id)->get();


       foreach($perfil as $perfi){
        //con el resultado de la busqueda muestro el perfil
        $mostrarperfil = Perfiles::where('id', $perfi->di)->get();
           }
           return view('products.show', compact('mostrarperfil'));
       }
    
asked by Lendy Rodriguez Silva 17.11.2018 в 16:19
source

1 answer

1

In your list model you must add the relationship you have with the profile, assuming that a list belongs to a profile (one to one):

public function perfil()
{
    return $this->belongsTo('App\Models\Perfil');
}

then in your query you must use with:

$perfil = lista::with('perfil')->where('di', $id)->get();

It will return an object that in its attributes will have one called profile that will be an array of objects in the profile table.

    
answered by 24.11.2018 / 02:44
source