Eager loading: http://laravel.com/docs/master/eloquent-relationships#eager-loading
$answers = Answer::with(['user'])->get();
dd($answers->toArray());
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am writing a small app which allows a teacher to assign homework to students and then a student able to answer the question / homework as many times as they like.
I am having trouble getting data with related objects.
For example I want to get all answers from the Answer model and each answer to pull in the user info.
My user and Answer models look as such:
User model:
namespace App\models;
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 {
public function UsersHomework(){
return $this->belongsTo('models/UsersHomework');
}
public function Answer(){
return $this->hasMany('models/Answer');
}
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'];
}
Answer model:
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Answer extends Model {
//
public function User(){
return $this->belongsTo('models/User');
}
public function Homework(){
return $this->belongsTo('Homework');
}
}
Then in my controller I am just calling:
$answers = Answer::all()->toArray();
which is returning:
Array
(
[0] => Array
(
[id] => 1
[answer] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into
[user_id] => 5
[homework_id] => 1
[created_at] => 2015-08-24 14:46:29
[updated_at] => 2015-08-23 02:12:00
[deleted_at] => 0000-00-00 00:00:00
)
[1] => Array
(
[id] => 2
[answer] => electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
[user_id] => 5
[homework_id] => 2
[created_at] => 2015-08-24 14:46:30
[updated_at] => 2015-08-23 02:12:19
[deleted_at] => 0000-00-00 00:00:00
)
[2] => Array
(
[id] => 3
[answer] => Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock
[user_id] => 5
[homework_id] => 3
[created_at] => 2015-08-24 14:46:32
[updated_at] => 2015-08-23 02:13:30
[deleted_at] => 0000-00-00 00:00:00
)
[3] => Array
(
[id] => 4
[answer] => answer here
[user_id] => 5
[homework_id] => 1
[created_at] => 2015-08-24 14:46:33
[updated_at] => 2015-08-23 05:51:16
[deleted_at] => 0000-00-00 00:00:00
)
)
Are my relationships built correctly? If so, how can I get all user data associated?
Eager loading: http://laravel.com/docs/master/eloquent-relationships#eager-loading
$answers = Answer::with(['user'])->get();
dd($answers->toArray());
Please or to participate in this conversation.