Support the ongoing development of Laravel.io →
Database Eloquent

Hi I've been using larval for a while now but I really want to make sure I'm doing this correctly and not creating bad code. In the past I've had Eloquent set up but if for example I want to insert a new row to the database I've set up a static method in my model like so:

class Category extends Eloquent {
    public static function insertCategory($input)
    {
        
        $category = new Category;
        $category->name = $input['name'];
        $category->parent_category_id = ($input['parent_category_id']!='') ? $input['parent_category_id'] : 0;
        $category->description = $input['description'];
        return $category->save();

    }
}

Then used something like this in my controller:

public function postInsertCategory()
	{

		$input = Input::all();

		Category::insertCategory($input);

		return Redirect::to('/link-here');
	}

What I'm not sure about is should I be creating a static method like I did? Or is it better to create a facade? I like having the insert (and other) functions in my model so I can reuse them but is this the correct way to do it?

Thanks for any help

Last updated 3 years ago.
0

Why use static at all? is there any need, ask yourself. Are you just doing this so the syntax looks pretty?

In my opinion, facades are bad news. Try not to use them as much as possible.

To address the actual question:

I would create a category service provider of some type.

0

To be honest I was just doing it like that because it worked. It seemed tidy to have it organised like that :)

I'll have a look at service providers though - thanks!

0

So what I've gone for in my workbench package is the following for the controller:

class TestController extends BaseController {
	
	protected $layout = 'workbench::layouts.common';
	protected $test;

	public function __construct(Test $test)
	{
		$this->test = $test;
	}

	public function getIndex()
	{
		$model_response = $this->test->testMe();
		Helpers::printArray($model_response);

		$this->layout->content = View::make('workbench-test::test.default');
	}
}

I know this isn't a service provider but it seems to work quite well. I assume this is a reasonable way to do it?

Thanks again

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.