4

Currently I am using the following code in my controller to query the database. This code checks whether a user has set their username yet.

$user = User::where('email', $userdata['email'])->first();

if(empty($user->username)){
    echo 'Set username here...';
} else {
    echo 'My home page!';
}

My question is, is it better to make a function in the User model to do this, or keep it as it is. So for example, the first line would be removed and in the if statement it would call the model function which would give true or false.

My initial thought is this should be moved to a model function as MVC structured projects should have 'fat models' and 'skinny controllers'. This is 'business logic' so should be in the model. If so, could you give an example on how I would move this to a model and call the function from the controller.

3
  • You should definitely move all data related code to the model. Commented Mar 2, 2017 at 18:32
  • @AlexeyMezenin Could you give an example on how I would do this, most examples I have seen do all the 'business logic' in the controller, which I thought was bad practice. Commented Mar 2, 2017 at 18:33
  • Create a Repository to query the data to the Model . Commented Nov 21, 2021 at 17:59

3 Answers 3

3

You should definitely move all data related code to the model. You've asked for an example. I'd create this method in a model:

public function findByEmail($email)
{
    return $this->where('email', $email)->first();
}

In the controller:

use App\User;

protected $user;

public function __construct(User $user)
{
    $this->user = $user;
}

public function showSomething($userdata)
{
    return view('some.view', [
        'user' => $this->user->findByEmail($userdata['email'])
    ]);
}

In a view:

{{ empty($user->username) ? 'Hello anonymous' : $user->username }}

In this example, it looks like moving query in the model is not very good idea, but when your application will grow you'll see this is the only good way to work with data. It's MVC. Also, you should keep validation logic in the Request classes, business logic in their own classes etc.

Sign up to request clarification or add additional context in comments.

Comments

0

You could write in your User.php model a function like :

public function hasUsername()
{
   if($this->username)
   {
      return true;
   } 

   return false;  
}

and in your controller you could say:

$user = User::where('email', $userdata['email'])->first();

if($user->hasUsername()) 
{
   // do somethin
}

2 Comments

Surely this isn't achieving much though?
sorry if it doesn't help. and i think it's better to make fatty models and skinny controllers.
0

Model

public static function checkIfUsernameExists($email) {
$user = User::where('email', $email)->first();

if (empty($user->username)) {
  return true;
} else {
  return false;
}
}

Controller

if(User::checkIfUsernameExists($userdata['email'])){
      echo 'Set username heree...';
    } else {
        echo 'My home page!';
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.