Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Pineco.de:
Easy Role Management with Pivot Models
Feb 13, 2018 @ 16:41:32

On the Pinco.de blog there's a post that covers the use of pivot models for role management in SaaS applications. The tutorial shows how to use the Laravel Eloquent "withPivot" method and a pivot database table to create an easy to use relationship between "users" and "teams" in a PHP application.

If you have ever developed any kind of SaaS app (like Spark), you know what team level role management means. Instead of picking a bad strategy for handling roles, we can bring simple solution by using Pivot Models.

The tutorial starts by talking about "robust permission handling" and why, more often than not, a simpler role-based approach is enough. It then covers the pivot table itself, showing the code to create the table. Next comes the creation of the relationship between teams and users via the withPivot method. With the relationship in place, the article then covers attaching users to a team, generating the models and how to add permission evaluation into the model's functionality.

tagged: role management pivot model tutorial laravel eloquent

Link: https://pineco.de/easy-role-management-pivot-models/

Jeff Madsen:
Custom Pivot Table Models, or Choosing the Right Technique in Laravel
Sep 11, 2017 @ 16:17:09

Jeff Madsen has some helpful hints for the Laravel (or just Eloquent) developers out there when it comes to models. In this new post to his Medium site he looks at using custom pivot table models (and choosing the right technique to use them).

Laravel is “opinionated”, but that usually just means that there are a lot of defaults set up that push you toward a certain style. Over the years I have found very few cases where you can’t easily override these; more often than not, it is simply a case of adding another function call when you instantiate something.

As I thought about the way I would normally code around Mark’s problem, and then searched and found the more direct answer from the docs, it occurred to me that this was a perfect example.

He starts with the more "standard" way of working with pivot tables in Laravel (code example include) using the updateExistingPivot method. He talks about the difference between writing the code and designing the code, making it more maintainable and well-architected. He talks about moving the logic into a service class but points out a few issues with that approach as well. Finally, in his last option, he covers the special "Pivot" model included with Eloquent and shows it in use.

tagged: custom pivot table model laravel tutorial example

Link: https://medium.com/@codebyjeff/custom-pivot-table-models-or-choosing-the-right-technique-in-laravel-fe435ce4e27e

Laravel News:
Laravel 5.5 Pivot Casting
Jul 06, 2017 @ 14:15:36

On the Laravel News site there's a new post about a feature in the v5.5 release of the popular framework: pivot casting.

A new addition to Laravel 5.5 will add support for pivot table casts when inserting & updating data on an intermediate table model.

Currently, the $casts you set on a traditional model will work in both directions; any model that extends the EloquentModel class will look for a $casts property and convert the specified attributes to a data type when reading and writing.

[...] Now with Laravel 5.5, the $casts property on the EloquentModel and EloquentRelationsPivot classes will behave the same. Laravel will “respect” your casts on both, whether you’re reading, inserting or updating data.

tagged: laravel pivot casting feature insert update

Link: https://laravel-news.com/laravel-5-5-pivot-casting

Gonzalo Ayuso's Blog:
Pivot tables in PHP
Jan 25, 2010 @ 14:45:51

In this new post today Gonzalo Ayuso shows how to make "pivot tables" (at least the concept) in a PHP script to modify a data set and transform it into something else.

It's not very difficult to handle pivot tables by hand but the work is always the same: groups by, totals, subtotals, totals per row... Now I want to write a class to pivot tables in PHP with the most common requirements (at least for me).

His examples takes a few rows of records from a MySQL result and shows how, with the help of a library he's developed, he can make a few commands and fetch just the data he needs including the column to pivot on. A few more examples are also included.

tagged: pivot table tutorial library group

Link:


Trending Topics: