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.