Laravel 8 [Yajra DataTable] - DataTables warning: table id=userTable - Invalid JSON response - php

I am trying to display all User (Where status is = 1) Data from database into DataTable using Yajra Package in Laravel but it always displays this error:
DataTables warning: table id=userTrashBin - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
When I check the response (In Developer's tools > Network Tab) it does not have any or it show Failed to load response data:
Here's my datatable() inside the UserController.php:
public function datatable()
{
$data = User::where('status', '=', 1)->get();
return DataTables::of($data)
->addColumn('action', function($data){
$url_edit = url('user/'.$data->id.'/edit');
$url_deact = url('user/deactivateUser/'.$data->id);
$url = url('user/'.$data->id);
$view = "<a class = 'btn btn-primary' href = '".$url."' title = 'View'><i class = 'nav icon fas fa-eye'></i></a>";
$edit = "<a class = 'btn btn-warning' href = '".$url_edit."' title = 'Edit'><i class = 'nav icon fas fa-edit'></i></a>";
$delete = "<button data-url = '".$url_deact."' onclick = 'deleteData(this)' class = 'btn btn-action btn-danger' title = 'Deactivate'><i class = 'nav-icon fas fa-trash-alt'></i></button>";
return $view."".$edit."".$delete;
})->editColumn('user_type', function($data){
$dataUser = User::find($data->id)->first();
$returnStr = "";
if($dataUser->hasRole('A')){
$returnStr = "Administrator";
}else{
$returnStr = "Employee";
}
return $returnStr;
})
->rawColumns(['action'])
->make(true);
}
Here's the script file in index.blade.php:
$("#userTable").DataTable({
responsive:true,
processing:true,
pagingType: 'full_numbers',
stateSave:false,
scrollY:true,
scrollX:true,
autoWidth: false,
ajax:"{{ url('user/datatable') }}",
columns:[
{data:'first_name', name: 'first_name'},
{data:'last_name', name: 'last_name'},
{data:'email', name: 'email'},
{data:'user_type', name: 'user_type'},
{data:'action', name: 'action', searchable: true, sortable:true},
]
});
The route for user/datatable inside web.php:
Route::get('user/datatable', 'UserController#datatable')->name('user/datatable')->middleware('role:A');
And finally the index.blade.php file where the userTable is located:
<table id="userTable" class="table table-bordered table-striped" data-toggle = "table1_" style = "width: 100%;">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail Address</th>
<th>User Type</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail Address</th>
<th>User Type</th>
<th>Action</th>
</tr>
</tfoot>
</table>
I don't know what causes the error and how to fix it for now because it doesn't have any response data or other error/s that will give me a hint.

Related

Hide the action column in Yajra datatables using gates

I'm pretty new to Laravel. I want to hide the Action column of the Yajra Datatable for specific users using Gates based on their roles.
I have been able to hide the buttons in the controller, but I want to hide the entire action column in the blade file so the users will not see it at all.
I have tried this
Jquery Codes
var table = $('.cdata-tables').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('clients.index') }}",
columns: [
{data: 'DT_RowIndex', name: 'DT_RowIndex'},
{data: 'name', name: 'name'},
{data: 'location', name: 'location'},
{data: 'description', name: 'description'},
{data: 'testimonial', name: 'testimonial'},
#can('view-actions'){data: 'action', name: 'action', orderable: false, searchable: false}#endcan,
],
columnDefs: [
{
render: function (data, type, full, meta) {
return "<div class='text-wrap width-200'>" + data + "</div>";
},
targets: [3]
}
]
});
HTML Codes
<table class="table table-striped table-bordered responsive cdata-tables" style="width:100%">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Location</th>
<th>Description</th>
<th>Testimonial</th>
#can('view-actions')
<th width="100px">Action</th>
#endcan
</tr>
</thead>
<tfoot>
<tr>
<th>No</th>
<th>Name</th>
<th>Location</th>
<th>Description</th>
<th>Testimonial</th>
#can('view-actions')
<th width="100px">Action</th>
#endcan
</tr>
</tfoot>
</table>
Controller codes
public function index(Request $request)
{
if ($request->ajax()) {
$data = Client::latest()->get();
$this->user = Auth::user();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '';
if ($this->user->can('view-actions')){
$btn = '<i class="p-1 zmdi zmdi-edit"></i>';
$btn = $btn.' <i class="p-1 zmdi zmdi-delete"></i>';
};
return $btn;
})
->rawColumns(['action'])
->make(true);
}
return view('admin.pages.client');
}
I just keep getting "Processing..." when I load the file on the browser on the Datatable. Please help out.

how to fetch data from single table and show as multiple datatables using codeignator?

I'm fetching the data from the database. Want to show the data into multiple dataTables separated by column name (certification_scope).
This is the labels table in the database
Here is my Controller Clients.php
public function labels_data(){
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$query = $this->db->select('*')->where('clientid',get_client_user_id())->order_by('labelid','desc')->get(db_prefix().'labels');
$data = [];
foreach($query->result() as $r) {
$data[] = array(
$r->certification_scope,
$r->test_evidence,
$r->c_scope_desc,
$r->total_quantity,
$r->current_balance,
$r->project_name,
$r->serial_no_from,
$r->serial_no_to,
$r->used_quantity
);
}
$result = array(
"draw" => $draw,
"recordsTotal" => $query->num_rows(),
"recordsFiltered" => $query->num_rows(),
"data" => $data
);
echo json_encode($result);
exit();
}
This is the view labels.php
<div class="table-wrap">
<table id="item-list" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Certification Scope</th>
<th>Test Evidence</th>
<th>Scope Description</th>
<th>Total Quantity</th>
<th>Current Balance</th>
<th>Project Name</th>
<th>Serial No From</th>
<th>Serial No To</th>
<th>Used Quanity</th>
</tr>
</thead>
<tbody> </tbody>
</table>
</div>
This is the dataTable Ajax
$('#item-list').DataTable({
"ajax": {
url : "<?php echo base_url('clients/labels_data'); ?>",
type : 'GET'
}
});
Result for the above code is
Want to show the DataTables like this
Anyone can please help me to fix this.

Can't display the fetch data

I am new in ReactJS so fetching data isn't my expertise, so I'm trying to learn ReactJS with my PHP Native. The problem is fetching data is difficult for me. This code below there's no error but it won't display my data.
It can't display in my UI, the data is stayed in console(Network -> Response), i tried everything still can't output it. Can someone help?
import './Timesheet.css';
import React, { useState, useEffect } from 'react';
import { Table, Container, Button, } from 'react-bootstrap';
const Timesheet = () => {
const [item, setItem] = useState([]);
useEffect(() => {
fetch("http://localhost/WaterRefillingStation/app/controllers/timesheet_controller.php")
.then(response => response.json())
.then(
(result) => {
setItem(result);
}
)
}, [])
return (
<Container fluid>
<div className="d_flex my-4, text-uppercase">
<h1>
Timesheet Screen
</h1>
</div>
<div className="float-end, tryy" >
<Button variant="success" size="md">Time IN</Button>{' '}
<Button variant="secondary" size="md">Time OUT</Button>{' '}
<Button variant="danger" size="md">Logout</Button>
</div>
<Table striped bordered hover size="md" variant="dark" responsive="xl" >
<thead className="thead-dark">
<tr style={{textAlign: "center"}}>
<th>ID</th>
<th>Date</th>
<th>Profile Picture</th>
<th>Last Name</th>
<th>First Name</th>
<th>Designation</th>
<th>Time IN</th>
<th>Time OUT</th>
</tr>
</thead>
<tbody>
{item.map(item => (
<tr key={item}>
<td>{item.userID}</td>
<td>{item.birthday}</td>
<td>{item.profpic}</td>
<td>{item.lastname}</td>
<td>{item.firstname}</td>
<td>{item.designation}</td>
<td>{item.created_at}</td>
<td>{item.updated_at}</td>
</tr>
))}
</tbody>
</Table>
</Container>
);
}
export default Timesheet;
Ok, it seems that you have used .map in the wrong way. You can use it for an array, not an object. use this instead:
return (
<Container fluid>
<div className="d_flex my-4, text-uppercase">
<h1>
Timesheet Screen
</h1>
</div>
<div className="float-end, tryy" >
<Button variant="success" size="md">Time IN</Button>{' '}
<Button variant="secondary" size="md">Time OUT</Button>{' '}
<Button variant="danger" size="md">Logout</Button>
</div>
<Table striped bordered hover size="md" variant="dark" responsive="xl" >
<thead className="thead-dark">
<tr style={{textAlign: "center"}}>
<th>ID</th>
<th>Date</th>
<th>Profile Picture</th>
<th>Last Name</th>
<th>First Name</th>
<th>Designation</th>
<th>Time IN</th>
<th>Time OUT</th>
</tr>
</thead>
<tbody>
<tr key={item}>
<td>{item.userID}</td>
<td>{item.birthday}</td>
<td>{item.profpic}</td>
<td>{item.lastname}</td>
<td>{item.firstname}</td>
<td>{item.designation}</td>
<td>{item.created_at}</td>
<td>{item.updated_at}</td>
</tr>
</tbody>
</Table>
</Container>
if you want to have a table for each user (for example), you should to provide data like below in your backend:
{
data:[
{ USER1 Info},
{ USER2 Info},
// so on
]
}

Laravel: Export PDF from blade with image

I'm trying to export a table blade with an image to PDF Using TCPDF.
My Controller:
public function exportPDF(){
$users = User::orderBy('id','DESC')->get();
$view = View::make('admin.users.export',compact('users'));
$contents = (string) $view;
$contents = $view->render();
$pdf = new PDF();
$pdf::SetTitle('Users List');
$pdf::SetSubject('Users List');
$pdf::SetKeywords('User');
$pdf::SetTitle('User List');
$pdf::AddPage();
$pdf::writeHTML($contents, true, false, true, false, '');
$pdf::Output('users.pdf');
}
My Blade:
<html>
<head>
<body>
<h4>Users List</h4>
<table>
<tr>
<th>Image</th>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>Email</th>
<th>Role</th>
</tr>
#if($users->count())
#foreach($users as $user)
<tr>
<td><img src="{{asset('images/users/'.$user->image_path)}}"></td>
<td>{{$user->first_name}}</td>
<td>{{$user->last_name}}</td>
<td>{{$user->username}}</td>
<td>{{$user->email}}</td>
<td>{{$user->role}}</td>
</tr>
#endforeach
</table>
</body>
</html>
But when i want to get the pdf it take a long time and after that it gave me this message
TCPDF ERROR: [Image] Unable to get the size of the image:http://127.0.0.1:8000/images/users/1523518590.jpg
Thank's

How to make search with condition using ajax and codeigniter

I want to make search within specific condition using codeigniter framework
when I press inside my view the condition doesn't happen
($this->db->where('del_status','1');)
I think my problem in query to data base.
my data base model
public function SearchData($tablename,$data)
{
//select * from users where username like'%ss%' AND del_status ='1'
$this->db->select("*");
$this->db->from($tablename);
$this->db->like('username', $data);
$this->db->or_like('fullname', $data);
$this->db->or_like('email', $data);
$this->db->where('del_status','1');
$sql = $this->db->get();
return $sql->result();
}
this is my controller
public function Search()
{
if($this->input->post("action") =="searchonusers" )
{
$data=$this->input->post("data");
$this->load->model("DatabaseModel");
$serachdata=$this->DatabaseModel->SearchData("users",$data);
if(is_array($serachdata)&&count($serachdata)>0)
{
echo '<div class="col-lg-12">
<div class="userdataa table-responsive">
<table class=" table table-responsive table-bordered table-hover ">
<tr>
<th>id</th>
<th>user name</th>
<th>full name</th>
<th> image</th>
<th>email</th>
<th>type</th>
<th>status</th>
<th>date</th>
<th>time</th>
<th>delete</th>
<th>edit</th>
<th>Activate</th>
</tr>';
foreach ($serachdata as $user):
echo'<tr id="'."user".$user->id.'">
<td>'.$user->id.'</td>
<td>'.$user->username.'</td>
<td>'.$user->fullname.'</td>
<td><img class="img-responsive" height="40%" width="40%" src="'.base_url()."upload/".$user->image.'"></td>
<td>'.$user->email.'</td>';
$usertype='';
if($user->type== '1'){
$usertype=" <label class='label label-warning'>User</label>";
}else{
$usertype="<label class='label label-danger '> Admin</label>";
}
echo '<td>'.$usertype.'</td>';
$acativate='';
$status='';
$regstatus=$user->status;
if($regstatus == '0'){
$acativate='<button class="btn btn-success btn-xs showsta" status="'.$user->status.'" id="'.$user->id.'">enable</button>';
$status='<label class="label label-danger">disactive</label>';
}else{
$acativate='<button class="btn btn-xs btn-danger showsta" status="'.$user->status.'" id="'.$user->id.'">disable</button>';
$status='<label class=" label label-success">active</label>';
}
echo' <td>'.$status.'</td>
<td>'.$user->date.'</td>
<td>'.$user->time.'</td>
<td><button class="deleteuserfirst btn btn-danger btn-xs" id="'.$user->id.'"><span class="glyphicon glyphicon-trash"></span>delete</button></td>
<td><button type="button" class="btn btn-primary btn-xs edituser" id="'.$user->id.'" data-toggle="modal" data-target="#usereditorwe"><span class="glyphicon glyphicon-edit">Edit</button>
<td> '.$acativate.'</td>
</tr>';
endforeach;
echo'</table>
</div>
</div>';
}else{
echo '<div class="col-lg-12">
<div class="userdataa table-responsive">
<table class=" table table-responsive table-bordered table-hover">
<tr>
<th>id</th>
<th>user name</th>
<th>full name</th>
<th> image</th>
<th>email</th>
<th>type</th>
<th>status</th>
<th>date</th>
<th>time</th>
<th>delete</th>
<th>edit</th>
<th>Activate</th>
</tr>
<tr>
<td colspan="12">No Data Found</td>
</tr>
</table>
</div>';
}
}else{
exit();
}
}
}
this is my ajax code
$(".serchdata").on("click",function () {
var action="searchonusers";
var data=$("#dataa").val();
if(data ==""){
alert("Please Insert Your Search Data ... ")
}else{
$.ajax({
url:"<?php echo base_url("Users/Search")?>",
method: "post",
data:{action:action,data:data},
success:function (data) {
$(".userdata").remove();
$(".box").remove();
$("#pagination_link").remove();
$(".serchresult").html(data)
Table();
}
})
}
});
You are checking is_array on an object type result() :
if(is_array($serachdata)&&count($serachdata)>0)
to check for an array, use result_array() instead :
return $sql->result_array();
Please replace your model with that
public function SearchData($tablename,$data)
{
//select * from users where username like'%ss%' AND del_status ='1'
$this->db->select("*");
$this->db->from($tablename);
$this->db->like('username', $data);
$this->db->or_like('fullname', $data);
$this->db->or_like('email', $data);
$this->db->where('del_status','1');
$sql = $this->db->get()->result_array();
return $sql;
}

Categories