【发布时间】:2021-09-08 14:08:49
【问题描述】:
我的用户模型中有 hasMany 关系;
/**
* Get the posts for the users.
*/
public function posts()
{
return $this->hasMany(Posts::class); //foreign key assigned by user_id
}
我需要在 Eloquent 数据中获取一个外来 id
控制器;
use App\Models\User;
$posts = User::find(1)->posts;
foreach ($posts as $post) {
//
}
//for example
$foreign_key = $posts->foreign_key;
echo "all posts collection assigned foreign key is; ".$foreign_key;
预期输出;
1
如何获取外键?
【问题讨论】:
标签: php mysql laravel eloquent eloquent-relationship