m11ad's avatar
Level 1

Method App\Http\Controllers\NewsController::isDeferred does not exist.

I'm new in laravel. I appreciate your help guys. trying to build a json API for backend of a newsdashboard with laravel. made migrations. I created the routes. made models. made the controller. Now I'm getting this error whenever I want to serve:

Method App\Http\Controllers\NewsController::isDeferred does not exist.

Here is my code guys:

Here are my routes:


use Illuminate\Support\Facades\Route;
use App\NewsItem;
use App\Http\Controllers\NewsController;



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

Route::get('/news', 'App\Http\Controllers\NewsController@index')->name('news.index');
Route::get('/news/{newsItem}', 'App\Http\Controllers\NewsController@show')->name('news.show');
Route::post('/news', 'App\Http\Controllers\NewsController@store')->name('news.store');
Route::patch('/news/{newsItem}', 'App\Http\Controllers\NewsController@update')->name('news.update');
Route::delete('/news/{newsItem}', 'App\Http\Controllers\NewsController@destroy')->name('news.destroy');
Route::get('/news/create', 'App\Http\Controllers\NewsController@create')->name('news.create');
Route::get('/news/{newsItem}/edit', 'App\Http\Controllers\NewsController@edit')->name('news.edit');

Here is the controller that was mentioned in the routes:

<?php

namespace App\Http\Controllers;

use App\Category;
use App\NewsItem;
use App\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class NewsController extends Controller
{

    /**
     * Display a listing of the news items.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $newsItems = NewsItem::with('category', 'tags')->get();

        return response()->json($newsItems);
    }



// and the rest of the code which are rest of the CRUD functions... and some validations


}

Here is NewsItem mode:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class NewsItem extends Model
{


    // Specify the table name for the model
    protected $table = 'news_items';

    // Specify the fillable fields for the model
    protected $fillable = [
        'title',
        'body',
        'category_id',
        'url',
    ];

    // Define the relationships for the model
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }
}

And made two other similar models for Tag and Category too.

Before getting this error I got another error : Class "App\NewsItem" not found

and then I added my controller and model classes to the $providers array in the config/app.php file and now I'm getting this error in the title.

I tried: composer dump-autoload and I'm getting :

Class App\Category located in D:/news-dashboard/app\Models\Category.php does not comply with psr-4 autoloading standard. Skipping.
Class App\NewsItem located in D:/news-dashboard/app\Models\NewsItem.php does not comply with psr-4 autoloading standard. Skipping.
Class App\Tag located in D:/news-dashboard/app\Models\Tag.php does not comply with psr-4 autoloading standard. Skipping.
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

In Controller.php line 68:

  Method App\Http\Controllers\NewsController::isDeferred does not exist.


Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
0 likes
11 replies
Sinnbeck's avatar

Remove them from that array again. The error before was the correct one

Next make sure the filename of the class matches the class name exactly

1 like
m11ad's avatar
Level 1

@Sinnbeck filenameNewsItem.php and class name: NewsItem are the same. and yet again I'm getting this error, when I open the page:

Class "App\NewsItem" not found

m11ad's avatar
Level 1

@Sinnbeck D:\project\app\Models\NewsItem.php

and the controller D:\project\app\Http\Controllers\NewsController.php

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@m11ad the namespace is wrong then

Fix the model

namespace App\Models;

And the import in the controller

use App\Models\NewsItem;
1 like
Sinnbeck's avatar

Be aware that the same problem is there for your other models (I assume)

1 like
m11ad's avatar
Level 1

@Sinnbeck Yes, Thank you! that resolved it. I no longer get the error. But I suppose should correct the other models nonetheless.

Sinnbeck's avatar

@m11ad yeah the namespace and folder structure should always be the same (except app becomes App)

1 like
m11ad's avatar
Level 1

@Sinnbeck I think now my API is kind of working since the /news was showing me[]. But when I tried to test it with postman. the GET requests are 200 OK. but the POST at first returned 419 unknown status I googled it and I saw I should put my routes in the /api instead of /web so there wouldn't be a need for CSRF tokens. Now I'm getting this error: 405 Method Not Allowed

Sinnbeck's avatar

@m11ad please make a new thread with this problem as it does not seem to be related

1 like

Please or to participate in this conversation.