Pathros
Pathros

Reputation: 10720

Laravel: How to reuse pieces of code in several methods of a controller

I am building a form in order to both create and edit records.

Since I am using a lot of relational information (tables) from several catalogs and shown in the for as a select box (Select2), I need to retrieve all the data to be shown in those HTML select tags.

So, let's say that in my controller in the create() method, I call that info like so:

create() method of MyController.php:

public function create(Token $token){
    //Tags
    $universities      = University::orderBy('name')->get();
    $countries         = Country::orderBy('name')->get();
    $programs      = Program::orderBy('name')->get();
    //... and many more

    return view('my.form.create',[
        'universities' =>  $universities,
        'countries'    =>  $countries,
        'programs'     =>  $programs,
        'token'        =>  $token
    ]);
}

How do I do to reuse that piece of code //Tags

//Tags
$universities      = University::orderBy('name')->get();
$countries         = Country::orderBy('name')->get();
$programs      = Program::orderBy('name')->get();
//... and many more

in order to reuse it for the, let's say, edit() method or other ones??

Upvotes: 1

Views: 3258

Answers (3)

user2094178
user2094178

Reputation: 9444

You can create a method to handle that part individually, such as:

/**
 * Tags from universities.
 * 
 * @return \Illuminate\Database\Eloquent\Collection
 */
protected function getUniversities() {
    return University::orderBy('name')->get();
}

Then you can use it in other methods of the class by just doing $universities = $this->getUniversities().

The getUniversities() method can also live in a parent class.

Upvotes: 2

Rwd
Rwd

Reputation: 35170

You could put them in a separate method in your class that just returns the data as an array and then call that method from your create and edit methods:

protected function getFormData()
{
    return [
        'universities' => University::orderBy('name')->get(),
        'countries'    => Country::orderBy('name')->get(),
        'programs'     => Program::orderBy('name')->get(),
    ];
}

Then your create and edit methods would look something like:

public function create(Token $token)
{
    $data = $this->getFormData();

    return view('my.form.create', $data);
}

public function edit(Token $token)
{
    $data = $this->getFormData();

    return view('my.form.edit', $data);
}

If you're not planning on adding anything to the data array then you could simply inline the call instead:

return view('my.form.edit', $this->getFormData());

Upvotes: 3

Carlos Salazar
Carlos Salazar

Reputation: 1898

If you will reuse the code in a major part of the controller i would say to use the constructor of the controller

function __construct()
{
    $this->universities      = University::orderBy('name')->get();
    $this->countries         = Country::orderBy('name')->get();
    $this->programs      = Program::orderBy('name')->get();   
}

this will load all the values you declare in each controller method.

if not you can create a helper in your app folder that return the values you need.

Upvotes: 0

Related Questions