display list of dependents per user in Laravel 6

Asked

Viewed 60 times

-2

I have my main "home" page with route that is displayed after login.

So this one "home.blade.php" displays a page with the logged in user data.

I created a "dependent" table, I made the relationship between this table and the "users" table, it’s a relationship Onetomany, where a user has "N" dependents.

When logging in I wanted to display a list of dependents and I must be doing wrong.

I have the following function in my controller Homecontroler :

class HomeController extends Controller
{
// OUTRAS FUNÇÕES OMITIDAS

 public function home(AQUI COMO COLOCO ID DO USUARIO LOGADO?)
{
    $user = User::where('id',$id)-> first();
    $dependentes = $user->dependentes()->get();
    return view('auth.home');
}

If the above function is correct, how do I put it on the Home page to list dependents? Would that be?

<div class="pmo-block pmo-items hidden-xs">
         <h2>Dependentes</h2>
         <div class="pmob-body">
   @foreach ($dependentes as $dependente)
             <div class="row">
            <a href="" class="col-xs-2">
            <img class="img-circle" src="/bs3/img/demo/profile-pics/1.jpg" alt="">
            </a>    
           </div>
   @endforeach
        </div>
    </div>

1 answer

0


Just make it work:

in your home method:

 public function home()
    {
       $dependentes =  Auth::user()->dependentes()->get();
       return view('auth.home',compact('dependentes')); // essa linha vai pra View que 
       //você quer que é a HOME e ainda passa a variáver $dependentes. Faça um Foreach e 
       //seja feliz
    }
  • Thank you very much, it worked perfectly. Gratitude

Browser other questions tagged

You are not signed in. Login or sign up in order to post.