简体   繁体   中英

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!

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('/');
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM