php 如何在 Laravel 5 中调用模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28937450/
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
How do I call a model in Laravel 5?
提问by Avi
I'm trying to get the hang of Laravel 5 and have a question which is probably very simple to solve but I'm struggling.
我正在尝试掌握 Laravel 5 的窍门,并有一个问题可能很容易解决,但我很挣扎。
I have a controller named TestController and it resides in \app\Http\Controllers
我有一个名为 TestController 的控制器,它位于 \app\Http\Controllers
This is the controller:
这是控制器:
<?php
namespace App\Http\Controllers;
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Then I have a model I created that resides in /app:
然后我创建了一个位于 /app 中的模型:
<?php
namespace App;
class diamonds extends Model {
}
Put all other mistakes aside which I'm sure there are, my problem is that laravel throws an error:
把我确定存在的所有其他错误放在一边,我的问题是 laravel 会抛出一个错误:
FatalErrorException in TestController.php line 10: Class 'App\Http\Controllers\diamonds' not found
TestController.php 第 10 行中的 FatalErrorException: Class 'App\Http\Controllers\diamonds' not found
So, how do I get the controller to understand I'm pointing to a model and not to a controller?
那么,如何让控制器理解我指向的是模型而不是控制器?
Thanks in advance...
提前致谢...
采纳答案by wiesson
You have to import your model in the Controller by using namespaces.
您必须使用命名空间将模型导入到控制器中。
E.g.
例如
use App\Customer;
class DashboardController extends Controller {
public function index() {
$customers = Customer::all();
return view('my/customer/template')->with('customers', $customers);
}
}
In your case, you could use the model directly App\diamonds::find(1);
or import it first with use App\diamonds;
and use it like you already did.
在您的情况下,您可以直接使用该模型,也可以App\diamonds::find(1);
先将其导入use App\diamonds;
并像之前一样使用它。
Further, it is recommended to use UpperCamelCase class names. So Diamonds instead of diamonds. You also could use dd()
(dump and die) instead of var_dump to see a nicely formatted dump of your variable.
此外,建议使用 UpperCamelCase 类名。所以钻石而不是钻石。您也可以使用dd()
(dump and die) 而不是 var_dump 来查看您的变量的格式良好的转储。
回答by Raheel
//Your model file
<?php
namespace App\Models;
class diamonds extends Model {
}
// And in your controller
<?php
namespace App\Http\Controllers;
use App\Models\
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
回答by danbahrami
Try adding the following lines above your class decleration in your controller file...
尝试在控制器文件中的类声明上方添加以下行...
use App\Diamonds;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
(this assumes your model file is called Diamonds.php
)
(这假设您的模型文件被称为Diamonds.php
)
回答by Avi
Ultimately, there were a few issues here and all of you helped, however I was missing the following on the model page:
最终,这里出现了一些问题,你们所有人都提供了帮助,但是我在模型页面上遗漏了以下内容:
use Illuminate\Database\Eloquent\Model;