1

Ok so I currently have this Controller which basically retrieves a Model and does some calculation.

Original code has tonnes of calculation but this is a trimmed down version for simplicity.

I wanted to move all logic to the Model and have built the code below it so far but can not figure out how to pass the custom messages to the Controller.

I am a beginner in Laravel so trying to achieve this in an easy to understand way so I can maintain it and managed to get the code below working but without custom error messages being passed onto the Controller.

Can you give me an example code of how you are passing custom error messages to controller

This is the original code in controller.

Controller

public function getDetail()
{
    $request = Model::where('id','=',8)->first();

    if($request)
    {
        if($request->number >= 5)
        {
            return Redirect::back()->withMessage('You have 5 or more');
        }

        if($request->number > 0 && $request->number < 5)
        {
            return Redirect::back()->withMessage('You have between 1 and 4');
        }

        if($request->number <= 0)
        {
            return Redirect::back()->withErrors('You do not have enough points');
        }       

    }
    else
    {
        return Redirect::back()->withErrors('No details found');
    }
}

This is the new code I tried to build to move logic to model but could not figure out how to pass the custom error messages along?

Model 
Class Profile
{
    private $model
    function __construct() 
    {     

            $this->model = Model::where('id','=',8)->first();

    }   

    public function Notification()
    {
        if($this->model->number >=5)
        {
            return true;
        }

        if($this->model->number > 0 && $this->model->number < 5)
        {
            return true;
        }

        if($this->model->number <=0)
        {
            return false;
        }

    }

}


Controller
public function getDetail()
{
    $request = new Profile;
    $result = $request->Notification();

    if($result)
    {
        return Redirect::back()->withMessage(????????);
    }
    else
    {
        return Redirect::back()->withErrors(????????);
    }

}

1 Answer 1

1

Just return the message from the Model function and use it in the controller to return like shown below.

Model function

public function Notification()
{
    $returnArray = array();
    if($this->model->number >=5)
    {
        $returnArray['isMessage'] = true;
        $returnArray['message'] = "You have 5 or more";
    }

    if($this->model->number > 0 && $this->model->number < 5)
    {
        $returnArray['isMessage'] = true;
        $returnArray['message'] = "You have between 1 and 4";
    }

    if($this->model->number <=0)
    {
        $returnArray['isError'] = true;
        $returnArray['error'] = "You do not have enough points";
    }
    return $returnArray;
}

Controller function

public function getDetail()
{
    $request = new Profile;
    $result = $request->Notification();

    if(isset($result['isMessage']) && $result['isMessage'] == true)
    {
        return Redirect::back()->withMessage($result['message']);
    }
    else if (isset($result['isError']) && $result['isError'] == true)
    {
        return Redirect::back()->withErrors($result['error']);
    }

}

Ideally speaking you should not create an object of Model in controller. You can just create the function as a static method inside model and call it from controller.

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

1 Comment

Hey adam, if this solved your problem, please accept it as the correct answer. :)

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.