I
I
Ilya Loopashko2021-07-26 10:50:27
Laravel
Ilya Loopashko, 2021-07-26 10:50:27

How to access the binding table through an api request?

Good afternoon. Tell me how to implement, what to read, where to peep?

I have a table, Orders and Equipments , and a linking table is equipment_order.

There is an Order model , it has a many-to-many relationship with Equipments , a relationship by the keys ' equipment_id ', ' order_id '.

How can I implement a query when creating a new record in the Orders table , specifying in the query that it is necessary to take a record with Id = 1 from the Equipments table and bind it to the created record by creating a record in the equipment_order linking table.

This is what I would like to have:

api/works?work[time_start]=08:00:00&work[time_end]=20:00:00&work[volume]=2&work[unit_id]=2&equipment_id=1

WorkController

public function store(Request $request, ApiResponse $response)
    {
        $work = new Work;
        $work->fill($request->work)->save();
        $response->setStatus('OK');
        $response->setMessage('Запись создана');
        $response->setData(['order' => $work]);
        return $response->asJson();
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2021-07-26
@pLavrenov

1) The add request should be POST
2) It's a little prettier to use Work::create()
3) The first thing to do is to read all the documentation, it has good examples and it's not big. this will give a general understanding of what he can do and where to look.
Working with relationships

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question