简体   繁体   中英

How to redirect into another page after inserting form data in Laravel?

I have a form with some input field.Now i want, if anyone submit this form then he will redirect to another page where i have written confirmation letter.I have searched but i can not find right answer of my issue.

My controller:

public function store(Request $request)
{
    $applicant = new Applicant();
    $applicant['name'] = $request->input('name');
    $applicant['sex'] = $request->input('sex');
    $applicant['marital_status'] = $request->input('marital_status');
    $applicant['date_of_birth'] = $request->input('date_of_birth');
    $applicant['email'] = $request->input('email');
    $applicant['fathers_name'] = $request->input('fathers_name');
    $applicant['mothers_name'] = $request->input('mothers_name');
    $applicant['spouses_name'] = $request->input('spouses_name');
    $applicant['nationality'] = $request->input('nationality');
    $applicant['present_add'] = $request->input('present_add');

    $applicant->save();
    \Session::flash('flash_message','Application has been successfully submitted.');
    return redirect(route('confirmationMsg'));
}

My Route:

Route::group(['prefix' => 'career'], function () {
    Route::get('apply', ['as' => 'addApplicant', 'uses' => 'ApplicantController@create']);
    Route::post('save', ['as' => 'saveApplicant', 'uses' => 'ApplicantController@store']);
    Route::get('submitted', ['as' => 'confirmationMsg', 'uses' => 'ApplicantController@store']);
});

I have got an error with this code:

QueryException in C:\\xampp\\htdocs\\NoticeBoard\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php line 729: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into applicants ( name , sex , marital_status

Could anyone help me where is the problem or how i do that? Thanks in advance.

Try this:

return redirect('your_route');

Route::get('your_route', function () {
    // return view from here or make a function call from a controller
});

尝试这个:-

return redirect()->route('confirmationMsg');

请尝试以下一种方法:

return Redirect::to(url);

I can see three answers which are saying about redirect() and route() usage, but your code is correct here. You even accepted one of these answers (why?).

The error says name (and probably few other) fields are empty, so you need to be sure you're passing those fields from the form. You can check this by using this code right in the beginning of the store() function:

dd($request->all());

Alternatively you can set one of few of these fields as nullable() in migration file:

$table->string('name')->nullable();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM