Attribute [livewire] does not exist

Viewed 10921

I am having trouble to run my code on Laravel 8 routing with laravel-livewire.
The class is within Livewire\LandingPage.

The error I'm getting is

Attribute [livewire] does not exist

Here are my routes

<?php

use Illuminate\Support\Facades\Route;

Route::livewire('/' , 'LandingPage');

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');
3 Answers

If you are using a recent install of Laravel 8, you will have Livewire V2. In this version, Route::livewire()has been removed. Instead, you specify a normal get() route, with the action being the Livewire component class.

Route::get('/' , App\Http\Livewire\LandingPage::class);

If you use livewire v1.x please use this annotation :

//(livewire v1.x)
Route::livewire('/post', 'LandingPage');

If you are using livewire v2.0 please use this one :

//(livewire v2.x)
Route::get('/post', \App\Http\Livewire\LandingPage::class);

With Laravel 8.29 and LiveWire 2.4.0

UnexpectedValueException Invalid route action: [App\Http\Controllers\App\Http\Livewire\Blog].

I think that is better tha you need create a new Controller in App\Http\Controllers and link the route with this Controller. In the view use @liveware to your LiveWire Controller.

Route::group(['middleware' => 'auth'], function () {
    // Only with LiveWire v1
    //Route::livewire('/blog', 'blog')->section('blog');

    // For LiveWire 2.
    Route::get('/blog' , 'BlogController@index');
});

App\Http\Controllers\BlogController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BlogController extends Controller
{
    public function index(){
        return view('blog.index');
    }
}

resources/views/blog/index.blade.php

@livewire('blog')

Note: With the fix (https://laravel-livewire.com/docs/2.x/upgrading)

protected function mapWebRoutes()
{
    Route::middleware('web')
        ->namespace($this->namespace) // Remove me
        ->group(base_path('routes/web.php'));
}

you will have problems with routes in Middlewares

Illuminate\Contracts\Container\BindingResolutionException Target class [Auth\LoginController] does not exist.

Related