0

I'm alway doing the same thing with some of my models. I get list of all models as [id => name] array. I need that to display them in drop-down list in other's models crud to set relations. How can I put one static method in one place that would be available from any model? Now I have to write this code everywhere:

 public static function getModelNameList()
    {
        return self::select('id','name')->get()->mapWithKeys(function ($model){
            return [$model->id => $model->name];
        })->toArray();
    }

and then

$list = ModelName::getModelNameList();
PttRulez
  • 115
  • 7
  • 2
    Add a "base" model/controller that every model/controller extends? – brombeer Nov 27 '19 at 09:25
  • You can check this issues. https://stackoverflow.com/questions/44021662/how-to-create-global-function-that-can-be-accessed-from-any-controller-and-blade – thynameisjayvee Nov 27 '19 at 09:30
  • 4
    btw, the `pluck` method doesn't work for you? `Model::pluck('name', 'id')->toArray()` ... seems like you reinvented the `pluck` method – lagbox Nov 27 '19 at 09:36
  • yeah it was correct. Pluck is better. But I'm still interested how to set new method for eloquent models. @thynameisjayvee I followed the link. They are talking about functions in Common file. I can sue them by using Common file or Helper and call like funcion() or helper->function(). But I need to be able to do like ModelName::myMethod. Just like now we always can do ModelName::where() with any eloquent model – PttRulez Nov 27 '19 at 10:44
  • @kerbholz As for "base" model. Yeah that's what I thought. But where should it be created? In which folder? Is there any standard for that? So my newModel will extend Eloquent Model, will have one new myMethod and I will have to extend my models from newModel ? – PttRulez Nov 27 '19 at 10:47
  • Yep, basically the answer @Davit gave – brombeer Nov 27 '19 at 10:52

1 Answers1

1

You can use

$list = ModelName::pluck('name', 'id'); //Collection
$list = ModelName::pluck('name', 'id')->all(); //array
$list = ModelName::pluck('name', 'id')->toArray(); //array

You can define BaseModel

use Illuminate\Database\Eloquent\Model;

class BaseModel extends Model
{
    public static function getModelNameList()
    {
        return self::select('id','name')->get()->mapWithKeys(function ($model){
            return [$model->id => $model->name];
        })->toArray();
    }
}

Then extend BaseModel

use App\Models\BaseModel;

class ModelName extends BaseModel
{

}

Or you can define Trait and use in model

Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
  • Yes, guys. Looks like I have invented plcuk )) I just knew only about Collection pluck method. Didn't know I can use it as static method for model. Thanks a lot. But I still don't know how to set common method for other eloquent models in my project – PttRulez Nov 27 '19 at 10:40
  • @PttRulez define BaseModel or some Trait – Davit Zeynalyan Nov 27 '19 at 10:48
  • in which folder it should be put? Just 'app' folder where all other models are? Is there any standard where we keep classes extending framework's classes? – PttRulez Nov 27 '19 at 11:11
  • Use some folder where located all model classes. I use Model folder. – Davit Zeynalyan Nov 27 '19 at 11:23