对在laravel控制器中引用模型进行故障排除 [英] Troubleshooting referencing a model in a laravel controller

查看:536
本文介绍了对在laravel控制器中引用模型进行故障排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直没有尝试解决laravel 5.2应用程序(carfreak)中的错误.

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

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

我已将默认用户模型移至文件夹app/models,并进行了必要的更改,以使它们都能正常工作.

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

现在我有一个新的控制器CarController和一个新的模型CarModel都无法使用.命名空间似乎是一个简单的问题,但我无法解决.

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.

  • 该模型是否在models文件夹中?是的. carfreak\app\Models\CarModel.php
  • 控制器名称空间正确吗?是的... namespace carfreak\Http\Controllers;
  • 控制器是否参考模型?是的... use App\Models\CarModel;
  • 模型名称空间正确吗?是的... namespace carfreak\Models;
  • 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;

我可以通过玩CarController来创建错误的不同版本,但是我想不到的排列没有用.

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

添加了控制器和模型...

Controller and Model added...

添加了更多详细信息:

具有讽刺意味的是,我可以php artisan make:model sanityCheck,它将在\app根目录中创建一个模型(即不在app \ models中)...并且可以很好地调用该模型.如果我将CarController放回根目录,并适当地更改其名称空间,它仍然无法正常工作.几乎就像我在类名中有一些愚蠢的拼写错误,但是我已经将类名复制并粘贴到了文件名,类声明,"use"声明等中.仍然.没有.工作.啊!

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!

//这是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',
    ];

}

//这是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.

让我有不同想法的帖子是曼苏尔·阿赫塔尔(Mansoor Akhtar)的建议:

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. 让工匠第一次在正确的位置制作模型. php artisan make:model Models/CarModel

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

在Controller中,正确引用模型 use name-of-app\Models\CarModel;

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

我的问题可能涉及也可能不涉及缓存刷新.每次更改之后,我最终都要重新启动XAMPP,以确保不涉及缓存.我也试过了 php artisan cache:clear

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

这篇关于对在laravel控制器中引用模型进行故障排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆