1

Hello,

I have a blade Layout called: profile.blade.php // component

And I have a blade file infos which extends from profile.blade.php component. On my controller, I have a profile method:

public method profile(User $user) {
   return view('infos.blade.php', compact('user'));
}

When I try to use the variable user to the profile.blade.php component, I have an error that says "undefined user variable"

My question is how can I get the data I received from my controller to Blade Component profle ? Because that part of component will be used many times.

Thanks.

Igor R.
  • 897
  • 5
  • 14
Artizan
  • 21
  • 1
  • 4

2 Answers2

2

in your app/view/components/profile-

  <?php

namespace App\View\Components;

use Illuminate\View\Component;

class profile extends Component
{

   public $user; //for consistency

   public function __construct($user)
   {
       $this->user = $user;
   }


   public function render()
   {
       return view('components.profile');
   }
 }

now the component can render a variable ($user), in your info's blade/infos.blade.php you can feed the variable to the component like --

<x-profile :user='$user' ></x-profile>

i understand the question was posted quite a long ago but i had the same issue and the answers posted here didn't work for me but this method did so.....it may help somebody.

Meraj
  • 31
  • 1
  • 8
0

As it's a component you have to pass user parameter each time you call it, for example

@component('profile', ['user' => $user]) @endcomponent // here $user comes from controller
Ruben Danielyan
  • 728
  • 4
  • 19