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
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();