I have been trying to use modal to create and edit entries in Laravel. I am using simple modal instead of ajax. I can add entries, but when I try to edit an entry, it only loads the last entry in the modal.
Here is my index (from where I am calling modal)
#foreach ($clients as $client)
<tr>
<td>{{ $client->name }}</td>
<td><button data-target="modal2" class="btn modal-trigger1">Edit</button></td>
</tr>
#endforeach
Here is edit file (modal file)
<div id="modal2" class="modal">
<div class="panel panel-default">
<div class="modal-content">
<p class="flow-text">Edit client</p>
<form class="form" role="form" method="POST" action="{{ url('/client/'. $client->id) }}">
<input type="hidden" name="_method" value="patch">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="name" value="{{ $client->name }}">
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</form>
</div>
</div>
</div>
And the edit in controller is
public function edit($id)
{
$clients = Client::findOrFail($id);
return view('client.edit', compact('clients'));
}
It only loading the last entry in the table, not the entry id in the row.
I am using Laravel 5.3. Please help
Set data to your edit link
#foreach ($clients as $client)
<tr>
<td>{{ $client->name }}</td>
<td>Edit</td>
</tr>
#endforeach
//On edit button click get data and set data to modal
$(document).on('click', '.edit-city-link', function(){
var client_id = $(this).data('client-id');
var client_name = $(this).data('client-name');
$('#client-id').val(client_id);
$('#client-name').val(client_name);
$('#edit-client-modal').modal('show');
});
//This is modal
<div id="modal2" class="modal">
<div class="panel panel-default">
<div class="modal-content">
<p class="flow-text">Edit client</p>
<form id="edit-client-modal" class="form" role="form" method="POST" action="{{ url('/client/edit') }}">
<input type="hidden" name="_method" value="patch">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="control-label">Name</label>
<input id="client-id" type="hidden" class="form-control" name="client-id">
<input id="client-name" type="text" class="form-control" name="name">
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</form>
</div>
</div>
</div>
This is your controller method
public function edit($request_data)
{
$obj_client = Client ::find($request_data['client-id']);
$obj_client = $request_data['client-name'];
$obj_client->save();
}
Add {{$client->id}} to your data-target in the button and also add it to your modal id :-
<td><button data-target="modal2 {{$client->id}}" class="btn modal-trigger1">Edit</button></td>
<div id="modal2 {{$client->id}}" class="modal">
Finally, put the modal inside the foreach loop.
Related
i have a function which sync api, i have to manually trigger the function from the front end by passing some parameters .
Can someone help me with script or a way to automatically sync the function after some specific time interval.
Code for frontend is below
<form
role="form"
method="POST"
action="{{ url('/admin/apisync/show') }}">
{{ csrf_field() }}
#if( ! $apis->isEmpty() )
<fieldset class="scheduler-border">
<div class="form-group{{ $errors->has('profit_percentage') ? ' has-
error' : '' }}">
<input type="text"
class="form-control"
data-validation="number"
placeholder="Profit Percentage"
data-validation-allowing="float"
id="profit_percentage"
value="{{ old('profit_percentage') }}"
name="profit_percentage">
#if ($errors->has('profit_percentage'))
<span class="help-block">
<strong>{{ $errors->first('profit_percentage') }}
</strong>
</span>
#endif
</div>
<div class="form-group">
<label for="sync_action" class="control-label">Sync Actions</label>
<label class="checkbox-inline">
<input type="checkbox"
style="margin: 0; margin-left: -20px"
name="sync_action[]"
checked
value="remove"> Remove
</label> <span class="fas fa-info-circle" data-
toggle="popover" data-trigger="hover" data-content="This will remove packages which exist
at your panel but have been removed at host"></span>
</div>
<div class="form-group" style="float:right">
<button type="submit" id="btn-proceed" class="btn btn-primary">Sync
Packages</button>
</div>
</fieldset>
#else
<span class="help-block">
<strong>No API is configured. Please update your API
configurations.</strong>
</span>
#endif
</form>
Working on a form which has got 3 phases. In phase 1 the user fills a form which is submitted to the backend for validation, some data computation and later passed to the 2nd phase where the user triggers a button which has a dynamic id. Next, I use AJAX to fetch the values the user entered in the 1st phase of the form to a final controller.
I store all the values the user entered in a PHP variable called oldata but when I try json_encode() the variable inside the AJAX code and log the values in console tab I dont get the data from the form.
Phase 1
<form method="POST" action="{{ route('b2c.getplans') }}" class="form-contact" accept-charset="UTF-8">
<div class="form-line{{ $errors->has('FirstName') ? ' has-error' : '' }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text-area" class="form-input" name="FirstName" id="FirstName" value="{{ old('FirstName') }}" required>
<label>First Name *</label>
<div class="error-label">Field is required!</div>
<div class="check-label"></div>
#if ($errors->has('FirstName'))
<span class="help-block">
<strong>{{ $errors->first('FirstName') }}</strong>
</span>
#endif
</div>
<div class="form-line{{ $errors->has('MiddleName') ? ' has-error' : '' }}">
<input type="text-area" class="form-input" name="MiddleName" id="MiddleName" value="{{ old('MiddleName') }}" required>
<label>Middle Name *</label>
<div class="error-label">Field is required!</div>
<div class="check-label"></div>
#if ($errors->has('MiddleName'))
<span class="help-block">
<strong>{{ $errors->first('MiddleName') }}</strong>
</span>
#endif
</div>
<div class="form-line registar love {{ $errors->has('email') ? ' has-error' : '' }}" style="margin-left: 0px;">
<input type="text-area" id="email" class="form-input" name="email" value="{{ old('email') }}" required>
<label>Email *</label>
<div class="error-label">Field is required!</div>
<div class="check-label"></div>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
<div class="form-line {{ $errors->has('phone') ? ' has-error' : '' }}">
<input type="text-area" class="form-input" name="phone" id="phone" value="{{ old('phone') }}" required>
<label>Phone Number *</label>
<div class="error-label">Field is required!</div>
<div class="check-label"></div>
#if ($errors->has('phone'))
<span class="help-block">
<strong>{{ $errors->first('phone') }}</strong>
</span>
#endif
</div>
<div class="form-line {{ $errors->has('dob') ? ' has-error' : '' }}">
<input type="date" class="form-input" name="dob" value="{{ old('dob') }}" required>
<label>Date of Birth *</label>
<div class="error-label">Field is required!</div>
<div class="check-label"></div>
#if ($errors->has('dob'))
<span class="help-block">
<strong>{{ $errors->first('dob') }}</strong>
</span>
#endif
</div>
<button type="submit" class="form-b3c" style="cursor:pointer;"> Get Plans</button>
</form>
Controller to handle phase 1
//Post Request of plan entries
public
function validatePlanEntries(Request $request)
{
$validation = $this->validate($request, [
'FirstName' => 'required|string|min:2',
'MiddleName' => 'required|string|min:2',
'phone' => 'required|string|max:20',
'dob' => 'required|valid_birth_date',
'email' => 'required|string|email|max:255',
]
);
//fetches all the unput values from the form
$oldata = $request->all();
$plans_benefits = view("B2C::travel.plans", compact(oldata))->render();
return $plans_benefits;
}
Phase 2
#if (!empty($plans_benefits))
<div class="container">
<div class="PLAN">
<main class="top">
<div class="row">
#foreach ($plans_benefits as $plan_benefits)
#php
$plan_data = $plan_benefits[0];
$benefits = $plan_benefits[1];
$plan_name = $plan_data->Calculation_TravelPlan->TravelPlan->Name;
#endphp
<div class="card plan">
<h5 class="card-title plan"> {{$plan_name}} </h5>
<img class="card-img-top plan" src="{{asset('assets/images-new/superior.svg')}}" alt="Card image cap">
<div class="card-body">
<div class="travel-plan">
<div class="superior-content">
<table class="table">
<tbody>
#foreach($benefits as $benefit)
<tr>
<td class="plan-title">{{$benefit->name}}</td>
#if($benefit->value == 'true')
<td class="plan-worth"><i class="fas fa-check"></i></td>
#elseif ($benefit->value == 'false')
<td class="plan-worth"><i class="fas"></i></td>
#else
<td class="plan-worth"> {{$benefit->value}} </td>
#endif
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
<!-- Hiden-->
<input type="hidden" value="{{$plan_data->CalculationId}}"" class ="calc_id" name="calc_id" id="calc_id{{$plan_data->CalculationId}}"/>
<input type="hidden" value="{{$plan_name}}" class ="travelplan" name="travelplan" id="plan{{$plan_data->CalculationId}}"/>
<!--Hidden-->
<p class="card-text plan">TOTAL
<span class="amount">$ {{round($plan_data->TravelBasicPremium,2)}}
</span>
</p>
<!-- AJAX call when the button is hit-->
<a id ="{{$plan_data->CalculationId}}" class="plan-quote get_quote" style="cursor:pointer;"><span>Get Quote</span></a>
</div>
</div>
#endforeach
</div>
</main>
</div>
</div>
#endif
AJAX code
$('.PLAN').on('click', '.get_quote', function () {
var olddata = {!! json_encode($oldata) !!};
console.log(oldata);
var entries = oldata.serialize();
$.ajax({
//URL from routes file in Laravel
url: 'getquote',
//GET request
type: 'get',
contentType: 'application/x-www-form-urlencoded',
data: entries + '&calc_id=' + c_id + '&travelplan=' + plan_name,
success: function success(response) {
console.log(response);
},
error: function error(data) {
console.log(data);
}
});
//END AJAX REQUEST
Set dataType in your ajax code.
$.ajax({
url: 'getquote',
type: 'get',
dataType: "json"
...
)};
Hope it will help.
I'm following a tutorial about Authorization in laravel, but it seems it got some errors in the code like this:
"Type error: Argument 2 passed to App\Providers\AuthServiceProvider::App\Providers\{closure}() must be an instance of App\Providers\Post, instance of App\Post given, called in E:\xampp\htdocs\cms\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php on line 323 ◀"
I got those error code when I'm accessing http://cms.dev/posts/edit/2. here's the code that i've put on AuthServicesProfider:
public function registerPostPolicies()
{
Gate::define('create-post', function ($user) {
return $user->hasAccess(['create-post']);
});
Gate::define('update-post', function ($user, Post $post) {
return $user->hasAccess(['update-post']) or $user->id == $post->user_id;
});
Gate::define('publish-post', function ($user) {
return $user->hasAccess(['publish-post']);
});
Gate::define('see-all-drafts', function ($user) {
return $user->inRole('editor');
});
}
and here's the code for the update.blade:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Update Post</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('update_post', ['post' => $post->id]) }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
<label for="title" class="col-md-4 control-label">Title</label>
<div class="col-md-6">
<input id="title" type="text" class="form-control" name="title" value="{{ old('title', $post->title) }}" required autofocus>
#if ($errors->has('title'))
<span class="help-block">
<strong>{{ $errors->first('title') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('body') ? ' has-error' : '' }}">
<label for="body" class="col-md-4 control-label">Body</label>
<div class="col-md-6">
<textarea name="body" id="body" cols="30" rows="10" class="form-control" required>{{ old('body', $post->body) }}</textarea>
#if ($errors->has('body'))
<span class="help-block">
<strong>{{ $errors->first('body') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Update
</button>
#can('publish-post')
<a href="{{ route('publish_post', ['post' => $post->id]) }}" class="btn btn-primary">
Publish
</a>
#endcan
<a href="{{ route('list_posts') }}" class="btn btn-primary">
Cancel
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
I really have no idea about how to fix it since i really don't understand about authorization in Laravel. Thanks.
You're missing a use statement for the Post class, therefore PHP assumes the gate takes the Post class from the current namespace, hence the App\Providers\Post in the error message.
Add the following in the provider class:
use App\Post;
I am using BootstrapDialog to open a edit user form. It opens perfectly.
In my list page when a user click on edit button, it will go to controller and and fetch that user information, and at the end it will show the Dialog Box which have a form and update button.
When he/she click on update button it will call update function of its controller and validate all field and revert back to the dialog box if some error is there. Other wise close the dialog box and show a success message.
My question is when someone left blank to a field and it validate in controller, but it does not able to show the dialog box anymore. How can I show error message to the user in that Dialog box.
I am using laravel 5.4.
Edit Button
<a class="btn btn-xs btn-primary edit_user_"
id="edit_user_<?php echo $v->id; ?>"
data-title="{{ $v->name }}"
href="edit-user/<?php echo $v->id; ?>">
<i class="glyphicon glyphicon-edit" title="Edit" ></i>
Edit
</a>
Js
$('.edit_user_').click(function(e){
e.preventDefault();
var url = $(this).attr('href');
BootstrapDialog.show({
title: $(this).attr('data-title') +"'s "+ 'Information',
message: $('<div></div>').load(url),
closable: true,
closeByBackdrop: false,
closeByKeyboard: false,
draggable: true,
buttons: [{
icon: 'glyphicon glyphicon glyphicon-save',
label: 'Update',
action: function(dialogRef) {
$('form').submit();
},
cssClass: 'btn-primary'
}]
});
return false;
});
Controller
public function updateSelectedUser(Request $request){
$id = $request->input('hId');
$oldImage = $request->input('oldImage');
$validator = $this->validate($request, [
'email' => 'required|email|unique:users,email,'.$id,
'name' => 'required|min:5',
'userimage' => 'required|image'
]);
//$path = Storage::putFile('userimages',$request['userimage']);
if ($validator->fails()) {
return Redirect::back()->with(array('error_code'=>1, 'uId'=>$id));
} else {
echo 'Validation Done';
}
}
And my edit.blade.php template
<div class='row'>
<div class='col-md-12'>
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Edit User</h3>
</div>
<!-- form start -->
<form class="" role="form" method="post" enctype="multipart/form-data" files="true" action="<?php echo asset('regUser/update');?>" >
{{ csrf_field() }}
<input type="hidden" name="hId" value="{{ $info->id }}">
<div class="box-body">
<!-- Name -->
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" >Name</label>
<input id="name" type="text" class="form-control" name="name" value="{{ old('name', $info->name)}}" placeholder="Your Name" required >
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
<!-- Email -->
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" >Email</label>
<input id="email" type="text" class="form-control" name="email" value="{{ old('email', $info->email)}}" placeholder="Your Email" required >
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
<!-- Profile Pic-->
<div class="row">
<div class='col-md-6'>
<div class="form-group">
<label for="userimage" >Image</label>
<input id="userimage" type="file" name="userimage" required>
#if ($errors->has('userimage'))
<span class="help-block">
<strong>{{ $errors->first('userimage') }}</strong>
</span>
#endif
</div>
</div>
<div class='col-md-6'>
<div class="form-group">
<label for="userimage" >Old Image</label>
<img class="img-circle" src="{{asset('public/storage/'. $info->userimage )}}" alt="" style="height:50px;"/>
<input type="hidden" name="oldImage" value="{{ $info->userimage }}">
</div>
</div>
</div>
</div>
<!-- /.box-body -->
{!! Form::close() !!}
</div>
</div><!-- /.col -->
</div><!-- /.row -->
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.