I got 2 models. File model:
class File extends Model
{
public function user()
{
return $this->belongsTo('App\m_user', 'CODUSER');
}
}
And m_user model:
class m_user extends Model
{
protected $table = 'm_users';
public function file()
{
return $this->hasMany('App\File', 'id_user');
}
}
The tables are the follow:
Files:
id, id_user, name
m_user:
CODUSER, email
I want to get the files with each user. I try with this code and dont return the values of m_user, returns only the values of file:
$file= File::with(['user'])->first();
print_r($file);
The result is:
(...)
[original:protected] => Array
(
[id] => 79
[id_user] => 1
[name] => Sublime Text Build 3211 x64 Setup.exe
)
(...)
[relations:protected] => Array
(
[user] =>
)
The relations are in blank! I need to get the user data in the relations.
UPDATE: i fix the problem using hasOne instead of belongsTo in the File model.
$file->user->email(note: you'll get an error if there's nom_userassociated with file.) Also, please name your model correctly;MUser, notm_user, andApp\fileisApp\File- Tim Lewis