2017 © Pedro Peláez
 

library laravel-urls

Url trait for Eloquent models

image

cliffordjames/laravel-urls

Url trait for Eloquent models

  • Thursday, September 14, 2017
  • by cliffordjames
  • Repository
  • 1 Watchers
  • 0 Stars
  • 32 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 7 % Grown

The README.md

Easy model routes for Laravel Eloquent models

This package allows you to create easy model routes for you Eloquent models., (*1)

Installing

$ composer require cliffordjames/laravel-urls

Usage

Let's say you have the following routes for displaying a user:, (*2)

Route::get('/users/{user}', function (\App\User $user) {
    return $user;
})->name('users.show');

All you have to do is add the trait to the User model:, (*3)

<?php

namespace App;

use CliffordJames\LaravelUrls\HasUrl;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasUrl;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

And you can generate the url to this route by using the traits url() function:, (*4)

$user = User::first();
//
$user->url(); // -> /users/1
// vs
route('users.show', $user); // -> /users/1

Resource routes

You can specify the route name as the first parameter in de url() function:, (*5)

Route::resource('users', 'UserController');

$user = User::first();

$user->url(); // -> /users/1
$user->url('show'); // -> /users/1
$user->url('edit'); // -> /users/1/edit
$user->url('update'); // -> /users/1
...

Additional parameters

Let's say you have the following route:, (*6)

Route::get('/threads/{channel}/{thread}', 'ThreadsController@show')->name('threads.show');

And you have added the trait and set the relationship from the Thread model to the Channel model, the following examples have the same outcome:, (*7)

$thread->url(); // -> /threads/*channel_id*/*thread_id*
$thread->url($thread->channel); // same as above
$thread->url($thread, $thread->channel); // same as above
$thread->url([$thread, $thread->channel]); // same as above
$thread->url(['thread' => $thread, 'channel' => $thread->channel]); // same as above

route('threads.show', ['thread' => $thread, 'channel' => $thread->channel]); // same as above

Configurations

There is only one configuration you can do for now and it is setting the $baseRoute on the model itself, it defaults to the same method of how the table name for a model is generated., (*8)

User model example, when not set it defaults to user:, (*9)

protected $baseRoute = 'profile';

Now $user->url() is looking for the profile.show route., (*10)

The Versions

14/09 2017

dev-master

9999999-dev

Url trait for Eloquent models

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Clifford James van den Bos

14/09 2017

1.0.4

1.0.4.0

Url trait for Eloquent models

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Clifford James van den Bos

13/09 2017

1.0.3

1.0.3.0

Url trait for Eloquent models

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Clifford James van den Bos

09/09 2017

1.0.2

1.0.2.0

Url trait for Eloquent models

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Clifford James van den Bos

31/08 2017

1.0.1

1.0.1.0

Url trait for Eloquent models

  Sources   Download

MIT

The Requires

  • php ^7.0

 

by Clifford James van den Bos

30/08 2017

1.0.0

1.0.0.0

Url trait for Eloquent model

  Sources   Download

MIT

The Requires

  • php ^7.0

 

by Clifford James van den Bos