Model 如何在Laravel 4中获得与命名空间模型相关的模型字段?
好的,我有那些模型 app/models/article.phpModel 如何在Laravel 4中获得与命名空间模型相关的模型字段?,model,laravel,namespaces,laravel-4,relationship,Model,Laravel,Namespaces,Laravel 4,Relationship,好的,我有那些模型 app/models/article.php 如果在此之前您没有出现错误,那么它几乎可以正常工作,因此: $article = Article::first(); 事实上与 $article = \Models\Article::first(); 但是如果$article或->author不是对象,是因为articles表是空的,或者authors和articles之间没有关系集(在数据库中),那么问题不在于$article。事实上,如果我转储$article,我就会得到
如果在此之前您没有出现错误,那么它几乎可以正常工作,因此:
$article = Article::first();
事实上与
$article = \Models\Article::first();
但是如果$article
或->author
不是对象,是因为articles
表是空的,或者authors
和articles之间没有关系集(在数据库中),那么问题不在于$article。事实上,如果我转储$article,我就会得到该对象。但如果我转储$article->author,我会得到空值!所以问题是你救了我!我检查了,在articles表的author_id字段中有不存在的作者id。这是由于播种作业中的一种奇怪行为造成的。非常感谢。
<?php
namespace Admin;
use Models\Article;
class TestController extends BaseController {
public function test()
{
$article = Article::first();
sd($article->author->id);
}
}
$article = Article::first();
$article = \Models\Article::first();