Git Product home page Git Product logo

findinset_relation's Introduction

findinset_relation

Set eloquent relationships with table that contains comma separated values as a foreign key

Installation

Get package using composer :

composer require ghanuz/relations

1.

You have to extends GhanuZ\Model class instead of Illuminate\Database\Eloquent\Model

<?php

namespace App;

use GhanuZ\Model;

class Test extends Model
{
    //
}

You can also use traits instead of extend Model class

Like in Laravel, Users model not extending Model class. So you can use trait. GhanuZ\FindInSet\FindInSetRelationTrait

Example:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use GhanuZ\FindInSet\FindInSetRelationTrait;

class User extends Authenticatable
{
    use Notifiable, FindInSetRelationTrait;

    public function city ()
    {
        return $this->FindInSetOne( 'App\City', 'city_ids', 'id', 1);
    }

    public function state ()
    {
        return $this->FindInSetOne( 'App\State', 'city_ids', 'id', 2);
    }

    public function country ()
    {
        return $this->FindInSetOne( 'App\Country', 'city_ids', 'id', 3);
    }

    public function hobbies ()
    {
        return $this->FindInSetMany( 'App\Hobbies', 'hobbies_id', 'id');
    }

}

To create HasMany Relation you have to write as :

public function order_product (){
    return $this->FindInSetMany( CLASS_NAME, FOREIGN_KEY, LOCAL_KEY);
}

To create HasOne Relation you have to write as :

public function order_product (){
    return $this->FindInSetOne( CLASS_NAME, FOREIGN_KEY, LOCAL_KEY);
}

You can also pass 4th argumaent to detect position in FIND_IN_SET :

If you have an address table schema and stored city_ids like city_id,state_id,country_id.

Now, you want to find only city_id, you will do as : select * from address where FIND_IN_CITY( 2, city_ids ) = 1;// Here $index = 1

Like, for state_id: select * from address where FIND_IN_CITY( 1, city_ids ) = 2;// Here $index = 2

Like, for country_id: select * from address where FIND_IN_CITY( 5, city_ids ) = 3;// Here $index = 3

4th argument is optional

public function order_product (){
    return $this->FindInSetMany( CLASS_NAME, FOREIGN_KEY, LOCAL_KEY, Number);
}

Added support of Array cast from version v3.

From version v3, you can also use custom casting provided by the Laravel framework to manipulate comma deligated value into array. If you are using casts of Laravel to convert the string with comma value into Array then this package also support it to retrive the related table data.

Example


/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
    'FIELD' => CUSTOM_CLASS::class,
];


/**
*
* To retrive array of relational tables 
*
*/
public function order_products ()
{
    return $this->FindInSetMany( CLASS_NAME, FOREIGN_KEY, LOCAL_KEY, Number);
}

/**
*
* To retrive single data of the relational table
*
*/
public function order_product ()
{
    return $this->FindInSetOne( CLASS_NAME, FOREIGN_KEY, LOCAL_KEY, Number);
}

findinset_relation's People

Contributors

ghanu119 avatar nagavinodcse avatar

Stargazers

Naveed Lawliet avatar Sterli Snax avatar Suganya Rajasekar avatar Bhavesh Daswani avatar Nguyễn Nam Long avatar

Watchers

 avatar

findinset_relation's Issues

Cant use with Laravel 9

Hi,

I'm tried to include vendor with composer but I got errors

Problem 1 - Root composer.json requires ghanuz/relations dev-master -> satisfiable by ghanuz/relations[dev-master]. - ghanuz/relations dev-master requires laravel/framework 5.3|5.4|5.6|5.7|5.8|^6.0.0|^7.0.0|8.* -> found laravel/framework[v5.3.0, ..., v5.8.0, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev] but it conflicts with your root composer.json require (^9.0).

Please update for Laravel 9

Laravel 10

Hi @ghanu119

Please update to Laravel 10

I got an error when doing composer require ghanuz/relations, here the result:

In PackageDiscoveryTrait.php line 332:
                                                                                                                                                                                 
  Could not find a version of package ghanuz/relations matching your minimum-stability (stable). Require it with an explicit version constraint allowing its desired stability.  
                                                                                                                                                                                 

require [--dev] [--dry-run] [--prefer-source] [--prefer-dist] [--prefer-install PREFER-INSTALL] [--fixed] [--no-suggest] [--no-progress] [--no-update] [--no-install] [--no-audit] [--audit-format AUDIT-FORMAT] [--update-no-dev] [-w|--update-with-dependencies] [-W|--update-with-all-dependencies] [--with-dependencies] [--with-all-dependencies] [--ignore-platform-req IGNORE-PLATFORM-REQ] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [-m|--minimal-changes] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--apcu-autoloader-prefix APCU-AUTOLOADER-PREFIX] [--] [<packages>...]

Not working

Return a empty array while using trait but query is run in phpmyadmin give result

Need laravel 8 support

This package is really nice, but it doesn't work on laravel 8.
Could you please add support for v8 as well
Thanks

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.