D2S Development

0 %
David Birkin
Full Stack Web Developer
Laravel Enthusiast
  • Residence:
    Florida
  • City:
    Port Saint Lucie
  • Age:
    35
English
Team Skills
People Skills
PHP
Laravel
HTML
Tailwind
JavaScript
DRY Principles
Wordpress
SOLID Principles
Database Design
  • Bootstrap, Material UI, MaryUI
  • TailwindCSS
  • Sass, Boostrap 4+
  • Webpack, Vite
  • Git / Software Versioning
  • Livewire, AlpineJS

How to Handle Laravel Pivot Table Exceptions

01/24/2024

Depending on how you setup your pivot tables in Laravel, you may experience a few exceptions. Whilst doing a Stripe Integration during a recent project I was trying to update a pivot table, that holds a little more than just 2 different id’s. The structure I used is in the screenshot below. You can see the main Pivot columns are plan_id and team_id. However, I associate a little more data with these columns.

The Problem!

When trying to update the additional pivot details associated with this table, I was initially getting exceptions that the id and updated_at fields were Unknown Columns in field_list. This was something done by design, as Pivot tables generally do not need an id as a primary key.

By default, if you try and update a model using the following method, Laravel will assume your primary key is id and you have the out of the box default timestamps that exist on the migration stubs when making a migration file.

 $teamPlan = $team->plan();
 $teamPlan->update([
   'plan_id' => $plan->id,
   'team_id' => $team->id,
   'licenses' => $plan->licenses,
   'products' => $plan->products,
   'price' => $plan->price,
   'annual_discount_percentage' => $plan->annual_discount_percentage,
   'additional_license_cost' => $plan->additional_license_cost,
]);

The Solution

But I was struggling to understand how to resolve this issue, until I realized you can place a protected and public property in your Models file, to tell Laravel that you don’t have these columns! To do this, you need to add the following properties to your Model file.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PlanTeam extends Model
{

    public $timestamps = false;

    protected $primaryKey = 'team_id';
    
    protected $table = 'plan_team';

    // Additional Code Here ... 

}

This will indicate to Laravel that your table has no timestamps, and uses a different column as a Primary Key/Unique column. The $primaryKey is what will be used in the SQL where clause instead of Laravel trying to default it to id.

The timestamps property, which has to be public, indicates you are not using any Laravel default timestamps, meaning created_at and updated_at so using $model->update([]) will not try and touch these columns!

Final Thoughts

Laravel provides a lots of useful features out of the box. Some are lesser known, whilst others are well known. With all these features, Laravel provides a seamless Developer Experience (DX) allowing us as developers to focus on whats important.

I hoped this helps you out. If you would like to keep up to date on more blog posts or Laravel Tips in the future be sure to give me a follow over on X/Twitter

Posted in Design, Laravel, Technology, Website Development