2017 © Pedro Peláez
 

library resolvable

Make Eloquent models resolvable by fields other than the primary key

image

journolink/resolvable

Make Eloquent models resolvable by fields other than the primary key

  • Saturday, July 7, 2018
  • by adam-prickett
  • Repository
  • 0 Watchers
  • 0 Stars
  • 11 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Resolvable

Make your Eloqeunt models resolvable by more than their key., (*1)

Models will be resolved from an instance of the same Model, the primary key or any of the fields specified in the $resolvable variable., (*2)

This saves the requirement to list multiple where() statements to locate the correct Model, or where the data parameter can vary., (*3)

Usage

Import the Resolvable trait to your model and define your resolvable fields. (The primary key field on your Model is automatically included during resolution), (*4)

<?php

namespace App\Models;

use JournoLink\Resolvable\Resolvable;
use Illuminate\Database\Eloquent\Model;

class Thing extends Model
{
    use Resolvable;

    /**
     * An array of field names that the Model can be resolved from
     *
     * @var array
     */
    protected $resolvable = ['name'];
    ...
}

Call the resolve() static method to resolve an instance from your parameter., (*5)

// Thing {
//     id: 1
//     name: "My Thing"
//     value: 123.45
// }

// All 3 will return the same Model
$thing1 = Thing::resolve(1);
$thing2 = Thing::resolve('My Thing');
$thing3 = Thing::resolve($thing1);

// This will return null as "value" isn't in the $resolvable array
$thing4 = Thing::resolve(123.45);

The Versions

07/07 2018

dev-master

9999999-dev https://bitbucket.org/journolink/resolvable

Make Eloquent models resolvable by fields other than the primary key

  Sources   Download

MIT

by Adam Prickett

07/07 2018

1.0.1

1.0.1.0 https://bitbucket.org/journolink/resolvable

Make Eloquent models resolvable by fields other than the primary key

  Sources   Download

MIT

by Adam Prickett

07/07 2018

1.0

1.0.0.0 https://bitbucket.org/journolink/resolvable

Make Eloquent models resolvable by fields other than the primary key

  Sources   Download

MIT

by Adam Prickett