从控制器类 Laravel 访问模型方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35163797/
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
Accessing model methods from controller class Laravel
提问by Prakhar Singh
I am new to Laravel, I am trying things around while going through a tutorial. This is where I am facing an unexpected behaviour.
我是 Laravel 的新手,我在学习教程时正在尝试各种方法。这是我面临意外行为的地方。
I have a model tweet
and a controller named tweetsController
; when I call tweet::find()
or any similar method I found this:
我有一个模型tweet
和一个名为的控制器tweetsController
;当我打电话tweet::find()
或任何类似的方法时,我发现了这一点:
FatalErrorException in tweetsController.php line 13: Class 'App\Http\Controllers\tweet' not found
FatalErrorException in tweetsController.php line 13: Class 'App\Http\Controllers\tweet' not found
I have also tried App\tweet::find()
.
Everything seems fine though tinder.
我也试过了App\tweet::find()
。虽然火种,但一切似乎都很好。
Please explain.
请解释。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class tweetsController extends Controller
{
public function show(){
$data = tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
public function delete($id){
return "here we dele the tweet ".$id;
}
public function add(){
return "i add your tweet to database then show you all the tweets";
}
}
tweet.php
推文.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tweet extends Model
{
protected $fillable = array(
'tweetHead',
'tweetBody'
);
}
?>
回答by Elias Rodrigues
A few options the maybe generating this error:
可能会产生此错误的几个选项:
- The model/controller namespace is incorrect;
- The name of the file and the class name for the model needs to be "Tweet" with the first letter in uppercase;
- If you set the right namespace on the model "Tweet.php" and import that on your "TweetController.php"
- 模型/控制器命名空间不正确;
- 模型的文件名和类名需要是“Tweet”,首字母大写;
- 如果您在模型“Tweet.php”上设置了正确的命名空间并将其导入到您的“TweetController.php”中
I hope that helps :)
我希望有帮助:)
UPDATE:
更新:
In the TweetController.php add this
在 TweetController.php 中添加这个
use App\Tweet;
Before the class declaration, like this
在类声明之前,像这样
use App\Tweet;
class tweetsController extends Controller
{
And remember to change the controller name in the class declaration like this
并记得像这样更改类声明中的控制器名称
class TweetsController extends Controller
{
And the controller filename will become "TweetsController.php"
控制器文件名将变为“TweetsController.php”
The Model also has to be named "Tweet" and not "tweet" in the class declaration and the filename
模型还必须在类声明和文件名中命名为“Tweet”而不是“tweet”
class tweet extends Model
will become
会变成
class Tweet extends Model
and the file will be named "Tweet.php"
该文件将被命名为“Tweet.php”
and everytime you need to call the model you will do this
每次你需要调用模型时,你都会这样做
public function show(){
$data = App\Tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}