如何从关系中获取所有记录?我的意思不仅是相关记录,还有其他。
假设我有一个帖子属于某个类别。如果要更改此帖子的类别,我需要所有可用类别的列表。我可以从亲戚那里得到这个清单吗?
发布模型:
class Post extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
}
类别模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function posts()
{
return $this->hasMany('App\Post');
}
}
在 PostsController 中,我尝试过:
$postModel->category->find('all'); // Returns null
$postModel->category->all(); // Returns only the related categories
我知道我可以在PostsController中简单地使用Category模型,但是我更喜欢使用关系。