Reputation: 117
I am trying to redirect the page into form or home after the submition the data and successful insertion of data in laravel but the error is showing that page not found. Please help me. My controller code is given testing.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Request;
use App\test;
class testing extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('create');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//store data in database
test::create(Request::all());
echo "data enserted Successfully";
?><a href="<?php return redirect()->action('HomeController@index');?>">Go Back Home</a><?php
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
And the view file of form which is create.blade.php is given
@extends('master')
@section('content')
<div class="container"><br>
<h1 class="text-success text-center">Student Registration Form</h1><br>
<div class="col-md-offset-3 col-md-6 m-auto d-block">
<form action="save" method="post" onsubmit="return validation()">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="form-group">
<label>Student Name: </label>
<input type="text" name="sname" id="sname" class="form-control">
<span id="studenterror" class="text-danger font-weight-bold"></span>
</div>
<div class="form-group">
<label>Father Name: </label>
<input type="text" name="fname" id="fname" class="form-control">
<span id="fnameerror" class="text-danger font-weight-bold"></span>
</div>
<div class="form-group">
<label>Graduation Year: </label>
<input type="date" name="gyear" id="gyear" class="form-control">
<span id="gyearerror" class="text-danger font-weight-bold"></span>
</div>
<div class="form-group">
<label>Phone: </label>
<input type="number" name="phone" id="phone" class="form-control">
<span id="phoneerror" class="text-danger font-weight-bold"></span>
</div>
<div class="form-group">
<label>Email: </label>
<input type="email" name="email" id="email" class="form-control">
<span id="emailerror" class="text-danger font-weight-bold"></span>
</div>
<div class="form-group">
<label>Postal Address: </label>
<input type="text" name="paddress" id="paddress" class="form-control">
<span id="addresserror" class="text-danger font-weight-bold"></span>
</div>
<div class="form-group">
<label>Program: </label>
<select class="form-control" name="program">
<option value="Nill">Select Program</option>
<option value="msc">MSc</option>
<option value="bs">BS</option>
<option value="mphil">MPhil</option>
<option value="phd">PHD</option>
</select>
</div>
<div>
<label>Job</label><br>
<div class="form-control radio-inline">
<div class="col-md-4">
<label>
<input type="radio" name="job" value="yes" data-toggle="collapse" data-target="#org">Yes</label>
<label>
<input type="radio" name="job" value="no" data-toggle="collapse" data-target="#">No </label>
</div>
</div>
<br>
<div id="org" class="collapse">
<div class="form-group">
<label>Organization: </label>
<select class="form-control" name="org">
<option value="Nill">Select Organization</option>
<option value="Higher_Education">Higher Education</option>
<option value="Software_House">Software House</option>
<option value="Hardware_industry">Hardware Industry</option>
<option value="other">Other</option>
</select>
</div>
<label>Position</label>
<input type="text" name="position" class="form-control" id="position">
</div>
</div><br>
<div>
<input type="submit" name="Submit" class="btn btn-lg col-md-offset-3 col-md-6 m-auto d-block">
</div>
</form>
</div>
</div>
<h4 align="center">---------------------------------------------------<br>
First Task of Project Learning</h4>
<br>
<a href="{{url('/plearning')}}" class="btn btn-primary">Back to Home</a>
@endsection
I want to redirect plearning view in HomeController@index and its code is given
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
//
public function index(){
return view('plearning');
}
}
Please correct my code and help me. Thanks in advance
Upvotes: 0
Views: 2497
Reputation: 31
You were almost right
public function store(Request $request)
{
//store data in database
test::create(Request::all());
return redirect()->action('HomeController@index');
}
Controller is not for HTML. You have tried to add a Button for redirection in the controller, which is not recommended.
For redirection in Laravel, we have redirect() helper_function.
If you still have "Page not found" error, make sure you have "plearning.blade.php" in view directory (default: resource/views).
Upvotes: 1
Reputation: 4112
To redirect after a post you should update your store method to following:
public function store(Request $request)
{
//store data in database
Test::create($request->all());
return redirect()->route('test.index');
}
I am not sure how you defined your routes if this test.index does not work post your routes to your question.
Your test route should look something like this:
Route::group(['prefix' => 'test', 'as' => 'test.'], function () {
Route::get('/', ['as' => 'index', 'uses' => 'TestController@index']);
Route::get('create', ['as' => 'create', 'uses' => 'TestController@create']);
});
Upvotes: 2