-1

Before in my controllers file there is no UserController.php then I made it manually and then after that I do this "php artisan make:controller UsersController" then it's already then when I start type the html on my index.blade.php it says error. Can someone help me with this?

This is the UserController.php

<?php

namespace App\Http\Controllers;

use App\Models\cr;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     * 
     */
    public function index()
    {
        return view('users.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(cr $cr)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(cr $cr)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, cr $cr)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(cr $cr)
    {
        //
    }
}

This is my navBar.balde.php

<a href="#" data-bs-toggle="modal" data-bs-target="#staticBackdrop" class="btn btn-outline rounded-pill"><i class="fa-sharp fa-solid fa-list"></i></a>
<a href="{{ route('users.index') }}" class="btn btn-outline rounded-pill"><i class="fa-sharp fa-solid fa-list"></i> Users</a>
<a href="#" class="btn btn-outline rounded-pill"><i class="fa-sharp fa-solid fa-list"></i> Menu</a>
<a href="#" class="btn btn-outline rounded-pill"><i class="fa-sharp fa-solid fa-list"></i> Cashier</a>
<a href="#" class="btn btn-outline rounded-pill"><i class="fa-sharp fa-solid fa-list"></i> Reports</a>
<a href="#" class="btn btn-outline rounded-pill"><i class="fa-sharp fa-solid fa-list"></i> Transactions</a>
<a href="#" class="btn btn-outline rounded-pill"><i class="fa-sharp fa-solid fa-list"></i> Reports</a>

<style>
    .btn-outline{
        border-color: #F2921D;
        color: #F2921D;
    }

    .btn-outline:hover {
        background: #F2921D
        color: #F2921D
    }
</style>

This is my web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('/orders', 'OrderController'); // orders.index
Route::resource('/products', 'ProductController'); // products.index
Route::resource('/users', 'UserController'); // userController
Route::resource('/companies', 'CompanyController'); // companies.index
Route::resource('/transactions', 'TransactionController'); // transactions.index


Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

This is my index.blade.php

@extends('layouts.app')

@section('content')
    <h4>Hello Users</h4>
@endsection

I already try use the method that told me to add "protected $namespace = 'App\Http\Controllers';" on RouteServiceProvider.php, and I try on web .php "use App\Http\Controllers\UserController;" but both of that still doesn't fix it. Can somebody help me with this cause I stuck on this problem for now.

sazen
  • 1
  • 1
  • 1
    Is this Laravel 7 or Laravel 8 and above.. if its laravel 7 .. U need to include the whole path for this to work ... and if its Laravel 8 and above you will need to import it at the top and then use it... – Anand Mar 25 '23 at 07:25
  • 2
    Try: `Route::resource('/users', \App\Http\Controllers\UserController::class);`. – Sachin Bahukhandi Mar 25 '23 at 07:36
  • you create an alias for `UserController` at the top of your file with the `use` statement but then in your route definition you don't use this reference ... `UserController::class` would give you the FQCN, but you are just passing a single string `UserController` ... there are no namespaces prefixed to route actions in Laravel by default so the error is correct as there is no controller named `UserController` .... duplicate: https://stackoverflow.com/questions/63807930/error-target-class-controller-does-not-exist-when-using-laravel-8/63808132#63808132 – lagbox Mar 25 '23 at 17:52

1 Answers1

0

Use like this:

Route::resource('/users', \App\Http\Controllers\UserController::class);
milwad
  • 12
  • 1