📜  只从有很多关系 laravel 中获取选定的值 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:53:51.234000             🧑  作者: Mango

代码示例1
class User extends Model {
  public function products(): hasMany {
      return $this->hasMany('App\Product');
  }
}
$user = User::findOrFail($user_id);
/* user_id is must important is hasMany Relationship */
$user_products = $user->products()->select('product_name', 'category',
                                           'user_id')->get();