1

By default when creating controller in Laravel there is empty methods like create, store, edit, update and etc. for basic CRUD operations. CRUD is something like generally accepted technique which is used in this kind of situations... But for example for user model I created 2 methods: save and get.

public function save($user_id = FALSE, UserRequest $request)
{
    $user = NULL;

    if ($user_id)
        $user = User::find($user_id);
    else if (($request->has('email') or $request->has('username')) and $request->has('password')) {
        $data = $request->only('username', 'email');
        $data = array_filter($data);

        $user = new User($data);
    }

    if (is_null($user))
        return $this->ajaxResponse(['error' => TRUE, 'message' => 'User not found']);

    if ($password = $request->get('password', FALSE))
        $user->password = Hash::make($password);

    $user->fill($request->except('password', '_token'));

    $user->save();

    return $this->ajaxResponse();
}

As you can see it consists of one if which checks if I need to use existing user or create a new one. Something like firstOrCreate method except that I don't create user until all check are done.

So basically my question is about should I stay in a traditional CRUD or if it suits my purposes I can leave it as it is now.

Flygenring
  • 3,818
  • 1
  • 32
  • 39
Kin
  • 4,466
  • 13
  • 54
  • 106

3 Answers3

1

As I know, the create in Laravel controller means show the create form and store means store data in storage as your save fucntion. the "create" here is not the same as the "C" in CRUD.

And in recently years, more and more frameworks provide integrated function call to simplify the routing and duplicated process. As you said, firstOrCreate may have included check and save action to minimum the coding efforts.

My personal experience is it is used in mixed way. Sometimes, you can use integrate function/lib to make code easy to read and get it done quickly. But sometimes you still need to back to the very basic for some optimization or fit to customized logic or process.

This is not a "YES" or "NO" question, from my point of view.

melson.jao
  • 204
  • 1
  • 6
1

Adding to @melson.jao's answer.

What Laravel provides you are just guidelines. If you feel like adopting a custom style will make your code better, go ahead.

CRUD is generally used for complying with REST API compatibility for entities such as "Books", "Items", etc. Few entities will need more than what a common entity would require such as "Users" as you have stated.

It will be a design decision that may get different opinions from each developer. It is in your hands to decide what will make your code more readable and maintainable.

My personal opinion: you don't need to stick to the guidelines in this case.

brainless
  • 5,698
  • 16
  • 59
  • 82
0

Generally, if you aren't able to tell if it's ok to contravene a practice imposed by a framework you are using, then you are better off following it.

That said - there is additional benefits to following the structure then just "staying in a traditional CRUD". Laravel supplies built in functionality for Restful Resource Controllers.

This can help you not having to make up your own architectural style and make sure you have a consistent set of architectural constraints from something more widely acknowledged.

Nicklas Kevin Frank
  • 6,079
  • 4
  • 38
  • 63
  • In my app I have huge controllers where `store` and `update` methods are slightly different so there of course I use `CRUD`, but in the app also are smaller things like user create\update which consists of few fields update. So in future I want save time in those places and simply use method i posted in first post. Laravel resource controller requires `PUT`, `DELETE` methods which requires extra time to develop. – Kin Mar 11 '16 at 09:59