Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class 'Task' not found in basic Laravel 5.4 tutorial

First of all, I'm a total Laravel noob but I want to learn it. I started with the tutorial at https://laravel.com/docs/5.2/quickstart but have 5.4 installed. That is where it goes wrong, since the location of the routes is different compared to version 5.2 of Laravel, that the tutorial is based upon. So at my root folder, I have /routes and added the tutorial code in /routes/web.php:

<?php
/*
|--------------------------------------------------------------------------
| 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!
|
*/

/**
 * Show Task Dashboard
 */

Route::get('/', function () {
    $tasks = Task::orderBy('created_at', 'asc')->get();
    return view('tasks', [
        'tasks' => $tasks
    ]);
});

/**
 * Add New Task
 */
Route::post('/task', function (Request $request) {
    $validator = Validator::make($request->all(), [
        'name' => 'required|max:255',
    ]);

    if ($validator->fails()) {
        return redirect('/')
            ->withInput()
            ->withErrors($validator);
    }

    $task = new Task;
    $task->name = $request->name;
    $task->save();

    return redirect('/');
});

/**
 * Delete Task
 */
Route::delete('/task/{task}', function (Task $task) {
    $task->delete();

    return redirect('/');
});

?>

I have made an app/Task.php, which contains the (empty) Task class and my database is setup correctly as far as I can tell.

FatalErrorException in web.php line 21:
Class 'Task' not found

Still, I'm getting the above error, suggesting there is something wrong with my namespacing, but I just can't seem to get it right.

Btw, in order to get the installation working, I've renamed server.php in my root folder tot index.php and copied the .htaccess from /public to my root folder.

Any help would be appreciated!

like image 487
Ewinnn Avatar asked Apr 25 '26 13:04

Ewinnn


1 Answers

Use correct namespaces

   /**
    * Show Task Dashboard
    */

    Route::get('/', function () {
        $tasks = \App\Task::orderBy('created_at', 'asc')->get();
        return view('tasks', [
            'tasks' => $tasks
        ]);
    });

    /**
     * Add New Task
     */
    Route::post('/task', function (Request $request) {
        $validator = Validator::make($request->all(), [
            'name' => 'required|max:255',
        ]);

        if ($validator->fails()) {
            return redirect('/')
                ->withInput()
                ->withErrors($validator);
        }

        $task = new \App\Task;
        $task->name = $request->name;
        $task->save();

        return redirect('/');
    });

    /**
     * Delete Task
     */
    Route::delete('/task/{task}', function (\App\Task $task) {
        $task->delete();

        return redirect('/');
    });
like image 54
Odin Thunder Avatar answered Apr 27 '26 09:04

Odin Thunder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!