我正在尝试掌握 Laravel 5 的窍门,并且有一个问题可能很容易解决,但我正在苦苦挣扎。
我有一个名为 TestController 的 Controller ,它位于\app\Http\Controllers 中
这是 Controller :
<?php
namespace App\Http\Controllers;
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
然后我创建了一个驻留在/app 中的模型:
<?php
namespace App;
class diamonds extends Model {
}
抛开我确定存在的所有其他错误,我的问题是 laravel 抛出错误:
FatalErrorException in TestController.php line 10: Class 'App\Http\Controllers\diamonds' not found
那么,如何让 Controller 明白我指向的是模型而不是 Controller ?
提前致谢...
最佳答案
您必须使用命名空间将您的模型导入 Controller 。
例如
use App\Customer;
class DashboardController extends Controller {
public function index() {
$customers = Customer::all();
return view('my/customer/template')->with('customers', $customers);
}
}
在您的情况下,您可以直接使用模型 App\diamonds::find(1);
或先使用 use App\diamonds;
导入它并使用它就像你已经做过的那样。
此外,建议使用 UpperCamelCase 类名。所以钻石而不是钻石。您还可以使用 dd()
(转储和死亡)而不是 var_dump 来查看格式良好的变量转储。
关于php - 如何在 Laravel 5 中调用模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28937450/