Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skylar01128's avatar

Selecting one field from an Eloquent Collection

I have a three way relationship between three different tables: users, permissions, and exercises. These tables converge into a single exercise_permission_user table that I use as the pivot between the three. With these relationships, each user can share their exercises with other users. All relationships and queries are working just fine. However, when I query for the exercise a user is sharing, I get all of the data including the exercise name, description, URL, and owner id when the only column that I want to display is the name column. For instance, when I query for the exercises that the first user is sharing I get:

[{"id":"3","name":"Test1","URL":"http://www.bing.com","user_id":"2"}]

[{"id":"4","name":"Test2","URL":"http://www.google.com","user_id":"1"}]

as a result in my foreach loop. However, I only want to display the name column (i.e "Test1" and "Test2"). Is there any way that I can only grab this one field?

The section of my controller that queries for the exercises

  public function viewshared() {
    $shared = \App\SharedExercises::where('user_id', 1)->get();
    return view('Delta.Shared')->with('shared', $shared);;
  }

My SharedExercises model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class SharedExercises extends Model
{
    protected $table = 'exercise_permission_user';
    protected $primaryKey = 'id';


    public function user() {
      return $this->hasMany('App\User', 'id', 'user_id');
    }

    public function permissions() {
      return $this->hasMany('App\Permission', 'id', 'permission_id');
    }

    public function exercises() {
      return $this->hasMany('App\Exercise', 'id', 'exercise_id');
    }

}

My User model

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

//Each user has many exercises in the normal exercise table while each exercises has only one user.
    public function exercises() {
        return $this->hasMany('App\Exercise');
    }
    public function shared() {
      return $this->belongsToMany('App\SharedExercises', 'exercise_permission_user');
    }

}

My Permission model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
  protected $fillable = array('Description');
  protected $table = 'permissions';
  protected $primaryKey = 'id';

public function shared() {
  return $this->belongsToMany('App\SharedExercises', 'exercise_permission_user');
}
}

My Exercise model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Exercise extends Model
{
    protected $fillable = array('Name');
    protected $table = 'exercises';
    protected $primaryKey = 'id';

  public function user() {
    return $this->belongsTo(User::class,'Owner');
  }

  public function shared() {
    return $this->belongsToMany('App\SharedExercises', 'exercise_permission_user');
  }

}

0 likes
2 replies
jhoff's avatar

Illuminate\Database\Eloquent\Builder@get optionally takes an array of fields that you'd like returned. By default, it'll return everything.

    public function get($columns = ['*'])
  public function viewshared() {
    $shared = \App\SharedExercises::where('user_id', 1)->get(['name']);
    return view('Delta.Shared')->with('shared', $shared);;
  }
skylar01128's avatar

When I attempt that after finding the exercises that a user has shared I get the following error:

array_key_exists(): The first argument should be either a string or an integer

This is in reference to my using the get() method ($shared->exercises->get(['name'])

Please or to participate in this conversation.