duanfen2008 2016-07-30 10:11
浏览 84
已采纳

在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);       


    }

}
  • 写回答

2条回答 默认 最新

  • duanshan2988 2016-07-31 00:46
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog