Welcome Guest, Not a member yet? Register   Sign In
Custom model methods
#1

Hey guys.
I'm working on a fairly new project, which is storing all setings in a MySQL table (myproject_settings). There are many cases where I need to fetch a single setting, using something like the following:
PHP Code:
$this->settings = new \App\Models\SettingsModel();

$mysetting $this->settings->where('name''mysetting')->first()->val

As you can see, having dozens of those lines throughout my project looks way too verbose. So I was thinking about adding a method to my model directly:
PHP Code:
class SettingsModel extends Model {
   ...

   public function getSetting($name) {
      return $this->where('name'$name)->first()->val;
   }


And then, in my controller, I could just do:
PHP Code:
$mysetting $this->settings->getSetting('mysetting'); 


My question is.. Is it bad practice to add custom methods directly in the model? I couldn't find anything related on the documentation.

Thank you.
Reply


Messages In This Thread
Custom model methods - by Ryubal - 07-01-2021, 03:39 PM
RE: Custom model methods - by ikesela - 07-01-2021, 09:20 PM
RE: Custom model methods - by includebeer - 07-02-2021, 10:20 AM
RE: Custom model methods - by Ryubal - 07-02-2021, 04:32 PM
RE: Custom model methods - by dewaz - 11-09-2021, 03:25 AM



Theme © iAndrew 2016 - Forum software by © MyBB