laravel 找不到类“App\Http\Controllers\Model”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46262172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:39:54  来源:igfitidea点击:

Class 'App\Http\Controllers\Model' not found

phplaravellaravel-5namespaceslaravel-5.5

提问by Ostap B.

I want to use Model functions in view

我想在视图中使用模型函数

My controller function code:

我的控制器功能代码:

 $model = Model::find(1);
 View::make('view')->withModel($model);

 return view('index.search', ['tickets' => $result]);

My model code:

我的型号代码:

<?php

namespace App;
namespace App\models;
use Illuminate\Database\Eloquent\Model;

class Tickets extends Model
{
    public function someFunction() {
        echo 'hello world!';
    }
}

My view code:

我的查看代码:

{{ $model->someFunction() }}

回答by Marcin Nabia?ek

You need to import your model like this:

您需要像这样导入模型:

use App\Tickets;

right after line with namespace so it should look something like this:

紧跟在命名空间之后,所以它应该是这样的:

<?php

namespace App\Http\Controllers;

use App\Tickets;

回答by Rwd

To get this to work you will either have to use the full namespace:

要使其正常工作,您必须使用完整的命名空间:

$model = \App\Tickets::find(1);


Or add a usestatement to the top of the controller:

或者use在控制器顶部添加一条语句:

use App\Tickets;

and load the model with:

并加载模型:

$model = Tickets::find(1);

回答by Ahsan

Your model should be

你的模型应该是

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Tickets extends Model
{
    public function someFunction() {
        echo 'hello world!';
    }
}

And controller function should be

控制器功能应该是

$model = Tickets::find(1);
 View::make('view')->withModel($model);

 return view('index.search', ['tickets' => $result]);