Giter Site home page Giter Site logo

eloquent-ci-relations's Introduction

Eloquent case-insensitive relations

Build Status Version Total Downloads License

Using Eloquent out of the box on case-insensitive collation databases could potentially return incomplete collection of items if the foreign keys are set as strings and they differ in terms of uppercase vs lowercase. On eager loaded relations Eloquent builds a dictionary of the parent models and associates their related models by their keys. PHP is case-sensitive and therefore if the keys are different, then some of the related models will not be returned. This package simply normalizes the dictionary keys into lowercase.

Example:

Items table

uuid name
aaa First
bbb Second
ccc Third

Tags table

id label
1 tag 1
2 tag 2
3 tag 3

item_tag table

item_uuid tag_id
AAA 1
aaa 2
bbb 3

Item::with('tags')->find('aaa');

The related tags would include only the "tag 2" even if the the DB collation is case-insensitive and the returned query data includes "tag 1".

With case insensitive relations: The related tags would include both "tag 1" and "tag 2".

Requirements

  • illuminate/database 5.5.33 and up, 5.6.*, 5.7.*, 5.8.*, 6.20.26 and up, 7.30.4, 8.40.0 an up

Installation

composer require tishotm/eloquent-ci-relations

Usage

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    use \TishoTM\Eloquent\Concerns\HasCiRelationships;

    // ... relations
}

License

MIT license.

eloquent-ci-relations's People

Contributors

asterd avatar d13r avatar tishotm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

eloquent-ci-relations's Issues

Laravel 10

PS C:\xampp\htdocs\laravel10> composer require tishotm/eloquent-ci-relations
Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been updated
Running composer update tishotm/eloquent-ci-relations
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

Problem 1
- tishotm/eloquent-ci-relations v1.0.0 requires illuminate/database ^5.5.33 || ~6.0 || ~7.0 || ~8.0 -> found illuminate/database[v5.5.33, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27] but these were not loaded, likely because it conflicts with another require.
- tishotm/eloquent-ci-relations v1.0.1 requires illuminate/database ^5.5.33 || ^6.20.26 || ~7.0 || ~8.0 -> found illuminate/database[v5.5.33, ..., v5.8.36, v6.20.26, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27] but these were not loaded, likely because it conflicts with another require.
- tishotm/eloquent-ci-relations[v1.0.2, ..., v1.1.0] require illuminate/database ^5.5.33 || ^6.20.26 || ^7.30.4 || ^8.40.0 -> found illuminate/database[v5.5.33, ..., v5.8.36, v6.20.26, ..., v6.20.44, v7.30.4, v7.30.5, v7.30.6, v8.40.0, ..., v8.83.27] but these were not loaded, likely because it conflicts with another require.
- tishotm/eloquent-ci-relations v1.2.0 requires illuminate/database ^5.5.33 || ^6.20.26 || ^7.30.4 || ^8.40.0 || ^9.0 -> found illuminate/database[v5.5.33, ..., v5.8.36, v6.20.26, ..., v6.20.44, v7.30.4, v7.30.5, v7.30.6, v8.40.0, ..., v8.83.27, v9.0.0, ..., v9.52.16] but these were not loaded, likely because it conflicts with another require.
- Root composer.json requires tishotm/eloquent-ci-relations * -> satisfiable by tishotm/eloquent-ci-relations[v1.0.0, ..., v1.2.0].

You can also try re-running composer require with an explicit version constraint, e.g. "composer require tishotm/eloquent-ci-relations:*" to figure out if any version is installable, or "composer require tishotm/eloquent-ci-relations:^2.1" if you know which you need.

Relationship query returns null on case insensitive keys

Models

use Illuminate\Database\Eloquent\Model;
use TishoTM\Eloquent\Concerns\HasCiRelationships;

class Brand extends Model
{
    use HasCiRelationships;

    public function sales(){
        return $this->hasMany(Sale::class, 'Brand', 'name');
    }

class Sale extends Model
{
    use HasCiRelationships;

    public function brand(){
        return $this->belongsTo(Brand::class, 'Brand', 'name');
}

Data

brands table
id, name
1, KIWI

sales table
id, Status, Brand
xx, Fulfilled, Kiwi
xx, Fulfilled, KIWI

Tinker

>>> $kiwi = Brand::where('name','KIWI')->first()
=> App\Models\Brand {#4018
     id: 2,
     name: "KIWI",
     created_at: null,
     updated_at: "2019-12-23 22:59:23",
     supplier_id: 1,
   }
>>> $kiwi->sales()
=> TishoTM\Eloquent\Relations\HasMany {#3983}
>>> $kiwi->sales()->get()->count()
=> 0
 $sale = Sale::where('Brand', 'Kiwi')->first()
=> App\Models\Sale {#3931
     id: 3994114,
     OrderQuantity: 1,
     Brand: "Kiwi",
   }
>>> $sale->brand
=> null

Laravel 6.18

There are both sales items listed as "KIWI" and "Kiwi" will only pull "KIWI".

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.