ONYX
ONYX

Reputation: 5859

call collection with other model to lists

How do I call the relational data in the statement below using a with statement.

$suppliers = Supplier::with('user')->lists('user.company', 'user.id'); // doesn't work

class Supplier extends Model
{
    protected $table = "suppliers";

    protected $fillable = ['email'];


    public function user() {
        return $this->belongsTo('App\User', 'email', 'email');
    }
}

Upvotes: 0

Views: 27

Answers (1)

Samuele Colombo
Samuele Colombo

Reputation: 685

You achieve your goal using the pluck method:

Supplier::with('user')->get()->pluck ('user.company', 'user.id');

The get method returns a Collection, then you can use its methods.

Upvotes: 2

Related Questions