Home > Mobile >  Laravel resource class not working for view() command
Laravel resource class not working for view() command

Time:04-05

I am using laravel 9. All my controllers are working. but I created a resource controller named UserController. this to my web file

use App\Http\Controllers\UserController;

I introduced it. next;

Route::resource('/users', UserController::class);

I introduced it in my route. then in the index method of my class

public function index()
{
    return 'index';
}

When I called it as , it worked fine and printed "index" to the screen. However

public function index()
{
   return view("users");
}

this way, when i try i get error.

enter image description here

Normally if I call my users.blade.php file with a controller without the resource it works. for example

Route::get('users2', function () {
   return view('users');
})->name('users');

it works!!! and for example

Route::get('users', [UserController::class,'index']);

it works!!! . But when I do it using resource I can't call view.

oppss I remember put my user.blade.php code

<x-app-layout>
    <x-slot name="header">
            MY TITLE      
    </x-slot>
    <x-slot name="pageHeader">
        page Name
    </x-slot>
    <x-slot name="links">
        <link href="{{asset('back/') }}/vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
    </x-slot>
    <x-slot name="scripts">
        <!-- Page level plugins -->
        <script src="{{asset('back/')}}/vendor/datatables/jquery.dataTables.min.js"></script>
        <script src="{{asset('back/')}}/vendor/datatables/dataTables.bootstrap4.min.js"></script>

        <!-- Page level custom scripts -->
        <script src="{{asset('back/')}}/js/demo/datatables-demo.js"></script>
    </x-slot>

    <!-- DataTales Example -->
    <div >
        
        <div >
            <div >
                <table  id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Adı</th>
                            <th>E-posta</th>
                            <th>Yetkisi</th>
                            <th>Kayıt Tarihi</th>                            
                            <th>İşlemler</th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>                            
                            <th></th>
                        </tr>
                    </tfoot>
                    <tbody>
                        <tr>
                            <td>Tiger Nixon</td>
                            <td>System Architect</td>
                            <td>Edinburgh</td>
                            <td>61</td>                            
                            <td>$320,800</td>
                        </tr>
                        
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    
</x-app-layout>

have an idea?

CodePudding user response:

Ok. Found it. because if I use resources, I can't name the route. so the places where I call the route by name are popping up in my code. When I put the previous definitions, the href links do not fail because I can name the route. resources automatically get a name. and we can call it for example.

this is an error

 <a  href="{{route('users')}}">users</a>

This is correct

<a  href="{{route('users.index')}}">users</a>

CodePudding user response:

add name to your route ex:

Route::resource('/users', UserController::class)->name('users');

i hope it was useful

  • Related