'How do I redirect to another page after form submission in Laravel

form

When i submit the form it redirects back to the form itself, can anyone help me?

    <form action="/jisajili" method="POST">
        @csrf
        <div class="card-panel z-depth-5">
            <h5 class="center red-text">Jiunge Nasi</h5>

            <div class="input-field">
                <i class="material-icons prefix">account_circle</i>
                <input type="text" name="username" class="validate">
                <label>Jina lako</label>
            </div>

            <div class="input-field">
                <i class="material-icons prefix">phone</i>
                <input type="number" name="phone" class="validate">
                <label>Nambari ya simu</label>
            </div>

             ....

            </p>
            <input type="submit" name="submit" value="Jiunge" class="btn left col s12 red">

Controller

class registration extends Controller{

     public function create(){
         return view('jisajili.jiunge');
     }

     public function store(Request $request){
         $reg = new regist;

         $reg->jina = $request->input('username');
         $reg->simuNumber = $request->input('phone');
         $reg->email = $request-> input('email');
         $reg -> password = bcrypt($request->input('password'));
         $cpassword = $request -> input('cpassword');

         $reg->save();

         $validated = $request->validate([
             'name' => 'required|unique:posts|max:10',
             'body' => 'required',
         ]);
         
         return redirect('home');
         
     }
}


Solution 1:[1]

What I would do is first check for the data requirements before you add the object to the database. Also I would add the columns of the models into the Model file to use the Object::create function with an array parameter.

I recomment to use routing in your blade file. I noticed you used action="/route". What you want to do is naming your routes with ->name('route_name') in the route files. To use them in your blade files with the global route function route="{{ route('route_name') }}".

<?php

class PostController extends Controller
{
    public function index()
    {
        return view('post.create');
    }

    public function store(\Illuminate\Http\Request $request)
    {
        $validator = Validator::make(
            $request->all(),
            [
                'name' => 'required|unique:posts|max:10',
                'body' => 'required'
            ]
        );

        // Go back with errors when errors found
        if ($validator->fails()) {
            redirect()->back()->with($validator);
        }

        Post::create(
            [
                'name' => $request->get('name'),
                'body' => $request->get('body')
            ]
        );

        return redirect()
            ->to(route('home'))
            ->with('message', 'The post has been added successfully!');
    }
}

What you can do after this is adding custom errors into the controller or add them into your blade file. You can find more about this in the documentation of Laravel.

Solution 2:[2]

it redirects you back because of validation error.

change password confirmation name from cpassword into password_confirmation as mentioned in laravel docs https://laravel.com/docs/7.x/validation#rule-confirmed

update your controller into:

public function store(Request $request){

         $validated = $request->validate([
             'username' => 'required',
             'phone' => 'required',
             'email' => 'required',
             'password' => 'required|confirmed'
         ]);

         $reg = new regist;
         $reg->jina = $request->input('username');
         $reg->simuNumber = $request->input('phone');
         $reg->email = $request-> input('email');
         $reg -> password = bcrypt($request->input('password'));

         $reg->save();
         return redirect('home');
         
     }

in your blade add the following to display validation errors:

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 N. Hamelink
Solution 2