Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What causes the failure of a success confirmation message in this Laravel 8 application?

I am working on an app with Laravel 8.

I use the following method to add new users to the users table:

public function insert() {

    //Check if user already exists
    $user = User::where('email', '=', session('userEmail'))->first();

    if (!$user) {
        return User::create([
            'first_name' => session('firstName'),
            'last_name' => session('lastName'),
            'email' => session('userEmail'),
            'role_id' => 1
        ]);
    }
    else {
        return redirect('/users')->with('error', 'User aleady exists!);
    }
} 

The problem:

I wanted to add a success confirmation message of "User created successfully". For this purpose, I did:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if (!$user) {
    return User::create([
        'first_name' => session('firstName'),
        'last_name' => session('lastName'),
        'email' => session('userEmail'),
        'role_id' => 1
    ])->with('success', 'User created successfully');
}

For a reason I have not been able to find out, this fails. With this message:

Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given, called in

What am I doing wrong?

>Solution :

The problem is that you are returning a User object and not a response.

Try this:

if (!$user) {
        User::create([
            'first_name' => session('firstName'),
            'last_name' => session('lastName'),
            'email' => session('userEmail'),
            'role_id' => 1
        ]);
        return redirect('/users')->with('success', 'User created successfully');
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading