I have a shopping cart that has a pick-up and return date option that needs to be selected. I cannot get either of them to be echoed into the cart view when they are selected. I am using the Laravel Shopping Cart library to build the cart, which has an array for the extra options. I have passed the values into there to pass into the view, but it doesn't seem to work.
Here is the form mark:
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">Pick Up Date</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1of2" style="float: left;">
<label class="label" for="location" style="float:left;">Pick Up Location</label>
<select name="location" id="location" class="location">
<option>please choose</option>
<option value="bakersfield">Bakersfield</option>
<option value="chico">Chico</option>
<option value="fresno">Fresno</option>
<option value="hayward">Hayward</option>
<option value="manteca">Manteca</option>
<option value="oakley">Oakley</option>
<option value="redwood_city">Redwood City</option>
<option value="sacramento">Sacramento</option>
<option value="salinas">Salinas</option>
<option value="san_jose">San Jose</option>
<option value="san_jose_fusion">San Jose Fusion</option>
<option value="santa_rosa">Santa Rosa</option>
</select>
</div>
</div>
</fieldset>
<input type="hidden" name="id" value="{{ $rental->id }}">
<input type="hidden" name="title" value="{{ $rental->title }}">
<input type="hidden" name="pickupDate" value="{{ $rental->pickupDate }}">
<input type="hidden" name="returnDate" value="returnDate">
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Add to Cart</button>
</div>
</div>
</form>
Here is the cart view:
<article>
#if(session()->has('success_message'))
<div class="alert alert-success">
{{ session()->get('success_message') }}
</div>
#endif
<h1>Shopping Cart</h1>
#if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Cart::count() > 0)
<h2>{{ Cart::count() }} item(s) in Shopping Cart</h2>
<div>
<div>
#foreach (Cart::content() as $item)
<fieldset>
<article class="js-cart-product">
<p class="prod-title">Name: {{$item->model->name}} </p>
<p class="pu-date">Pick up date: {{ ($item->options->has('pickupDate') ? $row->options->pickup : '') }} </p>
<p class="rtn-date">Return Date: {{ ($item->options->has('returnDate') ? $row->options->pickup : '') }}</p>
<p class="loc">Location: {{$item->location}}</p>
<form action="{{ route('cart.destroy', $item->rowId)}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Delete Item</button>
</div>
</div>
<!-- <div class="cart__footer">
<p class="cart__text">
<a class="button" href="#" title="Buy products">
Check Out
</a>
</p>
</div> -->
</form>
</article>
</fieldset>
#endforeach
</div>
</div>
#else
<h3>No items in Cart!</h3>
Return to Rental Equipment
#endif
</div>
</article>
And here is the create item part of the controller:
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
$this->validate($request, array(
'location'=>'required',
));
Cart::add($request->id, $request->title, 1, $request->location, $options = ['pickup' => 'pickupDate', 'returnDate' => 'returnDate'])
->associate('App\Rental');
Session::flash('success', 'The item was successfully save!');
return redirect()->route('cart.index');
}
From your form blade
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">
Pick Up Date
</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
then in the bottom somewhere of your master/app blade you put
<script>
$(function() {
$("#pickupDate" ).datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
<script type="text/javascript">
$(function() {
$("#returnDate").datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
be sure to include these necessary files jquery.min.js, and if you use bootstrap then you need bootstrap.min.js, bootstrap-datepicker.min.js
Please let me know if it works
Related
I have a problem while creating edit and create forms
I have a form that is used to create and edit when I call $data on the form so that it appears when editing the form.
But when I open the form to create new data the error $data is undefined appears.
This is my route
Route::resource('/tabungan/jadwal-autodebet', SavingAccountScheduleController::class);
This is the create and edit script in my controller
public function create()
{
$info['header'] = $this->title;
$info['description'] = 'Tambah';
$info['breadcrumb'] = null;
return view('banking::autodebet-schedule.form',$info);
}
public function edit($id)
{
$info['header'] = $this->title;
$info['description'] = 'Ubah';
$info['breadcrumb'] = null;
$data = TransactionSchedule::find($id);
$origin = clone $data;
$origin = $data->origin;
$destination = clone $data;
$destination = $data->destination;
$destinationType = '';
$saving = SavingAccount::class;
$loan = LoanAccount::class;
if($data->origin_type == $saving){
$destinationType = 'Tabungan';
}elseif($data->origin_type == $loan){
$destinationType = 'Pinjaman';
}
// dd($data,$origin,$destination);
return view('banking::autodebet-schedule.form',compact('data','origin','destination','destinationType'),$info);
}
and this is my autodebet.schedule.form script
<form id="formData" action="{{ isset($data) ? admin_url('cb/tabungan/jadwal-autodebet/'.$data->id) : admin_url('cb/tabungan/jadwal-autodebet/')}}" method="POST" autocomplete="off">
{{ csrf_field() }}
#if(isset($data))
{{method_field('PUT')}}
#endif
<div class="container d-flex justify-content-center">
<div class="col-12">
<div class="row">
<div class="col-2">
<label class="label-required" for="" >Produk Tujuan</label>
</div>
<div class="col-10">
<select name="destination_type" id="destination_type" class="select2 form-control destination_type">
<option value="{{ $data->origin_type }}">{{ $destinationType }}</option>
<option class="type_option" id="SavingAccount" value="Modules\Banking\Entities\SavingAccount">Tabungan</option>
<option class="type_option" id="LoanAccount" value="Modules\Banking\Entities\LoanAccount">Pinjaman</option>
</select>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="">No Rekening Sumber</label>
</div>
<div class="col-10">
<select id="origin_code" class="select2 form-control origin_code">
<option value="{{ $origin->id }}">{{ $origin->saving_code }}-{{ $origin->customer->customer_name}}-{{ $destinationType }}</option>
</select>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="" >Transaksi Rekening Anggota/Nasabah Lain</label>
</div>
<div class="col-10">
<input type="hidden" id="other_costumer_transaction" name="other_costumer_transaction" value="0">
<div class="az-toggle {{ 'off' }}" id="other_costumer_transaction_toggle"><span></span></div>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="">No Rekening Tujuan</label>
</div>
<div class="col-10">
<select id="destination_code" class="select2 form-control destination_code" >
<option value="{{ $destination->id }}">{{ $destination->saving_code }}-{{ $destination->customer->customer_name}}-{{ $destinationType }}</option>
</select>
</div>
</div>
<div class="row" id="info-nasabah" style="display: none">
<div class="col-2">
</div>
<div class="col-10">
<h6>No. Rekening : <span id="info-kode">xxx.xxx.xxx</span></h6>
</div>
<div class="col-2">
</div>
<div class="col-10">
<h6>Nama Nasabah : <span id="info-nama">xxx.xxx.xxx</span></h6>
</div>
<div class="col-2">
</div>
<div class="col-10">
<h6>Tipe Produk : <span id="info-produk">xxx.xxx.xxx</span></h6>
</div>
<div class="col-2">
</div>
<div class="col-10">
<h6>Saldo : <span id="info-saldo">xxx.xxx.xxx</span></h6>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="">Tanggal Autodebet</label>
</div>
<div class="col-10">
<select name="scheduled_day" id="scheduled_day" class="select2 form-control scheduled_day">
<option disabled value="{{ $data->scheduled_day }}">{{ $data->scheduled_day }}</option>
#for ($i = 1; $i <= 31; $i++)
<option class="day_option" id="day_{{ $i }}" value="{{ $i }}">{{ $i }}</option>
#endfor
</select>
</div>
</div>
<div class="row">
<div class="col-2">
<label for="saving_type_package">Keterangan</label>
</div>
<div class="col-10">
<textarea name="desc" class="form-control desc" id="" cols="30" rows="5">{{ $data->desc }}</textarea>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="" >Status Autodebet</label>
</div>
<div class="col-10">
<input type="hidden" id="transaction_schedule_status" name="transaction_schedule_status" value="{{ $data->transaction_schedule_status ?? 1 }}">
<div class="az-toggle {{ isset($data->transaction_schedule_status) ? ($data->transaction_schedule_status == 1 ? 'on' :'') : 'on' }}" id="autodebet_status_toggle"><span></span></div>
</div>
</div>
<input type="hidden" value="" id="origin_id" name="origin_id">
<input type="hidden" class="schedule_id" id="schedule_id" name="schedule_id">
<input type="hidden" name="destination_code" id="destination_code_input">
<input type="hidden" name="origin_code" id="origin_code_input">
<div class="row">
<div class="col-12">
<div class="float-right">
<div class="btn-group">
<a href="{{admin_url('cb/tabungan/jadwal-autodebet')}}">
<button id="back_button" type="button" class="btn btn-secondary btn-block"><i class="typcn typcn-times"></i>Batalkan</button>
</a>
</div>
<div class="btn-group">
<button id="submit_button" class="btn btn-success btn-block">
<i id="loading_icon" style="display: none" class="fa fa-spin fa-spinner"></i>
<i id="submit_icon" class="typcn typcn-input-checked"></i>
Simpan</button>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</form>
how i fixed this problem?
I've got problem with dropdown list.
i want to show a primary key as a dropdown list in view blade , but i can not make the right rout to show it. Have you any ideas how to solve this problem?
this my root
$objects = ['users', 'permissions', 'roles', 'coins','pillars','subtindicators'];
foreach ($objects as $object) {
Route::get("$object", ucfirst(str_singular($object))."Controller#index")->middleware("permission:browse $object")->name("{$object}");
Route::get("$object/datatable", ucfirst(str_singular($object))."Controller#datatable")->middleware("permission:browse $object")->name("{$object}.datatable");
Route::get("$object/add", ucfirst(str_singular($object))."Controller#add")->middleware("permission:add $object")->name("{$object}.add");
Route::post("$object/create", ucfirst(str_singular($object))."Controller#create")->middleware("permission:add $object")->name("{$object}.create");
Route::get("$object/{id}/edit", ucfirst(str_singular($object))."Controller#edit")->middleware("permission:edit $object")->name("{$object}.edit");
Route::post("$object/{id}/update", ucfirst(str_singular($object))."Controller#update")->middleware("permission:edit $object")->name("{$object}.update");
Route::get("$object/{id}/delete", ucfirst(str_singular($object))."Controller#delete")->middleware("permission:delete $object")->name("{$object}.delete");
Route::get("$object/{id}", ucfirst(str_singular($object))."Controller#view")->middleware("permission:view $object")->name("{$object}.view");
Route::get("$object/create", ucfirst(str_singular($object))."Controller#list")->middleware("permission:add $object")->name("{$object}.create");
this my controller
public function add()
{ $strs = DB::table('stargets')->select('*')->get();;
return view('subtindicators.add-edit',compact('strs'));
}
public function update(Request $request, $id)
{
$object = $this->objectModel::find($id);
$object->update([
'skey_name' => $request->skey_name,
'Subtarget_base' => $request->Subtarget_base,
'Subtarget_end' => $request->Subtarget_end,
'subtarget_id' => $request->subtarget_id
]);
if ($request->save == 'browse')
return redirect()->route("{$this->objectName}");
elseif ($request->save == 'edit')
return redirect()->route("{$this->objectName}.edit", ['id' => $object]);
elseif ($request->save == 'add')
return redirect()->route("{$this->objectName}.add");
else
return redirect($request->previous_url);
}
this my blade
#extends('adminlte::page')
#include('bread.title')
#section('main-content')
<div class="container-fluid spark-screen">
<div class="row">
{!! csrf_field() !!}
<form class="form-horizontal" action="{{$actionName=='edit'?route("{$objectName}.update",['id'=>$object->id]):route("{$objectName}.create") }}" method="post">
{!! csrf_field() !!}
<input type="hidden" name="previous_url" value="{{ url()->previous() }}">
<form class="form-horizontal" action="{{ $actionName == 'edit' ? route("{$objectName}.update", ['id' => $object->id]) : route("{$objectName}.create") }}" method="post">
{!! csrf_field() !!}
<input type="hidden" name="previous_url" value="{{ url()->previous() }}">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">{{ ucfirst($actionName) }} {{ ucfirst($objectName) }} {{ !empty($object) ? "(ID $object->id)" : '' }}</h3>
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">{{ ucfirst($actionName) }} {{ ucfirst($objectName) }} {{ !empty($object) ? "(ID $object->id)" : '' }}</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
</div>
<div class="box-body">
<div class="form-group">
<label for="" class="col-md-2 control-label">col</label>
<div class="col-md-10">
<input type="text" name="skey_name" class="form-control" maxlength="255" value="{{ !empty($object) ? $object->coin_arname : '' }}" required>
</div>
</div>
<div class="form-group">
<label for="" class="col-md-2 control-label">cl_d</label>
<div class="col-md-10">
<input type="text" name="Subtarget_base" class="form-control" maxlength="255" value="{{ !empty($object) ? $object->coin_enname : '' }}" required>
</div>
</div>
<div class="form-group">
<label for="" class="col-md-2 control-label">cl_e</label>
<div class="col-md-10">
<input type="text" name="Subtarget_end" class="form-control" maxlength="255" value="{{ !empty($object) ? $object->coin_arsymbol : '' }}" required>
</div>
</div>
<div class="form-group">
<label for="" class="form-control">c_i</label>
<div class="col-md-10">
<select class="form-control" name="starget_id">
<option selected disabled value = " ">choos</option>
#foreach($strs as $vis)
<option value="{{$vis->id}}">{{$vis->target_name}}</option>
#endforeach
<!-- <p class="form-control-static">{{ $object->subtarget_id }}</p>-->
</div>
</div>
<div class="form-group has-feedback">
<label for="title">main<span class="text-danger">*</span></label>
<select class="form-control" name="starget_id">
<option selected disabled value = " ">choos</option>
#foreach($strs as $slider2)
<option value="{{$slider2->id}}" {{ $slider2->id== $slider->starget_id ? 'selected' : '' }}>{{$slider2->vname}}</option>
#endforeach
</div>
</div>
</div>
<div class="box-footer">
#include('bread.add-edit-actions')
</div>
</div>
</form> </form>
</div>
</div>
</div>
#endsection
this error what i got
ErrorException (E_ERROR)
Undefined variable: actionName (View: C:\project Last\resources\views\bread\title.blade.php) (View: C:\Ministry Last\resources\views\bread\title.blade.php)
You should forward $actionName variable to your view:
public function add()
{
$strs = DB::table('stargets')->select('*')->get();
$actionName = 'edit';
return view('subtindicators.add-edit',compact('strs', 'actionName'));
}
However this will hardcode the form to being an "edit" form. You must see what you really want with your logic.
I have followed a tutorial to create a laravel shopping cart https://www.youtube.com/watch?v=Jzi6aLKVw-A&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR&index=3 and composer https://github.com/hardevine/LaravelShoppingcart
I want make button select size product
CartController.php
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
Cart::add($request->id, $request->name, 1, $request->price, $request->data)->associate('App\Product');
return redirect()->route('cart.index')->with('success_message', 'Item was added to your cart!');
}
Product.blade.php
<h1 class="topic">{{ $product->name}}</h1>
<h4>{{ $product->price }}</h4>
<div class="size-b">
<div class="title">
<h5>Trousers</h5>
</div>
<form action="{{ route('cart.store') }}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $product->id }}" >
<input type="hidden" name="name" value="{{ $product->name }}" >
<input type="hidden" name="price" value="{{ $product->price }}" >
<div class="btn-group" role="group" aria-label="Basic">
<button> <input type="checkbox" name="data" value="S" class="btn btn-secondary"><span>S</span></button>
<button><input type="checkbox" name="data" value="M" class="btn btn-secondary">M</button>
<button><input type="checkbox" name="data" value="L" class="btn btn-secondary">L</button>
<button> <input type="checkbox" name="data" value="XL" class="btn btn-secondary">XL</button>
</div>
<button type="submit" class="btn btn-secondary">ซื้อสินค้า</button>
</form>
cart.blade.php
#if (Cart::count() >0 )
<h4>{{ Cart::count() }}Item(s) in cart</h4>
<div class="table-cart-destop">
#foreach (Cart::content() as $item)
<div class="row">
<div class="col-lg">
<div class="remove">
<form action="{{ route('cart.destroy', $item->rowId) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-secondary">X</button>
</form>
</div>
</div>
<div class="col-lg-2">
<div class="image-border">
<img src="{{ $item->model->photo1}}" class="img-cart" alt="product-cart">
</div>
</div>
<div class="col-lg-5">
<div class="content">
<h4>{{ $item->model->name}}</h4>
<p>{{ $item->model->details}}</p>
</div>
</div>
<div class="col-lg">
<p class="size">size {{ $item->model->data }} </p>
</div>
<div class="col-lg">
<div class="quality">
<form action="">
<div class="form-row align-items-center">
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
<option selected>1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="3">5</option>
</select>
</div>
</div>
</form>
</div>
</div>
<div class="col-lg">
<p class="size">{{ $item->data}}฿</p>
</div>
</div>
#endforeach
<div class="tatal">
<p>ยอดชำระเงินทั้งหมด</p>
<p class="price-tatal">{{ Cart::subtotal() }}฿</p>
</div>
<div class="buy-now">
<a href="{{ route('checkout.index') }}">
<button type="submit" class=" btn btn-secondary">สั่งซื้อสินค้า</button>
</a>
</div>
I try dd($request->all());
run php artisan serve --> size product (don't show)
Hello I'm new to laravel and I tried to add HTML to a <h6> field like below image .
and it appears in the page source like this
My question is why it doesn't render in the browser ? What has Laravel done here ?
My Blade code :
#extends('layouts')
<div class="row">
<div class='col-md-6 offset-md-3' >
#section('content')
<h1>{{$card->title}}</h1>
{{$card->created_at}}
<br>
<ul class="list-group">
#foreach($card->notes as $note)
<li class="list-group-item">
<h6>{{$note->body}}</h6> <button class="btn btn-info btn-sm" onclick="togglediv('divform{{$note->id}}')" >Edit</button>
<div class="col-md-6 offset-md-3" id="divform{{$note->id}}" style="display: none;">
<hr>
<form action="/notes/{{$note->id}}/edit" method="POST">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="exampleInputPassword1"><h6>Edit the note</h6></label>
<input type="text-area" name="body" class="form-control" id="body" placeholder="{{$note->body}}">
</div>
<button type="submit" class="btn btn-success btn-sm">Done</button>
</form>
</div>
</li>
#endforeach
<script type="text/javascript">function togglediv(id) {
var div = document.getElementById(id);
div.style.display = div.style.display == "none" ? "block" : "none";
}</script>
</ul>
</div>
</div>
#endsection
#section('footer')
<br>
<div class="col-md-6 offset-md-3">
<hr>
<form action="/cards/{{$card->id}}/notes" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="exampleInputPassword1"><h3>Add a new note</h3></label>
<input type="text-area" name="body" class="form-control" id="body" placeholder="Body">
</div>
<button type="submit" class="btn btn-primary">Add Note</button>
</form>
</div>
#endsection
Your html is escaped ,if you don't want that use the following syntax to output the html
{!!$note->body!!}
more info
i created one view page using laravel 5.2, in that page created one table and fetch data from mysql database. and give dynamic drop down button for Edit and Delete. when i click on delete it will delete the corresponding row from the table.That i done and working properly. I want to do the Edit part. When i click on Edit option it will redirect to one form where i need to fetch all the details from corresponding row into form text field and we can edit the things , when we click on save button it will update the data to the mysql table.
Here i am not able to fetch the details of corresponding row into the form text field. Can anyone help me to do that.
my view page "userAdmin.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">
<h3>Select User :</h3>
<thead>
<tr>
<th>User ID</th>
<th>User Desc</th>
<th>Contact Name</th>
<th>Contact Email</th>
<th>Time Zone</th>
<th>Active</th>
<th>Last Login (GMT+05:30)</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{{--{{ UserController::getIndex() }}--}}
#foreach($name as $nam)
<tr>
<td>{{ $nam->userID }}</td>
<td>{{ $nam->description }}</td>
<td>{{ $nam->contactName }}</td>
<td>{{ $nam->contactPhone }}</td>
<td>{{ $nam->timeZone }}</td>
<td>
#if($nam->isActive == '1')
Yes
#else
No
#endif
</td>
<td>{{ date('Y/m/d H:i:s',($nam->lastLoginTime)) }}</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="{{ $nam->userID }}">View/ Edit
</li>
{{--#endif--}}
<li>Delete</li>
</ul>
</div>
{{--#endif--}}
</td>
</tr>
#endforeach
</tbody>
</table>
{{$name->links()}}
</div>
</div>
</div>
</div>
</div>
<input type="submit" id="add" name="add" value="Edit" class="button">
</br>
<h4>Create a new User</h4>
<form role="form" method="POST" action="{{ url('userAdmin') }}">
<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="userID" value="{{ old('userID') }}" placeholder="Enter User 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>
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable();
} );
</script>
#endsection
and Controller page "userController.php" is
<?php
/**
* Created by PhpStorm.
* User: Rahul vp
* Date: 12/05/16
* Time: 11:06 AM
*/
namespace App\Http\Controllers;
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 UserController extends Controller
{
public $type = 'User';
public function getIndex()
{
$name = DB::table('user')->simplePaginate(10);
return view('user.userAdmin')->with('name', $name);
}
public function getData()
{
$name = DB::table('user');
return view('user.add')->with('name', $name);
}
public function userInsert()
{
$postUser = Input::all();
//insert data into mysql table
$data = array('userID'=> $postUser['userID']
);
// echo print_r($data);
$ck = 0;
$ck = DB::table('user')->Insert($data);
//echo "Record Added Successfully!";
$name = DB::table('user')->simplePaginate(10);
return view('user.userAdmin')->with('name', $name);
}
public function delete($id)
{
DB::table('user')->where('userID', '=', $id)->delete();
return redirect('userAdmin');
}
}
then the Editing page where i need to fetch data for updation "add.blade.php"
#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">View/Edit User</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">View/Edit User Information</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('accountAdmin') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">User ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="userID" value="{{ old('userID')}}" placeholder="Enter User ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Active</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('isActive') }}" name="isActive" >
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">User Description</label>
<div class="col-md-6">
<input type="text" class="form-control" name="description" value="{{ old('description') }}" placeholder="Enter the description">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Contact Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="contactName" value="{{ old('contactName') }}" placeholder="Enter Contact Name">
</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="contactPhone" value="{{ old('contactPhone') }}" placeholder="Enter Mobile Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Contact Email</label>
<div class="col-md-6">
<input type="email" class="form-control" name="contactEmail" value="{{ old('contactEmail') }}" placeholder="Enter E-Mail Address">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Notify Email</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ old('notifyEmail') }}" placeholder="Enter E-Mail Address">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Time Zone</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('timeZone') }}" name="timeZone" >
<option value="0">GMT+05:30</option>
<option value="Inactive">xyz</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Authorized Group</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('distanceUnits') }}" name="distanceUnits" >
<option value="0">all</option>
<option value="1">Km</option>
<option value="2">Nm</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">First Login page</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('firstLoginPageID') }}" name="firstLoginPageID" >
<option value="0">Main Menu</option>
<option value="1">Liter</option>
<option value="2">IG</option>
<option value="3">ft^3</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Maximum Access Level</label>
<div class="col-md-6">
<select class="form-control" value="{{ old('maxAccessLevel') }}" name="maxAccessLevel" >
<option value="3">New/Delete</option>
<option value="1">Read/View</option>
<option value="2">Write/Edit</option>
</select>
</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>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
then route.php is
Route::any('userAdmin', 'UserController#getIndex');
Route::any('user/add', 'UserController#getData');
Route::any('user/delete/{id}', 'UserController#delete');
Route::post('userAdmin', 'UserController#userInsert');
Route::any('user/add', function()
{
return view('user/add');
});
Anyone please help me to do this. response are appreciable.
Your laravel code doesn't follow the common practices or the right way to use laravel.
Please have a look into Resource Controllers
https://laravel.com/docs/5.2/controllers#restful-resource-controllers
Also Eloquent Models
https://laravel.com/docs/5.2/eloquent
Now let's try to solve your problem, add these to your routes
//to show edit form and fetch passed user id info from db
Route::get('user/edit/{id}', 'UserController#edit');
//to get the edited info and save it to db
Route::get('user/update/{id}', 'UserController#update');
Now create a model called user by running this command from your project directory:
php artisan make:model User
Now add these in your controller with other use statements
use App\User; /* User model */
use Illuminate\Database\Eloquent\ModelNotFoundException; /* find or fail error exception class */
Now add these to your controller
// Show Edit page.
public function edit($user_id)
{
try{
//Find the user object from model if it exists
$user= User::findOrFail($user_id);
//Redirect to edit user form with the user info found above.
return view('add',['user'=>$user]);
}
catch(ModelNotFoundException $err){
//redirect to your error page
}
}
// Update user
public function update(Request $request, $user_id)
{
try{
//Find the user object from model if it exists
$user= User::findOrFail($user_id);
//Set user object attributes
//the $request index should match your form field ids!!!!!
//you can exclude any field you want.
$user->description = $request['description'];
$user->contactName = $request['contactName'];
$user->contactPhone = $request['contactPhone'];
$user->timeZone = $request['timeZone'];
//Save/update user.
$user->save();
//redirect to somewhere?
}
catch(ModelNotFoundException $err){
//Show error page
}
}
In your edit view you can access the retrieved user by doing
$user->description //$user->[attribute name]
Since you are not using blade, I believe you can do this, wherever you have old(...) replace it with old(...) || $user->...
except select tags
example:
value="{{ old('contactName') || $user->contactName }}"
for select tags I will give you one example and you need to do the rest by yourself
<select class="form-control" value="{{ old('timeZone') || $user->timeZone}}" name="timeZone" >
<option value="0" {{ $user->timeZone == "0"? 'Selected':''}}>GMT+05:30</option>
<option value="Inactive" {{ $user->timeZone == "Inactive"? 'Selected':''}}>xyz</option>
</select>
Good luck.
Upvotes will be appreciated!