I am trying to inserting data into database but, I am failing to do it. I have tried ample of videos and internet surf but not successful. My code is following I have created controller and passing request from controller it is not accepting.
<form action="/StudentInsert" method="post">
<div class="form-group">
<div class="col-sm-10">
<p>
<input type="text" name="txtFname" class="form-control" placeholder="First name">
<input type="text" name="txtLname" class="form-control" placeholder="Last name">
</p>
</div>
<div class="col-sm-10">
<p>
<input type="text" name="txtContact" class="form-control" placeholder="Contact">
<input type="text" name="txtEmail" class="form-control" placeholder="Email">
</p>
</div>
<div class="col-sm-10">
<p>
<button type="submit" name="btnSubmit" class="btn btn-outline-primary">Submit</button>
</p>
</div>
</div>
</form>
this is my controller file
<?php
namespace AppHttpControllers;
use AppHttpControllersController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
class StudInsertController extends Controller
{
//Insert Form
public function StudentInsertForm()
{
return view('StudInsert');
}
public function StudentInsert(Request $request)
{
$first_name = $request->input('txtFname');
$last_name = $request->input('txtLname');
$contact = $request->input('txtContact');
$email = $request->input('txtEmail');
$data = array('first_name' => $first_name, 'last_name' => $last_name, 'contact' => $contact, 'email' => $email);
DB::table('student')->insert($data);
echo "Record inserted Successfully.<br/>";
echo '<a href="/StudView">Click Here</a>';
}
}
web.php
<?php
use IlluminateSupportFacadesRoute;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('StudInsert');
});
Route::get('StudentInsertForm', 'AppHttpControllersStudentInsertController@StudentInsertForm');
Route::get('StudentInsert', 'AppHttpControllersStudentInsertController@StudentInsert');
2
Answers
You have to use the post route instead of get in web.php file to retrieve post data like this
Replace below line in web.php
with this new one
Would be great you provide the exact error you are getting.
But I can see a couple of bugs already.
You are trying to send a post request to your Laravel app from you form, but in your routes list you are not waiting for any post request.
You can do following:
web.php
:More details in Laravel docs
I would also recommend using Laravel form helpers