Cako
Cako

Reputation: 426

laravel one controller for differents models with same schema

Hello I am developing an application with a Laravel api server and a C# Winforms frontend. The application has many database models, but most of them consist only in an id field and a name field. No more operations than storing and retrieving that data is needed. The data can't be combined into one table (technically is possible, but it would be a mess) because is totally different data,from postal codes and associated city to families of products.

So I thought, as in C# I could make a generic form, if there is possible to make this in Laravel as it will speed up my work too much.

Edit: Here I attach one example controller I want to make it Generic.

<?php

namespace App\Http\Controllers;

use App\Http\Requests\LocalidadRequest;
use App\Localidad;
use Illuminate\Http\Request;

class LocalidadsController extends Controller
{
    public function index()
    {
        return response()->json(array("data" => Localidad::all(), "error" => 0), 200);
    }

    public function show(Localidad $localidad)
    {
        return $localidad;
    }

    public function store(LocalidadRequest $request)
    {

        $localidad = Localidad::create($request->all());

        return response()->json($localidad, 201);
    }

    public function update(LocalidadRequest $request, Localidad $localidad)
    {
        $localidad->update($request->all());
        return response()->json($localidad, 201);
    }

    public function delete(Localidad $localidad)
    {
        $localidad->delete();
        return response()->json(null, 204);
    }
}

I would like to generalize this controller like this (this isn't real code):

public function index()
{
    return response()->json(array("data" => ??::all(), "error" => 0), 200);
}

public function show(Model $routeModel)
{
    return $routeModel;
}

public function store(CommonRequest $request)
{

    $routeModel = ??::create($request->all());

    return response()->json($routeModel, 201);
}

public function update(CommonRequest $request, Model $routeModel)
{
    $routeModel->update($request->all());
    return response()->json($localidad, 201);
}

public function delete(Model $routeModel)
{
    $routeModel->delete();
    return response()->json(null, 204);
}

Thanks

Upvotes: 0

Views: 1135

Answers (1)

Ben
Ben

Reputation: 5129

It is possible and can structure the route and controller as follows:

Define a generic controller:

abstract class CommonController extends Controller
{
    abstract protected function getModel();

    public function index()
    {
        return response()->json(array("data" => ($this->getModel())::all(), "error" => 0), 200);
    }

    ....
}

For example, if you have city model, then you can define a city controller and inherit the generic controller:

class CityController extends CommonController
{
    protected function getModel()
    {
        return App\City::class;
    }
}

For route, register it to corresponding controller:

Route::get('cities', 'CityController@index');
....

Upvotes: 1

Related Questions