1

I'm trying to create a class function which resembles how we used to fetch database listing and convert into a dropdown listing.

eg: DB::table()->where()->get()

what i would like to achieve in laravel custom class or through model is this

Dropdown::fetch()->toArray()

Dropdown::fetch()->toDropdown()

I tried to figure out how this can be done through google. But couldn't find any solution to it.

I'm using laravel 5.8

--

Edit - Sample Code added

Code tried:

namespace App\Http\Models;

use DB;
use Closure;
use BadMethodCallException;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Eloquent\Model;

class Dropdown extends Model
{

    private $result = [];

    private $default;

    public function _cities(){
        $tbl_cities     =   config("tables.TBL_meta_cities");
        $result         =   DB::table($tbl_cities)->select('id', 'cityname')
                                ->orderBy('id')->get()->toArray();
        $this->result   =   $result;
    }

    public function _select(){

    }

    public function _list(){
        return $this->result;
    }

    public function _setDefault($def=''){

    }

    public static function __callStatic($method, $parameters)
    {
        $action     =   '_'.$method;
        if(method_exists(get_called_class(), $action))
            self::$action(...$parameters);
        else echo 'not found';
    }

    public function __call($method, $parameters)
    {
        $action     =   '_'.$method;
        if(method_exists($get_called_class(), $action))
            self::$action(...$parameters);
        else echo 'not found';
    }

}

and i tried

Dropdown::cities()->list()

but ended with bugs

Community
  • 1
  • 1
Dinesh Ravichandran
  • 312
  • 1
  • 5
  • 16
  • could you give an example of the data you want to be in ? – Joseph Feb 27 '20 at 08:22
  • i've added sample code here.. kindly check... thank you @Joseph – Dinesh Ravichandran Feb 27 '20 at 10:13
  • i meant how you want the data to be looks like that you fetch? give an example – Joseph Feb 27 '20 at 10:42
  • ooh.. got it... so lets say the function fetch retrieves the data from whatever table i wish to retrieve and store it in a variable and then the function toArray will convert the data to array and return. toDropdown will convert the retrieved data to a – Dinesh Ravichandran Feb 27 '20 at 11:16
  • but you don't need to all of these functionalities simply after getting the data from the table and pass it in your view you can simply make `foreach` on the data inside your dropdown list, for example, you want to get users name `` – Joseph Feb 27 '20 at 11:25
  • correct... thats how we've been using.. but sometimes we have to use the same dropdown multiple times in datatable... this takes time in foreach section, so thought of writing a function which will generate the dropdown once and do some changes whenever required... – Dinesh Ravichandran Feb 27 '20 at 11:43
  • also to learn new ways in writing reusable functions in laravel so that this method can be used elsewhere... – Dinesh Ravichandran Feb 27 '20 at 11:44
  • you can add function in your model to do that for you – Joseph Feb 27 '20 at 11:47
  • yes... ive already tried... you can see the code attached above. but im getting issue when executing.. not sure where and what issue i did... im pretty sure it might be a minor one... but stuck up and cannot able to figure it out... – Dinesh Ravichandran Feb 27 '20 at 11:52

1 Answers1

1

Well i figured it out myself.

class Dropdown extends Model
{

    private static $result = [];

    private function getCities(){
        $result         =   City::select('id', 'cityname')
                                ->orderBy('id')->get()->toArray();
        self::$result   =   $result;
    }

    public function toArray(){
        return self::$result;
    }

    public function toDropdown(){
        // Do the dropdown works
    }

    /**
     * Dynamically handle calls to the class.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     *
     * @throws \BadMethodCallException
     */
    public function __callMethod($method, $parameters){
        // Check with inclusive
        $class      =   get_called_class();
        $avail      =   false;
        $action     =   '';

        // Check method availability - direct
        if(!$avail){
            $action     =   $method;
            $avail      =   method_exists($class, $action);
        }

        // Check method 2
        if(!$avail){
            $action     =   'get'.ucwords($method);
            $avail      =   method_exists($class, $action);
        }

        if($avail){
            // Call the method
            $return = self::$action(...$parameters);
            if(!empty($return)) return $return;
        } else {
            // Throw error if method not found
            throw new BadMethodCallException("No such method exists: $name");
        }

        return new self;
    }

    public static function __callStatic($method, $parameters){
        return (new self)->__callMethod($method, $parameters);
    }

    public function __call($method, $parameters){
        return (new self)->__callMethod($method, $parameters);
    }

}

All i need to do is return new self which does the trick instead of return $this so that the trailing function can be called easily.

Now i can able to call that function like this

Dropdown::cities()->toArray();


Reference:
https://stackoverflow.com/a/41631711/1156493


Thank you @Joseph for your time & support.

Dinesh Ravichandran
  • 312
  • 1
  • 5
  • 16