laravel post is not working - php

I'am working on simple laravel project where user have a registration page.For this I have used two methods register(get method) and postregister(post method).
My code:
public function register() {
return view ( 'auth/register' );
}
public function postregister(Request $req) {
dd("sowmya");
$user = new User ();
$user->name = $req->get ( 'username' );
$user->email = $req->get ( 'email' );
dd($user);
$user->password = Hash::make ( $req->get ( 'password' ) );
$user->remember_token = $req->get ( '_token' );
$user->save ();
return redirect ( '/login' );
}
Get method is working fine.But after submitting form the post method is not calling.even I tried with printing dd("hii") but not displayed. Can someone help me?
#extends('layout') #section('content')<style>
<style>
nav {
visibility: hidden;
}
</style>
<h3 style="margin-top: 70px;" align="center">Signup Page</h3>
<div class="container">
<div class="row">
<div class="register is-Responsive"
style="background-color: rgba(0, 0, 0, 0.3)">
<div id="logo-container"></div>
<div class="col-sm-12 col-md-10 col-md-offset-1">
<form action="/register" id="loginForm" method="post">
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li> #endforeach
</ul>
</div>
#endif {!! csrf_field() !!}
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></i></span>
<input class="form-control" type="text" name='username'
placeholder="username" />
</div>
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input class="form-control" type="email" name='email'
placeholder="email" />
</div>
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-key"></i></span> <input
class="form-control" type="password" name='password'
placeholder="password" />
</div>
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-key"></i></span> <input
class="form-control" type="password" name='password_confirmation'
placeholder="confirm password" />
</div>
<div class="checkbox">
<label> <input type="checkbox"> I agree to the <a href="#">Terms
and Conditions</a>
</label>
</div>
<div class="form-group">
<button type="submit" class="btn btn-def btn-block">Signup</button>
</div>
</form>
</div>
</div>
</div>
#endsection

In normal case, without middleware,
Routes:
Route::post ( '/register', 'LoginController#postregister' );
In view:
<form action="{{ URL::to('/register') }}" method="post"></form>
In LoginController:
public function postregister(Request $req)
{
print_r("Came here");die;
}
Change the button tag to input:
<input type="submit" value="Submit" class="btn btn-def btn-block"></input>
Add this inside your form :
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Try this.Good luck.

Related

Laravel 7 - How to display properly the old input/s?

I am trying to display the old/previous inputs when its validation failed before submitting the form inputs/values to Controller's validation but it only redirects back to the page with all input fields in form is empty or not showing the previous inputs.
Here's my controller code inside store function (SaleController):
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'invoice_no' => 'required|unique:sales_h|max:255',
]);
if($validator->fails()){
//dd($validator);
Toastr::warning('Invoice No. cannot be repeated or blank.', 'Warning');
return redirect()->back()->withInput($request->all);
}
/* ...... */
}
Here's the snippet source code for my view blade file (create.blade.php):
<form class="form-horizontal" method = "POST" action = "{{ route('sales.store') }}">
#csrf
<div class="card-body">
#if ($errors->any())
<div class="alert alert-danger">
<ul class="mb-0">
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-group row">
<div class="col-md-12">
<div class="alert alert-info alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h5><i class="icon fas fa-info" d></i> Info!</h5>
A sales order is a document generated by the seller upon receiving a
purchase order from a <strong>buyer/customer</strong> specifying the details about the product or service
along with price, quantity, buyer details.
</div>
<strong><font color="red">*</font> Indicates required fields.</strong>
</div>
<div class="col-md-4">
<label for="date" class="col-sm-6 col-form-label">Date <font color="red">*</font></label>
<div class="input-group mb-2 {{$errors->has('date') ? 'has-error' : ''}}" >
<div class="input-group-prepend">
<span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
</div>
<input type="text"
class="form-control"
id="date"
name = "date"
autocomplete="off"
value="{{ old('date') }}"
required>
</div>
</div>
<div class="col-md-4">
<label for="invoice_no" class="col-sm-6 col-form-label">Invoice No. <font color="red">*</font></label>
<div class="input-group mb-2">
<div class="input-group-prepend {{$errors->has('invoice_no') ? 'has-error' : ''}}">
<span class="input-group-text"><i class="fas fa-hashtag"></i></span>
</div>
<input type="text"
class="form-control"
id="invoice_no"
name = "invoice_no"
maxlength = "15"
value="{{ old('invoice_no') }}"
onkeyup="this.value=this.value.replace(/[^\d]/,'')"
required>
</div>
</div>
<div class="col-md-4">
<label for="shop_name" class="col-sm-6 col-form-label">Customer Name <font color="red">*</font></label>
<div class="input-group mb-2">
<div class="input-group-prepend {{$errors->has('shop_name') ? 'has-error' : ''}}">
<span class="input-group-text"><i class="fas fa-hashtag"></i></span>
</div>
<input type = "text"
class="form-control"
id="shop_name"
name = "shop_name"
placeholder = "Customer Name"
autocomplete = "on"
value="{{ old('shop_name') }}"
required>
</div>
</div>
</div>
</div>
<div class = "form-group row">
<div class="col-md-12">
<label for = "info" class = "col-md-4 col-form-label"><strong><i class="fas fa-info-circle"></i></strong> Sales Order Information</label>
<div class = "col-sm-12 {{$errors->has('product_information') ? 'has-error' : ''}}">
<textarea name = "product_information"
class = "form-control"
rows = "4"
value="{{ old('product_information') }}"
></textarea>
</div>
</div>
</div>
<div class = "col-md-12 field-wrapper">
<div class = "form-group row">
<div class="col-md-12">
<label for="id_raw_product" class="col-sm-4 col-form-label">Product Name <font color="red">*</font></label>
</div>
<div class="col-sm-3">
<input type="hidden" readonly = "true" class="form-control" id="id_raw_product_1" name = "id_raw_product[]" placeholder = "Product Name" required>
<input type="text"
readonly = "true"
class="form-control"
id="name_raw_product_1"
name = "name_raw_product[]"
value="{{ old('id_raw_product_1') }}"
placeholder = "Product Name">
</div>
<div class="col-sm-2">
<a href = "/transaction/sales/product/popup_media/1"
class = "btn btn-info"
title = "Product"
data-toggle = "modal"
data-target = "#modal-default">Product</a>
</div>
<div class="col-sm-3">
<input type="number"
class="form-control"
id="price_1"
name = "price[]"
value="{{ old('price_1') }}"
placeholder = "Price"
onkeyup="this.value=this.value.replace(/[^\d]/,'')" required>
</div>
<div class="col-sm-2">
<input type="number"
class="form-control"
id="total_1"
name = "total[]"
value="{{ old('total_1') }}"
placeholder = "Quantity"
onkeyup="this.value=this.value.replace(/[^\d]/,'')" required>
</div>
<div class = "col-sm-2">
<i class = "fas fa-plus"></i>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-default float-right" name = "submit_create" id = "submit_create">Submit</button>
</div>
<!-- /.card-footer -->
</form>
Write your controller code like this -
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'invoice_no' => 'required|unique:sales_h|max:255',
]);
if($validator->fails()){
return redirect()->back()->withInput()->withErrors($validator->messages()->all());
}
}
and in blade file add this code -
#if ($errors->any())
<div class="alert alert-danger">
<ul class="mb-0">
#foreach ($errors->all(':message') as $input_error)
<li>{{ $input_error }}</li>
#endforeach
</ul>
</div>
#endif

Laravel Error: Missing required parameters

Please help me out. I'm having the missing required parameter's error. This is the form script:
<div class="container">
<div class="row">
<!--first column-->
<div class="col-md-6 col-sm-6 ">
<!-- Default form contact -->
<form class="text-center border border-light p-5" action="{{route('updating', $update->id)}}" enctype="multipart/form-data" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}" >
#method('PATCH')
<div class="well">
<p class="h4 mb-4">Edit Updates</p>
</div>
<!-- Name -->
<input type="text" name="title" id="defaultContactFormName" class="form-control mb-2" value=" {{ old('title') ?? $update->title }} ">
<!-- Message -->
<div class="form-group">
<textarea class="form-control rounded-0" name="body" id="exampleFormControlTextarea2" rows="5" value="{{ old('title') ?? $update->body }}"></textarea>
</div>
<!-- Send button -->
<button class="btn btn-success btn-block" type="submit">Publish</button>
</form>
</div>
</div>
</div>
And this is my controller and edit function:
public function update(Request $request, $title)
{
$this->validate($request, [
'title'=>'required',
'body'=>'required'
]);
//Publishing Updates
$update = Update::findOrFail($title);
$update->title = $request->input('title');
$update->body = $request->input('body');
$update->save();
return redirect('admin.updates')->with('success', 'Saved');
}
And this is my route:
Route::patch('/updates/{title}/update', 'UpdatesController#update')->name('updating');
I keep getting the error and i don't know where its coming from.

One form is working while other form is not working in laravel 5.4

I've UserController in which I've two options -
1) For Updating Profile
2) For Updating Password
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\User;
use Auth;
use Hash;
class UserController extends Controller
{
public function profile(){
return view('profile', array('user' => Auth::user()));
}
public function update_avatar(Request $request){
if(isset($request->avatar) && $request->avatar->getClientOriginalName()){
$ext = $request->avatar->getClientOriginalExtension();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->avatar->storeAs('public/avatar',$file);
}
else
{
$user = Auth::user();
if(!$user->avatar)
$file = '';
else
$file = $user->avatar;
}
$user = Auth::user();
$user->avatar = $file;
$user->name = $request->name;
$user->email = $request->email;
$user->mb_number = $request->mb_number;
$user->home_town = $request->home_town;
$user->save();
return view('profile', array('user' => Auth::user()));
}
public function update_password(Request $request){
$user = Auth::user();
if(Hash::check(Input::get('old_password'), $user['password']) && Input::get('new_password') == Input::get('confirm_new_password')){
$user->password = bcrypt(Input::get('new_password'));
$user->save();
}
return view('profile', array('user' => Auth::user()));
}
}
In my view blade, I've two forms -
update_avatar for updating profile like name, phone number and avatar.
update_password for updating password.
</div>
<div class="widget-user-image">
<img class="img-circle" src="{{ asset('public/storage/avatar/'.$user->avatar) }}" alt="User Avatar">
</div>
<div class="box-footer">
<div class="row">
<div class="col-sm-4 border-right">
<div class="description-block">
<h5 class="description-header">{{ $user->email }}</h5>
<span class="description-text">Email</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->
<div class="col-sm-4 border-right">
<div class="description-block">
<h5 class="description-header">{{ $user->name }}</h5>
<span class="description-text">{{ $user->home_town }}</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->
<div class="col-sm-4">
<div class="description-block">
<h5 class="description-header">{{ $user->mb_number }}</h5>
<span class="description-text">Phone No.</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
</div>
<!--
<div class="box-footer no-padding">
<ul class="nav nav-stacked">
<li>Projects <span class="pull-right badge bg-blue">31</span></li>
<li>Tasks <span class="pull-right badge bg-aqua">5</span></li>
<li>Completed Projects <span class="pull-right badge bg-green">12</span></li>
<li>Followers <span class="pull-right badge bg-red">842</span></li>
</ul>
</div>
-->
</div>
</div>
<section class="content">
<div class="container-fluid">
<form action="/profile" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" placeholder="Title" value="{{$user->name}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" id="email" placeholder="Description" value="{{$user->email}}" readonly>
</div>
<div class="form-group">
<label for="mb_number">Mobile No.</label>
<input type="text" name="mb_number" class="form-control" id="mb_number" placeholder="Schedule" value="{{$user->mb_number}}">
</div>
<div class="form-group">
<label for="home_town">Home Town</label>
<input type="text" name="home_town" class="form-control" id="home_town" placeholder="Deadline" value="{{$user->home_town}}">
</div>
<div class="form-group">
<label>Update Profile Image</label>
<input type="file" name="avatar">
#if($user->avatar)
<img src="{{ asset('public/storage/avatar/'.$user->avatar) }}" style="width:150px;">
#endif
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}"
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</section>
<section class="content">
<div class="container-fluid">
<form action="/profile" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="old_password">Old Password</label>
<input type="password" name="old_password" class="form-control" id="old_password" placeholder="Old Password">
</div>
<div class="form-group">
<label for="new_password">New Password</label>
<input type="password" name="new_password" class="form-control" id="new_password" placeholder="New Password">
</div>
<div class="form-group">
<label for="confirm_new_password">Confirm New Password </label>
<input type="password" name="confirm_new_password" class="form-control" id="confirm_new_password" placeholder="Confirm New Password">
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}"
<button type="submit" class="btn btn-primary">Update Password</button>
</div>
</div>
</section>
update_password function is working fine but update_avatar function is not working neither it's showing any error. I've tried dd($user) but still not giving output to dd.
Check this out i updated form a bit with indentation and proper closing, also please redirect profile avatar to different URL so that it can access different method :-
I would suggest you to use https://laravelcollective.com/docs/5.4/html , by using form collective it would be much more easier to implement more complex form structure.
In Route,
Route::post('/profile_avatar',"UserController#update_avatar");
Route::post('/profile_password',"UserController#update_password");
In Blade,
<section class="content">
<div class="container-fluid">
<form action="/profile_avatar" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="name">
Name
</label>
<input class="form-control" id="name" name="name" placeholder="Title" type="text" value="{{$user->name}}">
</input>
</div>
<div class="form-group">
<label for="email">
Email
</label>
<input class="form-control" id="email" name="email" placeholder="Description" readonly="" type="text" value="{{$user->email}}">
</input>
</div>
<div class="form-group">
<label for="mb_number">
Mobile No.
</label>
<input class="form-control" id="mb_number" name="mb_number" placeholder="Schedule" type="text" value="{{$user->mb_number}}">
</input>
</div>
<div class="form-group">
<label for="home_town">
Home Town
</label>
<input class="form-control" id="home_town" name="home_town" placeholder="Deadline" type="text" value="{{$user->home_town}}">
</input>
</div>
<div class="form-group">
<label>
Update Profile Image
</label>
<input name="avatar" type="file">
#if($user->avatar)
<img src="{{ asset('public/storage/avatar/'.$user->avatar) }}" style="width:150px;">
#endif
</img>
</input>
</div>
<input <a="" class="btn btn-info" href="" name="_token" type="submit" value="{{ csrf_token() }}"/>
</div>
<button class="btn btn-primary" type="submit">
Update
</button>
</form>
</div>
</section>
<section class="content">
<div class="container-fluid">
<form action="/profile_password" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="old_password">
Old Password
</label>
<input class="form-control" id="old_password" name="old_password" placeholder="Old Password" type="password">
</input>
</div>
<div class="form-group">
<label for="new_password">
New Password
</label>
<input class="form-control" id="new_password" name="new_password" placeholder="New Password" type="password">
</input>
</div>
<div class="form-group">
<label for="confirm_new_password">
Confirm New Password
</label>
<input class="form-control" id="confirm_new_password" name="confirm_new_password" placeholder="Confirm New Password" type="password">
</input>
</div>
<input <a="" class="btn btn-info" href="" name="_token" type="submit" value="{{ csrf_token() }}"/>
</div>
<button class="btn btn-primary" type="submit">
Update Password
</button>
</form>
</div>
</section>

Laravel - Refresh only a section instead of refresh the whole page on submit form

I create a contact form on the home page of the website.
This is the code:
<section id="contact" class="light-grey-background m-top-30">
<div class="container">
<header>
<h2 class="title-contact text-center">
<b>CONTATTACI</b>
</h2>
</header>
#if (count($errors) > 0)
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">x</button>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form id="formEmail" class="m-top-40" method="post" action="{{ url('/send') }}">
{{ csrf_field() }}
<div class="form-row">
<div class="col-sm-12">
<div class="form-group">
<input id="oggetto" name="oggetto" type="text" placeholder="Oggetto" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input id="nome" name="nome" type="text" placeholder="Nome" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input id="cognome" name="cognome" type="text" placeholder="Cognome" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input id="email" name="email" type="email" placeholder="Email" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<textarea id="messaggio" rows="6" placeholder="Scrivi qui il tuo messaggio..." name="messaggio" class="form-control"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="send" class="btn btn-primary contact m-bottom-70 text-center" value="Send" />
</div>
</div>
</div>
</form>
</div>
</section>
This is the HomeController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
function index()
{
return view('home');
}
function send(Request $request)
{
$this->validate($request, [
'oggetto' => 'required',
'nome' => 'required',
'cognome' => 'required',
'email' => 'required|email',
'messaggio' => 'required'
]);
}
}
And this is the route for the home page:
Route::get('/', 'HomeController#index');
Route::post('/send', 'HomeController#send');
The problem is that when you click on the send button the page is completely updated and to know the status of the form you have to scroll the page. Is there a way to avoid the problem and update only the section that contains this?

How to update a database table in laravel 5.2

i created one project in laravel 5.2. In that "myform.blade.php" page , created form for register the user. after registration it will show the current user in table format("index.blade.php"). There i given two dynamic button like the drop inside the table. One for Edit and other for Edit/View. When i click on delete button it will delete the corresponding row from the database by taken Primary key(employeeID) as reference id, it's working properly. if i click on Edit/View button it will redirect to "edit.blade.php". There i created same form as in myform.blade.php. If we want to edit the details we can edit from there. I can able to fetch the data from database to the form that i created in the "edit.blade.php". But i don't know how to update the data from their without inserting the same data again(It is not possible, because it will create Integrity constraint violation by trying to insert duplicate primary key. Can any one please tell me how to do updation. Replies are appreciable.
"myform.blade.php" is
#extends('app')
#section('content')
<div class="templatemo-content-wrapper">
<div class="container">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">Employee Form</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">Employee Form</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('myform/myform/') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Employee ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="employeeID" value="{{ old('employeeID') }}" placeholder="Enter employee ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">E_number</label>
<div class="col-md-6">
<input type="text" class="form-control" name="employeeNo" value="{{ old('employeeNo') }}" placeholder="Enter employee number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="Cname" value="{{ old('Cname') }}" placeholder="Enter Contact Name">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">DOB</label>
<div class="col-md-6">
<input type="date" class="form-control" name="dob" value="{{ old('dob') }}" placeholder="Enter date of birth">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Contact Phone</label>
<div class="col-md-6">
<input type="text" class="form-control" name="phoneNumber" value="{{ old('phoneNumber') }}" placeholder="Enter Mobile Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Address</label>
<div class="col-md-6">
<input type="text" class="form-control" name="address" value="{{ old('address') }}" placeholder="Enter Address">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning">
Save
</button>
</div>
</div>
view Data
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
"index.blade.php" is
#extends('app')
#section('content')
<div class="templatemo-content-wrapper" xmlns="http://www.w3.org/1999/html">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">user information</li>
</ol>
<div class="templatemo-content">
<h1>View/Edit user information</h1>
<div>
<div>
<div>
<table id="example" class="table table-striped table-hover table-bordered" bgcolor="#fff8dc">
<thead>
<tr>
<th>Employee ID</th>
<th>Employee No</th>
<th>Contact Name</th>
<th>Date of birth</th>
<th>Mobile number</th>
<th>address</th>
</tr>
</thead>
<tbody>
{{--{{ UserController::getIndex() }}--}}
#foreach($employer as $emp)
<tr>
<td>{{ $emp->employeeID }}</td>
<td>{{ $emp->employeeNo }}</td>
<td>{{ $emp->Cname }}</td>
<td>{{ $emp->dob }}</td>
<td>{{ $emp->phoneNumber }}</td>
<td>{{ $emp->address }}</td>
<td>
{{--#if ( in_array($nam->isActive, array('Yes','No')) )--}}
<div class="btn-group">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
{{--#if ($nam->isActive == 'Yes')--}}
<li data-toggle="modal" data-target="#acceptModal" data-bookingid="{{ $emp->employeeID }}">View/ Edit
</li>
{{--#endif--}}
<li>Delete</li>
</ul>
</div>
{{--#endif--}}
</td>
</tr>
#endforeach
</tbody>
</table>
{{$employer->links()}}
</div>
</div>
</div>
</div>
</div>
{{-- <input type="submit" id="add" name="add" value="Edit" class="button">--}}
</br>
<h4>Create a new Employee</h4>
{{--<form class="templatemo-preferences-form" role="form" method="POST" action="{{ action('UserController#save') }}">--}}
{{--<input type="hidden" name="_token" value="{{ csrf_token() }}">--}}
<form role="form" method="POST" action="{{ url('myform/index') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6 margin-bottom-15">
<input type="text" class="form-control" name="employeeID" value="{{ old('employeeID') }}" placeholder="Enter employee ID">
</div>
<div class="row templatemo-form-buttons">
<div class="submit-button">
<button type="submit" class="btn btn-primary">New</button>
</div>
</div>
</div>
</form>
{{--</form>--}}
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable();
} );
</script>
#endsection
"edit.blade.php" is
#extends('app')
#section('content')
<div class="templatemo-content-wrapper">
<div class="container">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">Employee Form</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">Employee Form</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('myform/myform/') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($user as $use)
<div class="form-group">
<label class="col-md-4 control-label">Employee ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="employeeID" value="{{ $use->employeeID }}" placeholder="Enter employee ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">E_number</label>
<div class="col-md-6">
<input type="text" class="form-control" name="employeeNo" value="{{ $use->employeeNo}}" placeholder="Enter employee number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="Cname" value="{{ $use->Cname }}" placeholder="Enter Contact Name">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">DOB</label>
<div class="col-md-6">
<input type="date" class="form-control" name="dob" value="{{ $use->dob }}" placeholder="Enter date of birth">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Contact Phone</label>
<div class="col-md-6">
<input type="text" class="form-control" name="phoneNumber" value="{{ $use->phoneNumber }}" placeholder="Enter Mobile Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Address</label>
<div class="col-md-6">
<input type="text" class="form-control" name="address" value="{{ $use->address }}" placeholder="Enter Address">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning"><a href="{{ url('myform/update/'.$use->employeeID) }}">
Update</a>
</button>
</div>
</div>
{{-- <input type="button" id="add" name="add" value="View data" class="button">--}}
#endforeach
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
"myformController.php" is
<?php
namespace App\Http\Controllers;
use App\myform;
use Mail;
use Illuminate\Support\Facades\DB;
use Faker\Provider\DateTime;
use App\User;
use App\Http\Requests\createUserRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\Request;
class myformController extends Controller
{
public $type = 'myform';
public function getIndex()
{
// $user = DB::table('user')->get();
$employer = DB::table('employee')->simplePaginate(5);
return view('myform.index')->with('employer',$employer);
}
public function formInsert()
{
$postform = Input::all();
//insert data into mysql table
$data = array('employeeID'=> $postform['employeeID'],
'employeeNo'=> $postform['employeeNo'],
'Cname'=> $postform['Cname'],
'dob'=> $postform['dob'],
'phoneNumber'=> $postform['phoneNumber'],
'address'=> $postform['address'],
);
// echo print_r($data);
$ck = 0;
$ck = DB::table('employee')->Insert($data);
//echo "Record Added Successfully!";
$employer = DB::table('employee')->simplePaginate(10);
return view('myform.index')->with('employer',$employer);
}
public function delete($id)
{
DB::table('employee')->where('employeeID', '=', $id)->delete();
$employer = DB::table('employee')->simplePaginate(10);
return view('myform.index')->with('employer', $employer);
}
public function formIDinsert()
{
$postform = Input::all();
//insert data into mysql table
$data = array('employeeID'=> $postform['employeeID'],
);
// echo print_r($data);
$ck = 0;
$ck = DB::table('employee')->Insert($data);
//echo "Record Added Successfully!";
$employer = DB::table('employee')->simplePaginate(10);
return view('myform.index')->with('employer',$employer);
}
public function edit($id)
{
try {
//Find the user object from model if it exists
$user=DB::table('employee')->where('employeeID', '=', $id)->get();
//$user = User::findOrFail($id);
//Redirect to edit user form with the user info found above.
return view('myform.edit')->with ('user', $user);
//return view('myform.edit')->with('user', myform::find($id));
} catch (ModelNotFoundException $err) {
//redirect to your error page
}
}
// Update user
public function update(Request $request, $id)
{
try{
//Find the user object from model if it exists
$user= myform::findOrFail($id);
DB::table('employee')
->where('employeeID', $id)
->update(['employeeNo' =>$request['employeeNo'],
'Cname'=>$request['Cname'],
'phoneNumber'=>$request['phoneNumber'],
'address'=>$request['address']
]);
//Set user object attributes
//the $request index should match your form field ids!!!!!
//you can exclude any field you want.
// $user->employeeNo = $request['employeeNo'];
// $user->Cname = $request['Cname'];
// $user->phoneNumber = $request['phoneNumber'];
// $user->address = $request['address'];
//Save/update user.
$user->save();
return view('myform.index')->with('user', $user);
//redirect to somewhere
}
catch(ModelNotFoundException $err){
//Show error page
}
}
}
model "myform.php" is
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class myform extends Model
{
protected $table = 'employee';
//protected $primaryKey = 'employeeID';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'employeeID',
'employeeNo',
'Cname',
'dob',
'phoneNumber',
'address',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Routes.php is
Route::any('myform', function()
{
return view('myform/myform');
});
Route::any('myform/myform', 'myformController#formInsert');
Route::any('myform/index', 'myformController#getIndex');
//to show edit form and fetch passed user id info from db
Route::get('myform/edit/{id}', 'myformController#edit');
//to get the edited info and save it to db
Route::get('myform/update/{id}', 'UserController#update');
Route::any('myform/index', 'myformController#formIDinsert');
Route::any('myform/delete/{id}', 'myformController#delete');
You wanna change this in your route:
Route::get('myform/update/{id}', 'UserController#update');
to
//Because the data in your form is transferred/submitted by post request
Route::post('myform/update/{id}', 'UserController#update');
then change your update function to this
// Update user
public function update(Request $request, $id)
{
try{
//Find the user object from model if it exists
$user= myform::findOrFail($id);
//$request contain your post data sent from your edit from
//$user is an object which contains the column names of your table
//Set user object attributes
$user->employeeNo = $request['employeeNo'];
$user->Cname = $request['Cname'];
$user->dob = $request['dob'];
$user->phoneNumber = $request['phoneNumber'];
$user->address = $request['address'];
// Save/update user.
// This will will update your the row in ur db.
$user->save();
return view('myform.index')->with('user', $user);
}
catch(ModelNotFoundException $err){
//Show error page
}
}
If you have any question or need clarification you are most welcome to ask :)
Update in your edit view
change this
<form class="form-horizontal" role="form" method="POST" action="{{ url('myform/myform/') }}">
to
<form class="form-horizontal" role="form" method="POST" action="{{ url('myform/update/').$user->employeeID}}">
change $user->employeeID to whatever is your primary key.
Also change this
<button type="submit" class="btn btn-warning"><a href="{{ url('myform/update/'.$use->employeeID) }}">
Update</a>
</button>
to this
<button type="submit" class="btn btn-warning">Update</button>
Aso you didn't fix your route as I mentioned earlier in this answer.

Categories