У меня есть 3 модели:Отображение пользователей с 2 связанными таблицами записей
User, Role and Branch.
Они связаны, как это:
Налейте модель Пользователь:
class User extends Model
{
// Define fields to be fillable
protected $fillable = [
'firstname',
'lastname',
'telephone',
'address',
'password',
'code',
'status',
'branches_id',
'roles_id'
];
/**
* Get the role record associated with the user.
*/
public function role()
{
return $this->hasOne('App\Role');
}
/**
* Get the branch record associated with the user.
*/
public function branch()
{
return $this->hasOne('App\Branch');
}
}
Для Role Model:
class Role extends Model
{
// Define fields to be fillable
protected $fillable = ['description'];
/**
* Get the user that owns the role.
*/
public function user()
{
return $this->belongsToMany('App\User');
}
}
Для филиала модели:
class Branch extends Model
{
// Define fields to be fillable
protected $fillable = ['description', 'location'];
/**
* Get the user that owns the branch.
*/
public function user()
{
return $this->belongsToMany('App\User');
}
}
Я знаю, что если я использовал B lade, чтобы перечислять роли пользователя, я мог бы сделать что-то вроде: $ user-> role()
Но я пытаюсь использовать угловой 2 для интерфейса и laravel 5.3 для моего backend.
Мой вопрос заключается в том, чтобы получить пользователей наряду с ролями и ветвей
Вот мой указательный действие в моем UserController:
public function index()
{
// Get all users, along with roles and branches
$users = User::all();
// Send the response
return response()->json([
'users' => $users
], 200);
}
Спасибо Алексей, Проблема решена, – Beni
Это может помочь другим, я создал другие модели 'Transfer', 'status'. «передача» связана с «статусом» и «пользователем». Я хотел отобразить все переводы со всеми его отношениями и всеми отношениями с пользователями, я просто сделал: $ transfer = Transfer :: with ('status', 'user', 'user.branch', 'user.role') -> get() – Beni