chenas
chenas

Reputation: 5

User not found why?

there I have followed a Laravel tutorial to route model bindings. But I have stumbled across this one error why is that so I here have the code. Please find and list me here a fix that works. The video can be found on Udemy job finder , route model binding episode 11.

TaskController.php

    <?php
    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\User;

    class TaskController extends Controller {
        public function index(){
            return User::all();
        }
    }

User.php

    <?php
    namespace App\Models;

    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;

    class User extends Authenticatable {
        use HasFactory, Notifiable;

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'name', 'email', 'password',
        ];

        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];

        /**
         * The attributes that should be cast to native types.
         *
         * @var array
         */
        protected $casts = [
            'email_verified_at' => 'datetime',
        ];
    }

web.php

    <?php
    use Illuminate\Support\Facades\Route;
    use App\User;
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */

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

    Auth::routes();

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

    Route::get('/user', [App\Http\Controllers\TaskController::class, 'index']);

Upvotes: 1

Views: 61

Answers (2)

Hedayatullah Sarwary
Hedayatullah Sarwary

Reputation: 2834

In Laravel-8 the User.php file is moved from the app folder to the app/Models, so when you want to call the user model call like this use App\Models\User; not like this use App\User;

Upvotes: 1

Ankita Dobariya
Ankita Dobariya

Reputation: 831

Just check the namespace of your Model,

Replace use App\User; to use App\Models\User;

Upvotes: 3

Related Questions