定制 codingpaws/laravel-findby 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codingpaws/laravel-findby

最新稳定版本:2.3

Composer 安装命令:

composer require codingpaws/laravel-findby

包简介

Ruby-like findBy in your Laravel model queries.

README 文档

README

The codingpaws/laravel-findby library allows you to use the modern PHP 8.0 named arguments in Laravel’s eloquent queries similar to Ruby’s Active Record find_by.

class User extends Model
{
    use CodingPaws\FindBy\FindBy;
}

User::findBy(
    first_name: 'Jane',
    role_id: 5,
    is_admin: false,
)->get();

It also introduces a findByNot method that is a shorthand for not equals (!=):

Book::findByNot(
    genre: 'SciFi',
    legth: 'long',
)->findBy(genre: 'Drama', author: 'Dürrenmatt')->get();

This project is still in its early days. The technical solution builds on top of Laravel’s existing query builder. While adding new functionality we need to ensure that existing features keep working.

Installation

composer require kevslashnull/futuristic-eloquent-builder

The futuristic eloquent builder is opt-in per model. To perform queries with named arguments for a given model, use the trait (use FindBy) in its class.

Supported Methods

You can use these methods on your model (e.g. Book::findBy) and on builders of that model (e.g. Book::where('id', 3)->findBy).

  • findBy, works like the native where
  • findByNot, works like the native where but inverted (using != instead of =)
  • orFindBy, works like the native orWhere
  • orFindByNot, works like the native orWhere but inverted (using != instead of =)

All methods support using arrays as value. For example,

Book::findBy(
    genre: ['SciFi', 'Adventure', 'Drama'],
)->get();

queries all books where the genre column is either SciFi, Adventure, or Drama.

Technical background

Most of the magic happens in the NamedBuilder class. It extends eloquent’s Builder class with the findBy and findByNot methods. Chaining where and findBy is possible by overriding the where method and always returning a NamedBuilder instance.

Version 1.0 of the library allowed named parameters in the where query but it turned out to be impossible because.

In reality,

User::findBy(first_name: 'John', last_name: 'Doe', gender: 'male');

translates to

User::where('first_name', 'John')->where('last_name', 'Doe'->where('gender', 'male');

Everyone can contribute

The project is licensed under MIT. Everyone can contribute. ❤️

统计信息

  • 总下载量: 4.14k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固