get data from a pivot table

1

I am working with laravel 5.4 ... I have a question that may be quite simple to answer but I could not find the solution, I have a table state and another called organization to these two unites an intermediate table called state_organization which has the ids of each table plus a field called "note"

It turns out that from the controller I have the following.

$organizacion = Organizacion::findOrFail($id);
$detalle_organizacion = $organizacion->estados()->first()->pivot;

This returns only the first record in the organization that matches the id as follows.

{organizacion_id: 21, estado_id: 1, nota: "nota de serviline limitada prospecto.", created_at: "2018-10-13 00:30:00", updated_at: "2018-10-13 00:30:00"}

so far so good, but it turns out that in the pivot table I have more than one record for each organization as each organization goes through different states leaving notes of why the status change has been updated.

The question is how can I get all the records of an organization?

I have tried using the method all (), pluck (), get () and I have not obtained the result I'm looking for.

I already thank you for your answers.

    
asked by Rafa 13.10.2018 в 21:50
source

3 answers

2

You must use the relationships within Eloquent, in Laravel's documentation this topic is well explained

return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');

You need to use a HasMany relationship to enter your table and pivot table

    
answered by 14.10.2018 в 03:55
0

Thank you very much for responding, what @Victor Gonzalez tells me I had previously configured it in my model, anyway solve my problem with the following line of code

$organizacion->estados()->select('estado_id','organizacion_id','nota', 'fecha_creado', 'fecha_actualizado')->get();

This returns all the records that match the organization's id as follows.

  

0: {state_id: 1, organization_id: 21, note: "note for prospect", date_created: "2018-10-15 13:15:00", updated_date: "2018-10-15 13:15:00 ", ...}   1: {state_id: 2, organization_id: 21, note: "note for contacted", date_created: "2018-10-15 13:15:00", updated_date: "2018-10-15 13:25:00", ... }   2: {state_id: 2, organization_id: 21, note: "contact note number 2", date_created: "2018-10-16 12:00:00", updated_date: "2018-10-15 12:00:00" , ...}

I appreciate your time.

    
answered by 15.10.2018 в 18:35
-2

you have to use query builder, and make joins .. when passing the id of an organization you group all the states, example: organization makes the join to the pivot table and then a join to the notes table, then the grouped by the idOrganizacion and show you all the notes that organization has .. I hope it serves and clarify your doubts.

    
answered by 14.10.2018 в 02:37