It would be so much easier to review if it was posted on GitHub or something. Looks super clean but I do not like it. I think that you should watch this video https://www.youtube.com/watch?v=MF0jFKvS4SI
Hope it helps.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm still new at Laravel, i don't really know if what i coded is bad. I studied a little about repository pattern, however i felt like doing a base controller would be simple and would solve my problem.
My controller receives json and returns json response. Heres my base controller:
abstract class ApiController extends Controller
{
use ApiResponser;
protected $model;
protected $storeRules, $updateRules, $deleteRules;
public function store(Request $request)
{
$this->validate($request, $this->storeRules);
$requestData = $request->get($this->model::table());
if (!$this->model::insertAll($requestData))
return $this->errorResponse('Could not insert data', 500);
return $this->showAll($requestData, 201);
}
public function index()
{
$items = $this->model::all();
return $this->showAll($items);
}
public function show($id)
{
$item = $this->model::findOrFail($id);
return $this->showOne($item);
}
public function update(Request $request)
{
$this->validate($request, $this->updateRules);
$requestData = $request->get($this->model::table());
$updated = [];
foreach ($requestData as $data) {
$item = $this->model::findOrFail($data['id']);
$item->fill($data);
if ($item->isDirty()) {
$item->save();
array_push($updated, $item);
}
}
return $this->showAll($updated, 200);
}
public function destroy(Request $request)
{
$this->validate($request, $this->deleteRules);
$requestData = $request->input($this->model::table().'.*.id');
$deleted = $this->model::destroy($requestData);
return $this->showAll($deleted, 201);
}
Here is how a child controller looks like:
class CourseController extends ApiController
{
public function __construct()
{
$this->model = Course::class;
$this->storeRules = Course::STORE_RULES;
$this->updateRules = Course::UPDATE_RULES;
$this->deleteRules = Course::DELETE_RULES;
}
Please if anyone could review it or at least give me some advice i would appreciate. First time posting here in laracasts discussion, sorry if my post is poorly structured.
Too much abstraction, magic, and automation.
I like implicit model binding, request objects validation. Takes controllers cleanliness to the extreme.
Dedicated controllers are a must in real-world apps since every model has different relationships and it's better to have all the logic in the app than to outsource for example cascade delete to the database. What if you switch to some database that does not support it? It would get very dirty to put that logic in the controller you wrote. Also overwriting base controller methods is not a common pattern and it can be overlooked easily especially by new team members.
This is just an excerpt but there is so much that would bring complexity and headache if you go with this snippet of code.
I think I've heard @JeffreyWay say that programmers (especially juniors) are hesitant to create new classes and they tend to put everything in existing structure/files. This might not apply to you but it gave me that impression.
Sorry if my thoughts are confusing. I lack some serious amount of sleep.
Please or to participate in this conversation.