Answer the question
In order to leave comments, you need to log in
How to link 4 tables?
Kind Everyone. Faced with a problem. I have tables, in the controller there is an Index method
public function index(Request $request, ApiResponse $response)
{
$limit = $request->get('limit');
if (!isset($limit)) $limit = 20;
$orders = Order::with('equipments')->paginate($limit);
if (count($orders) > 0) {
$response->setStatus('OK');
$response->setMessage('Данные успешно загружины');
$response->setData(['orders' => $orders]);
}
return $response->asJson();
}
class ApiResponse extends Response
{
public $body = [
'status' => 'ERROR',
'message' => 'Произошла ошибка',
];
public function setStatus($value) {
$this->body['status'] = $value;
}
public function setMessage($value) {
$this->body['message'] = $value;
}
public function setData($value) {
$this->body['data'] = $value;
}
public function asJson()
{
return response()->json($this->body);
}
}
"data": {
"orders": {
"data": [
"id": 1,
"title": "Order 1",
"equipments": [
{
"id": 1,
"title": 'Equipment 1'
"pivot": {
"wing_id": 1,
}
}
],
]
}
}
"data": {
"orders": {
"data": [
"id": 1,
"title": "Order 1",
"equipments": [
{
"id": 1,
"title": 'Equipment 1'
"pivot": {
"wing": {
"id": 1
"title": Right
}
public function equipments()
{
return $this->belongsToMany(Equipment::class, 'equipment_order')->withPivot([
'wing_id',
]);
}
Answer the question
In order to leave comments, you need to log in
Everything is described in the documentation: https://laravel.com/docs/8.x/eloquent-relationship...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question