update my database and discard an element from table - doctrine

i want to delete an item from database and remove it from table
Entity product
i want to select all the products and after a click on button x i want it to update name and remove it from table
Any help ??
my twig :
<script>
$("document").ready(function () {
$(".btn-group-vertical").click(function () {
$.ajax({
type:'get',
url:'http://localhost/symfony/nouveau%20dossier/web/app_dev.php/'+$(this).val(),
beforeSend: function () {
},
success: function(data){
var x = $("produit").val(data.approuver);
console.log(x);
$("tt").find('td').fadeOut(1000,function(){
$("tt").remove();
});
}
});
});
});
</script>
{% endblock script %}
{% block body %}
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Produit en attente</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>Produit de</th>
<th>Libelle Produit</th>
<th>Quantite stock</th>
<th>etat</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr class="tt">
<td align="center">{{ product.seller }}</td>
<td align="center">{{ product.libelle }}</td>
<td align="center">{{ product.quantiteStock }}</td>
<td><button
type="button"
class="btn btn-group-vertical btn-danger btn-xs"
id=1>Decliner</button>
<button type="button"
class="btn btn-group-vertical btn-sucess btn-xs"
id=2>Approuver</button>
</td>
</tr>
{% endfor %}
<tfoot>
<tr>
<th>Produit de</th>
<th>Libelle Produit</th>
<th>Quantite stock</th>
<th>etat</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
<!-- ./wrapper -->
my controller :
public function approuverAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$produit = $em->getRepository('ProductBundle:Product')
->findOneBy(array('id'=>$id));
$produit->setApprouver('Approuver');
$respones = new JsonResponse();
return $em->merge($produit);
$em->flush();
}
Any help please

I want to delete an item from database and remove it from table Entity
product
To remove a product:
$em->remove($produit);
$em->flush();

Related

How to make filter on table based on option select

I am working on a project (vue + laravel) where i need to fetch all table structure from db. So my table will be dynamically with all field(table header) and record. Now i need to do a filter(search) for each visible column. I have tryed some way but it does not work for me, I just dont know how to pass only column value for each option select value.
Collapse content is the option who will show on filter click and there should display the option for each column
<table class="table table-hover align-middle mb-0" :class="smallTable.smTable">
<thead class="">
<tr>
<th><input type="checkbox" class="form-check-input" v-model="selectAll" title="Select All"></th>
<th v-for="(header, i) in visibleHeaders" :key="i" scope="col">
{{ header.name }}
</th>
<th v-if="actionHide">ACTION</th>
</tr>
</thead>
<!-- Collapsed content ./ -->
<thead class="collapse" id="collapseFilter">
<tr>
<th></th>
<th v-for="(header, i) in visibleHeaders" :key="i">
<div class="filter-table col-12 d-flex">
<select id="" class="form-select" >
<option v-for="(lead, i) in leads" >{{lead}}</option>
</select>
</div>
</th>
</tr>
</thead>
<!-- ./ Collapse contet -->
<tbody>
<tr v-show="leads.length" v-for="(lead, i) in leads" :key="i">
<td>
<input type="checkbox" class="form-check-input" v-model="selected" :value="lead.id" />
</td>
<td v-for="(field, j) in lead" :key="j">
<span v-if="field == 'new'" class="badge badge-primary">
{{ field }}
</span>
<span v-else-if="field == 'contract'" class="badge badge-success">
{{ field }}
</span>
<span v-else>
{{ field }}
</span>
</td>
<td >
<button #click="editLead(lead.id)" type="button" class="btn btn-sm btn-secondary" data-mdb-toggle="modal" data-mdb-target="#editLeadModal" >
<i class="fa-solid fa-eye"></i>
</button>
</td>
</tr>
<tr v-show="!leads.length">
<td colspan="12" class="text-center">Sorry :( No data found.</td>
</tr>
</table>
data() {
return {
headers: [],
leads: [],
fields: [],
}
}
mounted() {
axios.get('/leads/getfields')
.then(response => {
this.fields = response.data.map(field => {
return {
name: field,
visible: true,
}
});
}
this.getData();
});
}
getData() {
this.headers = this.fields.map(item => {
if (item.visible) {
return item.name;
}
});
axios.post('/leads/getleads?page=' + this.pagination.current_page, {
perPage: this.displayRecord,
fields: this.headers,
})
.then(response => {
this.leads = response.data.data;
this.pagination = response.data.meta
});
},

Update Column Value with the help of vue js in laravel

This is my blade of laravel
<!-- Main Container -->
<main id="main-container">
<!-- Page Content -->
<div class="content" id="app">
<h2 class="content-heading">Accessory Details</h2>
<div class="block block-themed">
<div class="block-header block-header-default">
<div class="block-options">
<button class="btn btn-success pull-right">New Request</button>
</div>
</div>
<div class="block-content">
<form action="" method="get">
<div class="form-group row">
<label class="col-12" for="example-select2-multiple">Store Location</label>
<div class="col-lg-11">
<select class="form-control" name="store" style="width: 100%;" required>
<option value="">Choose Select Store location..</option>
#foreach($stores as $store)
<option #if($store_selected == $store->id) selected #endif value="{{ $store->id }}">{{ $store->channel_id }} | {{ $store->street_name }}</option>
#endforeach
</select>
</div>
<div class="col-1">
<button type="submit" class="btn btn-alt-success">
Filter
</button>
</div>
</div>
</form>
</div>
</div>
<!-- Dynamic Table Full -->
<div class="block">
<div class="block-content block-content-full">
#include('errors.error')
<div class="table-responsive">
<table class="table table-bordered table-striped table-vcenter js-dataTable-full1" data-page-length='50'>
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center">User</th>
<th>Store</th>
<th class="">Request Date</th>
<th class="">Status</th>
<th class=""></th>
</tr>
</thead>
<tbody>
#foreach($accessories as $accessory)
<tr>
<td>{{ $accessory->id }}</td>
<td>{{ $accessory->user->name }}</td>
<td>{{ $accessory->store->channel_id }} - {{ $accessory->store->street_name }}</td>
<td>{{ $accessory->request_date }}</td>
<td>{{ $accessory->status }}</td>
<td><button class="btn btn-primary btn-sm" #click="getdetails({{ $accessory->id }})" data-toggle="modal" data-target="#details">Details</button></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
<!-- END Dynamic Table Full -->
<b-modal id="modal-1" title="Details" v-model="dialog" #ok="handleOk">
<form ref="form" #submit.stop.prevent="handleSubmit">
#csrf
{{-- <input type="hidden" v-model="id"/> --}}
<select class="form-control" name="status">
<option>Pending</option>
<option>Ordered</option>
<option>Received</option>
</select>
<br><br>
<table class="table table-bordered">
<tr>
<td>Vendor</td>
<td>Description</td>
<td>QTY</td>
</tr>
<tr v-for="line in lines">
<td>#{{line.name}}</td>
<td>#{{line.description}}</td>
<td>#{{line.qty}}</td>
</tr>
</table>
</form>
</b-modal>
</div>
</main>
<script >
window.app = new Vue({
el: '#app',
mounted: function() {
},
data() {
return {
dialog: false,
lines: [],
}
},
methods: {
getdetails: function(id) {
let self = this;
this.lines = [];
axios.get('/accessory/' + id)
.then(response => {
this.dialog = true;
self.lines = response.data;
console.log(self.lines)
//$(location).attr('href', '/accessory')
})
.catch(function (error) {
alert('Error');
});
},
handleOk(bvModalEvt) {
// Prevent modal from closing
bvModalEvt.preventDefault()
// Trigger submit handler
this.handleSubmit()
},
handleSubmit() {
axios.post('/accessory/status-change/' )
.then(response => {
$(location).attr('href', '/accessory')
})
.catch(function (error) {
alert('Error');
});
}
},
created()
{
}
})
</script>
<script>
$(document).ready(function() {
$('.js-dataTable-full1').DataTable( {
"order": [[ 0, "desc" ]]
} );
});
</script>
Here for the following statement in model , i want to bind it with accessory_request_id which i am recieving {{-- --}}. So what should i select for v-model?
If i check for console.log(self.lines), it returns me an array with accessory_request_id.
Then later on with the help of accessory_reques_id, i want to update status in database.
So follow is my controller code --
{
//return 'test';
//dd($request->all());
$accessory_request = AccessoryRequest::findorfail($request->id);
$accessory_request->status = $request->input('status');
$accessory_request->save();
return ['message' => 'Status Updated'];
}
In web.php -- route is
Route::post('accessory/status-change','AccessoryController#status_change');
please help me to resolve it

how to implement Laravel AJAX live search on CRUD management table?

I have a CRUD table that's holding records of doctors that get
retrieved from the MySQL database. I've been following a few tutorials
and managed to set up AJAX live results search.
Now the problem comes from the fact that the logic is stored in a
Controller. I can succesfully retrieve the details from the database
using '-- foreach($data as $row) --' but I don't know how to retrieve
the CRUD actions ( In this case Delete.The create method is separated
from the table itself. I also can't implement status cell using i)
I've tried putting the actions inside the td'. .'td. tags but I get
Routing errors.
Any help is appreciated.
I have the code for the CRUD actions, images etc..inside
"index.blade.php". They should be somehow implemented in the
controller though. doctor controller
function liveSearchDoctor(Request $request)
{
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$data =Doctor::
where('name', 'like', '%'.$query.'%')
->orWhere('mobile', 'like', '%'.$query.'%')
->orWhere('email', 'like', '%'.$query.'%')
->orderBy('id', 'desc')
->paginate(2);
}
else
{
$data =Doctor::latest()->paginate(2);
}
$total_data = $data->count();
if($total_data > 0)
{
foreach($data as $row)
{
$output .= '
<tr>
<td class="center"><img src="'.$row->image.'" style="width:40px;height:40px;"/></td>
<td>'.$row->id.'</td>
<td>'.$row->name.'</td>
<td>'.$row->department->name.'</td>
<td>'.$row->email.'</td>
<td>'.$row->mobile.'</td>
//status should be implement here
<td><a class="btn btn-primary" href="'.route('doctors.edit',$row->id).'"><i class="fas fa-edit">Edit</i></a>
//delete form should be implement here
</td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output,
'total_data' => $total_data
);
echo json_encode($data);
}
}
index.blade.php (( Note: The CRUD actions and everything is here
implemented succesfully. I however can't implement delete in the
controller )) here is the index.blade.php
#extends('layout.master')
#section('css')
#endsection
#section('content')
<br>
<div class="br-pagebody">
<div class="br-section-wrapper">
#include('include._message')
<div class="row">
<h6 class="br-section-label">Doctors List</h6>
</div>
<div class="row">
<div class="col-lg-4 ">
<div class="form-group">
<input type="text" name="search" id="search" class="form-control" placeholder="Search doctor Data" />
</div>
</div>
<div class="col-lg-3 offset-lg-5">
Add New Doctor
</div>
</div>
</br>
<div class="table-responsive">
<table class="table table-bordered table-colored table-dark">
<thead class="thead-colored thead-dark">
<tr>
<th>Image</th>
<th>Doctor Id</th>
<th>Name</th>
<th>Department</th>
<th>Email</th>
<th>mobile</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach($doctors as $doctor)
<tr>
<td class="center"><img src="{{URL::asset($doctor->image)}}" style="width:40px;height:40px;"/></td>
<td class="center">{{$doctor->id}}</td>
<td class="center">{{$doctor->name}}</td>
<td class="center">{{$doctor->department->name}}</td>
<td class="center">{{$doctor->email}}</td>
<td class="center">{{$doctor->mobile}}</td>
<td class="center">
#if($doctor->status==1)
<span class="badge badge-success">Active</span>
#else
<span class="label label-danger">Deactive</span>
#endif
</td>
<td class="center">
<a class="btn btn-primary" href="{{route('doctors.edit',$doctor->id)}}">
<i class="fas fa-edit">Edit</i>
</a>
{!! Form::open(['method' => 'DELETE','route' => ['doctors.destroy', $doctor->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
</div><!-- table-wrapper -->
<div class="d-flex justify-content-center">
{!! $doctors->links() !!}
</div>
</div>
</div>
#endsection
#section('js')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
fetch_doctor_data();
function fetch_doctor_data(query = '')
{
console.log(query)
$.ajax({
url:"{{route('search.doctor')}}",
method:"GET",
data:{'query':query},
dataType:'json',
success:function(data)
{
console.log(data)
$('tbody').html(data.table_data);
}
})
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_doctor_data(query);
});
});
</script>
#endsection

in laravel pass updated value from view to controller without submit button

I am tiring to store manager auth id, I have view page that contents of user tickets, in my database name called "ticket" in that table I have column name called "ticket_view_by_manager_id" in this column I am trying to store manager auth id, when manager open that ticket that time manager auth id, store in this "ticket_view_by_manager_id" column,
my controller
public function manager_assigned_Chat(Request $request, $ticket_id){
$this->validate($request, [
'ticket_view_by_manager_id' => 'required',
]);
$input = User_Ticket::find($ticket_id);
$input['ticket_view_by_manager_id'] = $request->submit;
$input->save();
}
my route
Route::post('user_ticket_chat{ticket_id}', 'Services\User_TicketController#manager_assigned_Chat')->name('user_ticket_chat{ticket_id}');
my view "showing all user ticket list"
<table id="myTable" class="table table-bordered table-striped">
<thead>
<tr>
<th>slNo</th>
<th>Ticket ID</th>
<th>Subject</th>
<th>Status</th>
<th>Last Update</th>
<th>Created</th>
</tr>
</thead>
<tbody>
<form method="POST" action="{{ route('user_ticket_chat{ticket_id}')}}" enctype="multipart/form-data">
#csrf
#foreach ($my_tickets as $key=>$my_tickets_list)
<tr>
<td style="text-align:center"> {{ $key + 1 }} </td>
<td >{{ $my_tickets_list->ticket_id }}</td>
<td > <input type="hidden" name="submit" value="{{ Auth::user()->staff_id }}" href="ticket_chat{{ $my_tickets_list->ticket_id }}" >{{ $my_tickets_list->subject }}</td>
<td style="text-align:center">
#if($my_tickets_list->status == 'OPEN')
<span class="btn waves-effect waves-light btn-sm btn-success">OPEN</span>
#elseif($my_tickets_list->status == 'COMPLETE')
<span class="btn waves-effect waves-light btn-sm btn-info">COMPLETE</span>
#else($my_tickets_list->status == 'PENDING')
<span class="btn waves-effect waves-light btn-sm btn-danger">PENDING</span>
#endif
</td>
<td >{{$my_tickets_list->created_at->todatestring()}} </td>
<td >{{$my_tickets_list->updated_at->todatestring()}} </td>
</tr>
#endforeach
<input type="submit" value="Send.">
</form>
</tbody>
</table>
in your web.php
Route::post('user_ticket_chat','Services\User_TicketController#manager_assigned_Chat')->name('user_ticket_chat');
in your controller
public function manager_assigned_Chat(Request $request){
$this->validate($request,[
'ticket_id' => 'required',
]);
$input = User_Ticket::find($ticket_id);
$input->ticket_view_by_manager_id = Auth::user()->id; // or Auth::user()->staff_id; in your case
$input->save();
}
in blade view add meta tag :
<meta name="csrf-token" content="{{ csrf_token() }}">
in table :
<table id="myTable" class="table table-bordered table-striped">
<thead>
<tr>
<th>slNo</th>
<th>Ticket ID</th>
<th>Subject</th>
<th>Status</th>
<th>Last Update</th>
<th>Created</th>
</tr>
</thead>
<tbody>
#foreach ($my_tickets as $key=>$my_tickets_list)
<tr>
<tdstyle="text-align:center"> {{ $key + 1 }} </td>
<td>{{ $my_tickets_list->ticket_id }}</td>
<td class="ticket" data-ticketid="{{ $my_tickets_list->ticket_id}}">{{ $my_tickets_list->subject }}</td>
<td style="text-align:center">
#if($my_tickets_list->status == 'OPEN')
<span class="btn waves-effect waves-light btn-sm btn-success">OPEN</span>
#elseif($my_tickets_list->status == 'COMPLETE')
<span class="btn waves-effect waves-light btn-sm btn-info">COMPLETE</span>
#else($my_tickets_list->status == 'PENDING')
<span class="btn waves-effect waves-light btn-sm btn-danger">PENDING</span>
#endif
</td>
<td>{{$my_tickets_list->created_at->todatestring()}} </td>
<td>{{$my_tickets_list->updated_at->todatestring()}} </td>
</tr>
#endforeach
</tbody>
</table>
now using Jquery AJAX request :
<script>
$(document).on('click','.ticket',function(){
var ticketID=$(this).attr('data-ticket');
$.ajax({
url:'/user_ticket_chat',
type:'POST',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
dataType:'json',
data:{"ticket_id":ticketID},
success:function(response){
console.log(response);
},
error:function(){
alert('Error');
}
});
});
</script>
You should change your validator. It will be like, Please try this:
$this->validate($request, [
'submit' => 'required',
]);
Because your are not sending any info about ticket_view_by_manager_id from view.
Suggestion: may be your code is not well decorated. If you can please
have a look.

Why is The Entire Body of the Web page loaded to the table instead of the data when retrieving data from the database using ajax in Larvel

I have this table showing all the encashment records, it works fine but when I try to get the rows from the database within a specific range of dates and display it to the same table, the whole body of the webpage is being loaded to the table instead of the rows from the database.
Please see image below:
this is my function from the tableController.php
function encashmentRecord_DateRange(Request $request){
if($request->ajax())
{
$start = $request->get('startDate');
$end = $request->get('endDate');
$data = DB::table('encashment_requests')->whereBetween('date',[$start,$end])->get();
return view('incashment',compact('data'));
}
}
the script in my encashment.blade.php
$(document).ready(function(){
$('#dataTable').DataTable();
$('#start_date').datepicker({
format:'yyyy-mm-dd',
autoclose:true
}).val();
$('#end_date').datepicker({
format:'yyyy-mm-dd',
autoclose:true
}).val();
var range = document.getElementById('btnrange');
range.addEventListener('click',function(){
var start_date = $('#start_date').val();
var end_date =$('#end_date').val();
alert(end_date);
$('#dataTable').DataTable().destroy();
listDateRange(start_date,end_date);
},false);
function listDateRange(startDate,endDate){
$.ajax({
method: 'GET',
url : "{{ route('encashmentRecord.action') }}",
data: {startDate:startDate, endDate:endDate},
success:function(data)
{
alert(data);
$('#dataTable').html(data);
}
})
}
});
If you only need the data, you shouldn't be returning view() from your controller.
Try replacing it with this: return response()->json($data, 200);
I may have found the solution to my problem, though it may look lengthy. But it worked.
I made another view, encashmentRecord.blade, containing only the table, so in the controller, instead of returning the data in the incashment.blade, I returned the data to the new view, encashentRecord.blade and I use "#include('encashmentRecord')" in my incashment.blade to display the table.
encashmentRecord.blade.php
<div class="card-body">
<table id="dataTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Account ID</th>
<th class="sort">Date</th>
<th>Desired Ammount</th>
<th>Destination</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#if(isset($data))
#foreach($data as $row)
<tr>
<td>{{ $row->account_name}}</td>
<td>{{ $row->account_id}}</td>
<td>{{ $row->created_at}}</td>
<td>Php {{ $row->requested_ammount}}</td>
<td>{{ $row->destination}}</td>
<td>{{ $row->status}}</td>
</tr>
#endforeach
#else
<tr>
No Data
</tr>
#endif
</tbody>
</table>
and this is incashment.blade.php
<div class="row">
<div class="col-xl-3 ">
<input type="text" name="query" id="start_date" class="form-control input_daterange">
</div>
<div class="col-xl-3 ">
<input type="text" name="query" id="end_date" class="form-control">
</div>
<div class="col-xl-3 ">
<button class="btn" id="btnrange">Go</button>
</div>
</div>
<div class="card" style="margin-bottom: 0px">
<div class="card-header">
Encashment Record
</div>
<div id="table_data">
#include('tables.encashmentRecord')
</div>
</div>
I just change the view in my return on the controller
return view('tables.encashmentRecord',compact('data'));

Resources