Home > Back-end >  Laravel: How do you pass data to a blade.php?
Laravel: How do you pass data to a blade.php?

Time:10-06

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

CodePudding user response:

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 }}

CodePudding user response:

You can pass them

<livewire:show-post :user="$user">

In Laravel Way

public function render()
{
    return view('livewire.dashboard', ['user' => auth()->user()]);
}
  

Read More Official LiveWire Doc


Apart from all

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

CodePudding user response:

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

CodePudding user response:

You are trying to route the index (/) route to your Livewire component, which is not how Livewire is meant to function. It is used to load a component on from another blade file, like how you would render a Vue component.

Your view should be rendered from a Controller.

You can read more that here: https://laravel.com/docs/9.x/routing

And I also recommend you reading on how to properly render livewire components

https://laravel-livewire.com/docs/2.x/rendering-components

  • Related