Jhorra
Jhorra

Reputation: 6321

Laravel - Calling model from controller looks for the wrong file name

I’m assuming this may be some convention I’m not understanding.

I have a model called Ingredient in the main App folder.

namespace App;

use Illuminate\Database\Eloquent\Model;

class Ingredient extends Model
{
    protected $table = "ingredients";
}

Then I have a controller trying to call that app

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Config;
use App\Ingredient;

class IngredientController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return response()
            ->json([
                'status' => 'ok',
                'ingredients' => Ingredient::all()
            ]);
    }
}

Everything seems to be ok. I get a 500 server error though:

include(C:\\Dev\\ScratchApi\\vendor\\composer\/..\/..\/app\/Ingredients.php): failed to open stream: No such file or directory

The file name of the model is Ingredient.php

Do I need to rename my file to Ingredients.php to meet a naming convention? Or why is this trying to call a file name different from the class name I’m telling it to look for?

Upvotes: 0

Views: 586

Answers (3)

Giang D.MAI
Giang D.MAI

Reputation: 244

Try to use this:

composer dump-autoload

Upvotes: 0

Le Khiem
Le Khiem

Reputation: 886

Add option "-o" when run dump autoload.

composer dump-autoload -o

Upvotes: 1

Sagar Gautam
Sagar Gautam

Reputation: 9369

I think it's because of autoload cache. So, Run following command on your project root directory,

composer dumpautoload

Hope it works.

Upvotes: 1

Related Questions