project VERSION 5.2試圖獲得非物件的財產[laravel 5.2]
我是一個新的Laravel 5學習者。 PLZ解決...
埃羅 R:試圖讓非對象的屬性
Comment.php [model]
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
//
public function articals()
{
return $this->belongsTo('App\Artical');
}
protected $fillable = array('body','artical_id');
}
Article.php [model]
namespace App;
use Illuminate\Database\Eloquent\Model;
class Artical extends Model
{
//
public function comments()
{
return $this->hasMany('App\Comment');
}
protected $fillable = array('title','body');
}
route.php [route]
use App\Artical;
use App\Comment;
Route::get('/', function()
{
$c = Artical::find(18)->comments;
foreach($c as $comment)
{
print_r($comment->body.'<br />');
}
}); // working ok.....but
$a = Comment::find(18)->articals;
print_r($a->title); // error:Trying to get property of non-object
}
getting error:Trying to get property of non-object
plz幫助我...
沒有說哪行的錯誤是嗎? – Derek
它是什麼文件告訴你,拋出錯誤? – Derek
這意味着'$ a'不是一個對象,這意味着這些文章可能是空的。 'Article :: comments()'應該是'hasMany(Comment :: class)'和'Comment :: articles()'應該是'Comment :: article()'。修復這種關係很可能意味着評論文章被返回,這意味着$ a-> title'會起作用 –