Reputation: 67
I am having difficulty in understanding how to pass data to a .blade
. I want to pass the users' username ($user)
to a dashboard component. Here's a few things I've tried:
return view('livewire.dashboard', ['user'=>'$user']);
return view('livewire.dashboard', compact('$user'));
return view('livewire.dashboard'->with('user',$user));
But then I realized that the class I was trying to pass the user info from didn't know what the user variable is. Here is the class (Dashboard.php):
<?php
namespace App\Http\Livewire;
use Illuminate\Contracts\View\View;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\Container\BindingResolutionException;
use Livewire\Component;
class Dashboard extends Component
{
/**
* @return View|Factory
* @throws BindingResolutionException
*/
public function render()
{
return view('livewire.dashboard');
}
}
And here is my web.php file:
use App\Http\Livewire\Dashboard;
use App\Http\Livewire\User;
// Dashboard
Route::get('/', Dashboard::class)->name('dashboard');
// I have also tried something like 'Route get user' here, also didn't work
// User routes
Route::prefix('/users')->group(function () {
Route::get('/', User\Index::class)->name('users.index');
Route::get('/create', User\Create::class)->name('users.create');
Route::get('/{user}', User\Edit::class)->name('users.edit');
});
I'm having difficulty understanding how components/classes/blade tamplates communicate with each other. How can I pass the username of the person currently logged in to my dashboard.blade.php file? It looks like this:
@isset($user)
Hallo {{ $user->name }},
@endisset
</div>
<div>Willkommen im Formular-Editor!</div>
<div>Neue Formulare kannst du über den Tab "Editor" oben in der Leiste anlegen. Hier kannst Du auch bestehende Formulare bearbeiten.</div>
An explanation as well as a solution would be appreciated, as I really don't understand how Laravel works. Thank you
Upvotes: 1
Views: 1720
Reputation: 38642
You can pass them
<livewire:show-post :user="$user">
public function render()
{
return view('livewire.dashboard', ['user' => auth()->user()]);
}
Read More Official LiveWire Doc
Laravel is a Well designed Framework. So passing the logged-in users here and there(fetch in the controller and pass to view with $user
), you can directly access using the auth()
helper.
In view
auth()->id();
auth()->user();
Upvotes: 1
Reputation:
In a more simpler way, you can use compact as you are using in second step. The only change you need to remove $ from user variable.
eg.
return view('livewire.dashboard', compact('user'));
Upvotes: 0
Reputation: 10210
As you are using Livewire, you probably want to take advantage of data binding (one of the benifits of Livewire).
What you can do is define a $user
property on your Dashboard
component that you can then access in the dashboard.blade.php
view.
Your Dashboard
component might look as follows (I've omitted code for brevity)
class Dashboard extends Component
{
public $user;
/*
* The mount() function is Livewires equivalent of __construct()
*/
public function mount()
{
$this->user = Auth::user();
}
public function render()
{
return view('livewire.dashboard');
}
}
Then in your view you would simply access $user
Hallo {{ $user->name }}
Upvotes: 2