What's new on Laravel 5.5?
This is a new release. whith a lot to give us.
For this case, we are going to talk about some new features of Laravel 5.5, who is a real mounster on the world because it has given a huge solution for simple problems which include a lot code.
Of course we can't talk about everything in one oportunity so, we will dedicate some post to specify one by one of new features.
So, for this oportunity, we will talk about, pivots.
Pivot is that way on which i could get information from a many to many query of database
In Laravel 5.4, Taylor added the ability to define a $casts property on your custom pivot models as well, but it only applied those $casts when reading the data—no conversion was performed when inserting or updating an attribute.
For example, let’s say you have a Runner model and a Race model. One runner can have many races, and one race can have many runners. Let’s call our pivot model a RaceRunner, which will include a splits array with a varying number of individual lap times (depending on the length of the race, and stored in seconds) in addition to the required runner_id and race_id.
The splits array is serialized as JSON in the race_runner table, so if you defined splits as an array in the $casts of your RaceRunner pivot model, then the following will dump an array:
dd( $runner->pivot->splits );
// Example:
[
'Lap 1' => 150,
'Lap 2' => 163,
'Lap 3' => 146
]
But when creating or updating the pivot model, you still have to cast it manually:
// Cast first...
$splits = $splits->toJson();
// ...then attach:
$runner->races()->attach($raceId, ['splits' => $splits]);
Now with Laravel 5.5, the $casts property on the Eloquent\Model and Eloquent\Relations\Pivot classes will behave the same. Laravel will “respect” your casts on both, whether you’re reading, inserting or updating data. This will make the attach, sync and save methods available to pivot models. Applying this new feature to our example above, the // Cast first... code is no longer necessary.