All Projects → sebastiaanluca → laravel-route-model-autobinding

sebastiaanluca / laravel-route-model-autobinding

Licence: MIT license
THIS PACKAGE HAS BEEN DEPRECATED — Automatically bind Eloquent models as route segment variables.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-route-model-autobinding

Plans
Laravel Plans is a package for SaaS apps that need management over plans, features, subscriptions, events for plans or limited, countable features.
Stars: ✭ 326 (+2228.57%)
Mutual labels:  eloquent, model
Laravel Sluggable
An opinionated package to create slugs for Eloquent models
Stars: ✭ 831 (+5835.71%)
Mutual labels:  eloquent, model
Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (+2578.57%)
Mutual labels:  eloquent, model
laravel-loggable
🎥 📽 🎞 Log your model changes in multiple ways
Stars: ✭ 58 (+314.29%)
Mutual labels:  eloquent, model
Rating
Laravel Eloquent Rating allows you to assign ratings to any model.
Stars: ✭ 175 (+1150%)
Mutual labels:  eloquent, model
laravel-boolean-dates
Automatically convert Eloquent model boolean attributes to dates (and back).
Stars: ✭ 31 (+121.43%)
Mutual labels:  eloquent, model
Befriended
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Stars: ✭ 596 (+4157.14%)
Mutual labels:  eloquent, model
laravel-geoly
Perform fast and efficient radius searches on your Laravel Eloquent models.
Stars: ✭ 25 (+78.57%)
Mutual labels:  eloquent, model
Schedule
Schedule is a package that helps tracking schedules for your models. If you have workers in a company, you can set schedules for them and see their availability though the time.
Stars: ✭ 155 (+1007.14%)
Mutual labels:  eloquent, model
Eager Load Pivot Relations
Eager load pivot relations for Laravel Eloquent's BelongsToMany relation.
Stars: ✭ 134 (+857.14%)
Mutual labels:  eloquent, model
Laravel Model Status
Easily add statuses to your models
Stars: ✭ 510 (+3542.86%)
Mutual labels:  eloquent, model
acorn-db
Provides Acorn projects with Eloquent Models for WordPress data.
Stars: ✭ 30 (+114.29%)
Mutual labels:  eloquent, model
Guardian
Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.
Stars: ✭ 121 (+764.29%)
Mutual labels:  eloquent, model
eloquent-filemaker
A Model extension and Eloquent driver for Laravel connecting to FileMaker through the Data API
Stars: ✭ 38 (+171.43%)
Mutual labels:  eloquent, model
db-safedelete
Attempts to invoke force delete, if it fails - falls back to soft delete
Stars: ✭ 16 (+14.29%)
Mutual labels:  eloquent, model
modelsafe
A type-safe data modelling library for TypeScript
Stars: ✭ 13 (-7.14%)
Mutual labels:  model
eloquent-has-by-join
Convert has() and whereHas() constraints to join() ones for single-result relations.
Stars: ✭ 21 (+50%)
Mutual labels:  eloquent
google streetview
A command line tool and module for Google Street View Image API
Stars: ✭ 77 (+450%)
Mutual labels:  route
yii2-rest-client
REST client (AR-like model) for Yii Framework 2.0 (via yii2-http-client)
Stars: ✭ 19 (+35.71%)
Mutual labels:  model
laravel-mass-update
Update multiple Laravel Model records, each with its own set of values, sending a single query to your database!
Stars: ✭ 65 (+364.29%)
Mutual labels:  eloquent

Automatically bind Eloquent models as route segment variables

Latest stable release Software license Build status Total downloads Total stars

Read my blog View my other packages and projects Follow @sebastiaanluca on Twitter Share this package on Twitter

Immediately start using models in your routes without having to worry about maintaining any list or map.

Medium to large Laravel applications can have a lot of models. If you're a heavy user of route model binding to automatically retrieve and inject model instances in your controllers, that means you have to manually register dozens or hundreds of models in your route service provider. You then need to do the same for each new, changed, or deleted model which makes this nifty feature hard to maintain and easy to forget.

This package solves the issue of manually having to register each model by doing all the grunt work for you. It reads your composer.json PSR-4 autoload section and scans all their model directories for usable Eloquent models. It then explicitly binds each model into the router as a route segment variable using a case type of your choosing (see configuring casing).

Example

Get rid of this boilerplate code:

public function boot() : void
{
    Route::model('user', \App\Users\User::class);
    Route::model('order', \App\Orders\Order::class);
    Route::model('shoppingCart', \App\Carts\Cart::class);
    Route::model('Item', \App\Item::class);
    Route::model('Admin', \App\Users\Admin::class);
    … (repeat dozens of times)
}

And just do this (for any Eloquent model in your application):

Route::get('profile/{user}', ShowProfile::class);
Route::get('orders/{order}', ShowOrder::class);
Route::get('carts/{shoppingCart}', ShowShoppingCart::class);
…

Table of contents

Requirements

  • PHP 7.3 or higher
  • Laravel 7.0 or higher

How to install

Via Composer:

composer require sebastiaanluca/laravel-route-model-autobinding

How to use

Defining model namespaces

Laravel route model autobinding uses your composer.json PSR-4 autoload section to know which namespaces and paths to scan. In any new Laravel project, the default App\\ namespace is already in place, so for most projects no additional setup required. If you have other namespaces registered like local modules or (dev) packages, those will be scanned too.

{
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "MyModule\\": "modules/MyModule/",
            "MyPackage\\": "MyPackage/src/"
        }
    }
}

Furthermore it filters out traits, abstract classes, helper files, and other unusable items to only bind valid Eloquent models.

Route segment variables

After installing the package, you can immediately get to work using the aliased Eloquent models in your routes:

Route::get('profile/{user}', ShowProfile::class);
Route::get('orders/{order}', ShowOrder::class);
Route::get('carts/{shoppingCart}', ShowShoppingCart::class);
…

Besides scanning and aliasing your models for you, this package alters no native Laravel functionality. Therefore, see the Laravel documentation on how to use route model injection.

Caching bindings in production

To cache all bindings and speed up your application in production, add the cache command to your deploy scripts:

php artisan autobinding:cache

This scans all your current models and writes a static cache file to the bootstrap/cache directory. Upon subsequent framework booting, it reads the cache file instead of scanning and aliasing on-the-fly.

Note that this thus disables runtime scanning, meaning new models will not be recognized and changes to existing models will not be reflected (not very handy during development). You can however still change the case type in the configuration file, as the binding happens in a later stage.

To clear the cache file, run:

php artisan autobinding:clear

Configuration

Run

php artisan vendor:publish

and select

laravel-route-model-autobinding (configuration)

to publish the configuration file.

Casing

By default, the case type for aliasing models is set to camel case. You can change this to use camel, snake, or studly casing.

See \SebastiaanLuca\RouteModelAutobinding\CaseTypes for possible options.

Camel case (default):

Route::get('carts/{shoppingCart}', ShowShoppingCart::class);

Snake case:

Route::get('carts/{shopping_cart}', ShowShoppingCart::class);

Studly case:

Route::get('carts/{ShoppingCart}', ShowShoppingCart::class);

The case type can still be changed after caching your models.

License

This package operates under the MIT License (MIT). Please see LICENSE for more information.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

composer install
composer test

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

About

My name is Sebastiaan and I'm a freelance back-end developer specializing in building custom Laravel applications. Check out my portfolio for more information, my blog for the latest tips and tricks, and my other packages to kick-start your next project.

Have a project that could use some guidance? Send me an e-mail at [email protected]!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].