Accessors (Getter) & Mutators (Setter) On a Pivot Table in Laravel



PHP Snippet 1:

class User extends Model {
    public function workspaces() {
        return $this->belongsToMany('App\Models\Workspace')->withPivot('role');
    }
}

class Workspace extends Model {
    public function users() {
        return $this->belongsToMany('App\Models\User')->withPivot('role');
    }
}

PHP Snippet 2:

$user = User::first();

// get data
foreach($user->workspaces as $workspace) {
    var_dump($workspace->pivot->role);
}

// set data
$workspaceId = $user->workspaces->first()->id;
$user->workspaces()->updateExistingPivot($workspaceId, ['role' => 'new role value']);

PHP Snippet 3:

namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class UserWorkspacePivot extends Pivot {
    getRoleAttribute() {
        ...
    }
    setRoleAttribute() {
        ...
    }
}

PHP Snippet 4:

class User extends Model {
    // normal many-to-many relationship to workspaces
    public function workspaces() {
        // don't forget to add in additional fields using withPivot()
        return $this->belongsToMany('App\Models\Workspace')->withPivot('role');
    }

    // method override to instantiate custom pivot class
    public function newPivot(Model $parent, array $attributes, $table, $exists) {
        return new UserWorkspacePivot($parent, $attributes, $table, $exists);
    }
}

class Workspace extends Model {
    // normal many-to-many relationship to users
    public function users() {
        // don't forget to add in additional fields using withPivot()
        return $this->belongsToMany('App\Models\User')->withPivot('role');
    }

    // method override to instantiate custom pivot class
    public function newPivot(Model $parent, array $attributes, $table, $exists) {
        return new UserWorkspacePivot($parent, $attributes, $table, $exists);
    }
}

PHP Snippet 5:

namespace App;
use Illuminate\Database\Eloquent\Model;

class User extends Model {
    public function workspaces() {
        return $this->belongsToMany('App\Workspace')->using('App\UserWorkspace');
    }
}

PHP Snippet 6:

namespace App;
use Illuminate\Database\Eloquent\Model;

class Workspace extends Model {
    public function users() {
        return $this->belongsToMany('App\User')->using('App\UserWorkspace');
    }
}

PHP Snippet 7:

namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;

class UserWorkspace extends Pivot {
    public function getRoleAttribute() {
        // your code to getter here

    }
    public function setRoleAttribute($value) {
        // your code to setter here
    }
}

PHP Snippet 8:

// This will skip the mutator on our extended Pivot class
$user->workspaces()->attach($workspace, ['role' => 'new role value']);

PHP Snippet 9:

$user->workspaces()->attach($workspace);
$user->workspaces()->updateExistingPivot($workspaceId, ['role' => 'new role value']);

PHP Snippet 10:

use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class UserWorkspaceBelongsToMany extends BelongsToMany {
    public function attach($id, array $attributes = [], $touch = true)
    {
        $role = $attributes['role'];
        unset($attributes['role']);
        parent::attach($id, $attributes, $touch);
        $this->updateExistingPivot($id, ['role' => $role], $touch);
    }
    // You will need sync here too
}

PHP Snippet 11:

// put this in the User and Workspace Class
public function userWorkspaceBelongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
{
    if (is_null($relation)) {
        $relation = $this->getBelongsToManyCaller();
    }

    $foreignKey = $foreignKey ?: $this->getForeignKey();

    $instance = new $related;

    $otherKey = $otherKey ?: $instance->getForeignKey();

    if (is_null($table)) {
        $table = $this->joiningTable($related);
    }

    $query = $instance->newQuery();

    return new UserWorkspaceBelongsToMany($query, $this, $table, $foreignKey, $otherKey, $relation);
}

PHP Snippet 12:

class User extends Model {
    public function workspaces() {
        return $this->userWorkspaceBelongsToMany('App\Models\Workspace')->withPivot('role');
    }
}

class Workspace extends Model {
    public function users() {
        return $this->userWorkspaceBelongsToMany('App\Models\User')->withPivot('role');
    }
}