I'm converting a page to Laravel. I used to have a PHP script (GET) that handled select queries from any table. All I had to do was sent the table parameter, and it would return the result.
The idea was to have a controller like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
class Projects extends Controller
{
/**
* Get projects based on the table
*
* @return Response
*/
public function index($table)
{
$result = DB::select("SELECT projectid FROM `" . $table . "` WHERE isClass=1 LIMIT 10");
return view('pages.allviews', ['results' => $result]);
}
}
The problem is that page will execute this query three times. Happens that, the controller will create the View right after executing, and the only solution would be to repeat the query three times in the controller.
I want to avoid doing that, I want to separate the controller from what the page needs, and be able to execute it the times the page needs. Generalizing is the key.
Is there a way to do this, and keep this abstraction without having to create a new controller?