GithubHelp home page GithubHelp logo

khalyomede / laravel-eloquent-uuid-slug Goto Github PK

View Code? Open in Web Editor NEW
25.0 2.0 0.0 187 KB

Use auto generated UUID slugs to identify and retrieve your Eloquent models.

Home Page: https://packagist.org/packages/khalyomede/laravel-eloquent-uuid-slug

License: MIT License

PHP 97.44% Dockerfile 2.56%
laravel laravel-package eloquent uuid slug route-model-binding url

laravel-eloquent-uuid-slug's People

Contributors

khalyomede avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-eloquent-uuid-slug's Issues

New firstBySlug and firstBySlugOrFail methods

Current alternative, which forces the developer to hard code the slug column name, since Sluggable::getSlugColumn() is private:

$slug = "...";
$post = Post::withTrashed()->where("slug", $slug)->first();

Expected:

$slug = "...";
$post = Post::withTrashed()->firstBySlug($slug);

Trait doesn't preserves Eloquent model generic when running static analysis

Running PHPStan on this code will return the error below

$shoppingLists = ShoppingList::with("items")
  ->authorOrMemberMatching($user)
  ->when(
    $sort === ViewOrder::UncompletedFirst->value,
    fn (Builder $query): Builder =>
    $query->withCount(["items" => fn (Builder $item): Builder => $item->uncompleted()])
      ->orderByRaw("(items_count > 0) DESC, updated_at DESC")
  )
  ->when(
    $sort !== ViewOrder::UncompletedFirst->value,
    fn (Builder $query): Builder =>
    $query->orderByDesc("updated_at")
  )
  ->paginate(8)
  ->appends([
    "sort" => $sort,
  ]);
Anonymous function should return Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model> but returns  
  Khalyomede\EloquentUuidSlug\Builder\SluggableBuilder<App\Models\ShoppingList>.                                         
  ๐Ÿ’ก Template type TModelClass on class Illuminate\Database\Eloquent\Builder is not covariant. Learn more:               
    https://phpstan.org/blog/whats-up-with-template-covariant

add ExistsBySlug form request

Use case:

namespace App\Http\Requests;

use App\Models\Posts;
use Illuminate\Foundation\Http\FormRequest;
use Khalyomede\EloquentUuidSlug\ExistsBySlug;

class MyRequest extends FormRequest
{
  public function rules()
  {
    return [
      "post" => "required|exists:slug", // before
      "post" => ["required", new ExistsBySlug(Post::class), // after (will dynamically get the "slug" column name for you)
    ];
  }
}

Find eloquent model by slug

User::findBySlug("..."); which is a shortcut of User::withSlug("...")->first();

User::findBySlugOrFail(); as well

New ExistsBySlug validation rule

Instead of

return [
  "post" => [
    "required", 
    new Exists(Post::class, "slug"),
  ],
];

We have this rule which automatically finds the correct slug column name

return [
  "post" => [
    "required", 
    new ExistsBySlug(Post::class),
  ],
];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.