在laravel控制器中引用模型的故障排除

I've been trying unsuccessfully to resolve an error in a laravel 5.2 app (carfreak).

FatalErrorException in PropertyController.php line 85: Class 'App\Models\CarModel' not found

I have moved the default user model to a folder app/models and made the necessary changes so that it's all working fine.

Now I have a new controller CarController, and a new model, CarModel that are just not working. It seems to be such a simple problem with namespaces, but I am unable to resolve it.

  • is the model in the models folder? Yes. carfreak\app\Models\CarModel.php
  • is the controller namespace correct? Yes... namespace carfreak\Http\Controllers;
  • does the controller reference the model? Yes...use App\Models\CarModel;
  • is the model namespace correct? Yes... namespace carfreak\Models;

I am able to create different versions of the error by playing with the CarController but no permutation I can think of has worked.

EDIT: Controller and Model added...

EDIT: More details added:

The irony of this is that I can php artisan make:model sanityCheck and it will create a model in the \app root (i.e. not in app\models)... and that model can be called just fine. If I put my CarController back in the root, and change it's namespace appropriately, it still doesn't work. It's almost like I have some stupid spelling error in the class name or something, but I've copied and pasted class names into my filename, class declaration, "use" declarations, etc. and it. still. doesnt. work. Aargh!

//this is carModel

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CarModel extends Model
{
    //

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

}

//this is carController

<?php

namespace carfreak\Http\Controllers;

use Illuminate\Http\Request;
//use \carfreak\app\Models\CarModel;
use App\Models\CarModel;


class CarController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        $this->middleware('auth');
    }


    public function store(Request $request)
    {

       // validate the data
        $this->validate($request, array(
                'CarColour' => 'required|max:50'
            ));

        // store in the database
        $newCar = new CarModel;
dd($request);       


    }

}

Well, here it is. I solved it by asking myself this question: If I'm having so much trouble namespacing, referencing and organising my models, then maybe I can get artisan to do it for me.

The post that got me thinking differently was Mansoor Akhtar's advice here: How do I instruct artisan to save model to specific directory?

  1. Get artisan to make the model in the right place first time. php artisan make:model Models/CarModel

  2. In the Controller, reference the model correctly use name-of-app\Models\CarModel;

  3. There may or may not be cache flushing involved in my problem. I was eventially restarting my XAMPP after every change to ensure no caching was involved. I also tried php artisan cache:clear

This looks wrong use \carfreak\app\Models\CarModel; should be use App\Models\CarModel in this is carController

Casing is important on linux. The namespace generally is used in a PSR Aloader to find the file. And Linux filesystem is case sensitive. So the CareModel.php file should be located in App/Models/CarModel.php

But I never used Laravel...