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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助