only get selected value from has many ralation laravel

Solutions on MaxInterview for only get selected value from has many ralation laravel by the best coders in the world

showing results for - "only get selected value from has many ralation laravel"
Dylan
02 Apr 2019
1class User extends Model {
2  public function products(): hasMany {
3      return $this->hasMany('App\Product');
4  }
5}
6$user = User::findOrFail($user_id);
7/* user_id is must important is hasMany Relationship */
8$user_products = $user->products()->select('product_name', 'category',
9                                           'user_id')->get();