ortiga Asked: 2020-02-25 08:25:13 +0800 CST 2020-02-25 08:25:13 +0800 CST 2020-02-25 08:25:13 +0800 CST How to create the relationship of two tables in Laravel Lumen? 772 I have two tables joined like so In my project Laravelwhat would be the relation that I would have to put in the model <?php namespace App; use Illuminate\Database\Eloquent\Model; class Archivo extends Model { protected $fillable = [ 'ruta', ]; } php 1 Answers Voted Best Answer Dev. Joel 2020-02-25T08:31:25+08:002020-02-25T08:31:25+08:00 The relations here are defined One to many (hasMany) and inversely belongsTo so that from the file we can obtain to which user it belongs. Model User public function archivos() { return $this->hasMany('App\Archivo'); } Model File public function usuario() { return $this->belongsTo('App\User'); } If you have problems with belongsTo (may not) you can specify your model's foreign key columns relative toUser public function usuario() { return $this->belongsTo('App\User','user_id'); } And when you want to get for example the files of a user it would be $usuario = User::find(1); $archivos = $usuario->archivos; dd($archivos); And if you want to get the User from a file $archivo = Archivo::find(1); dd($archivo->usuario);
The relations here are defined One to many (hasMany) and inversely belongsTo so that from the file we can obtain to which user it belongs.
Model User
Model File
If you have problems with
belongsTo
(may not) you can specify your model's foreign key columns relative toUser
And when you want to get for example the files of a user it would be
And if you want to get the User from a file