i am trying to pass an id in the checkbox while generating datatables,My codes are as follows
Controller
public function list_view() {
$this->datatables->select('c.id,,c.image,c.first_name,c.email_primary,c.contact_mobile,c.contact_home,cn.country_name,c.unique_id,c.title')
->from('contact c')
->join('country cn','cn.id=c.country_id','left');
$this->datatables->add_column('action', '<input id="list-chk_" class="top-label" type="checkbox" value=c.id />', 'id');
$this->datatables->add_column('sync', '<span class="list-sync" ></span>', 'c.id');
$this->datatables->add_column('email', '<span class="list-mail" ></span>', 'c.id');
echo $this->datatables->generate('json');
}
I just want to pass c.id along with the checkbox,as I need to have further manipulation using that id in datatables,
Thank You .
You can just pass the variable as {{ c.id }}
Example:
$result = DB::table('users')
->select('users.id as id','users.first_name', 'users.last_name', 'users.email', 'users.username', 'users.activated');
return Datatables::of($result)
->add_column('actions', '<a aid="{{ $id }}" style="cursor: pointer;" class="edit_admin" data-toggle="modal" data-target="#edit_admin_modal" title="Edit Admin" ><i class="fa fa-pencil"></i></a> <a style="cursor: pointer;" aid="{{ $id }}" class="delete_admin" data-toggle="modal" data-target="#delete_admin_modal" title="Remove Admin" ><i class="fa fa-times-circle"></i></a> <a aid="{{ $id }}" style="cursor: pointer;" class="enable_disable_admin" title="Enable/Disable Admin" ><i class="fa fa-check-circle-o"></i></a>')
->remove_column('id')
->make();
Related
i'm new to laravel. So in this case i'm trying to sum child data and pass it to parent nominal column inside parent index #foreach,
the thing is idk how to declare the parent id inside controller
here's my parent index which i want to show the sum child each parent id in #foreach
here's my child index that i've already sum it, i want to show it into parent #foreach nominal column as well
here's my SPJCreateController which is parent controller
class SPJCreateController extends Controller
{
public function index(Request $request, $id)
{
$kegiatan = DPAKegiatan::with('dpatahun')->findorfail($id);
$data = SPJCreate::with('spjcreatedetail')
->where('id_dpakegiatan', $kegiatan->id)
->orderBy('created_at', 'asc')
->paginate(10);
$total = SPJCreateDetail::where('id_spj', $request->id)->sum('nominal');
return view('spj.kegiatan.create.data', compact('kegiatan','data','total'));
}
}
in this controller i declare variable $total which is gonna sum column 'nominal' in SPJCreateDetail Model but idk how to declare where inside of it since $id is the id of grandparent
It returns 0 inside nominal column if i put $request->id inside where
however it shown when i call it manually like this
$total = SPJCreateDetail::where('id_spj', 6)->sum('nominal');
my parent index after i put where id manually
But i don't want that because i want my id to be dynamically called and sum eachrow of it
My SPJCreate Model which is parent model
class SPJCreate extends Model
{
protected $primaryKey = 'id';
protected $table = 'spj';
protected $fillable = ['id_dpakegiatan','tipebelanja','tipebelanjadetail'];
public function dpakegiatan()
{
return $this->belongsTo(DPAKegiatan::class, 'id_dpakegiatan');
}
public function spjcreatedetail()
{
return $this->hasMany(DPAKegiatan::class, 'id');
}
}
My SPJCreateDetail model which is child model
class SPJCreateDetail extends Model
{
protected $primaryKey = 'id';
protected $table = 'spj_detail';
protected $fillable = ['id_spj','id_listbelanja','ket','penerima','nominal','ppn','pph21','pph22','pph23'];
public function spjcreate()
{
return $this->belongsTo(SPJCreate::class, 'id_spj');
}
}
Sorry for my bad english i hope u guys can help me because im stuck for a month trying to figure it out :( , Thank you.
i dd inside my controller
$total = dd(SPJCreateDetail::where('id_spj', $request->id)->sum('nominal'));
it returns like this
image
however
$total = dd(SPJCreateDetail::where('id_spj', 6)->sum('nominal'));
it return the value correctly
image
here's my web routes
// --Here's my grandparent routes-- //
Route::get('/spj/kegiatan&id={spjkeg}', 'SPJKegController#index')->name('spjkeg')->middleware('auth');
// ------------- //
// --Here's my Parent Route Which is Gonna show my child sum-- //
Route::get('/spj/kegiatan/create&id={spjcreate}', 'SPJCreateController#index')->name('spj')->middleware('auth');
Route::post('/spj/kegiatan/create/store&id={spjcreate}', 'SPJCreateController#store')->name('spj.store')->middleware('auth');
Route::post('/spj/kegiatan/create/updt&id={spjcreate}', 'SPJCreateController#update')->name('spj.update')->middleware('auth');
Route::delete('/spj/kegiatan/create/del&id={spjcreate}', 'SPJCreateController#destroy')->name('spj.destroy')->middleware('auth');
// ------------ //
// --Here's the child route-- //
Route::get('/spj/kegiatan/create/detail&id={spjdet}', 'SPJCreateDetailController#index')->name('spj.detail')->middleware('auth');
Route::post('/spj/kegiatan/create/detail/store&id={spjdet}', 'SPJCreateDetailController#store')->name('spj.detail.store')->middleware('auth');
Route::post('/spj/kegiatan/create/detail/updt&id={spjdet}', 'SPJCreateDetailController#update')->name('spj.detail.update')->middleware('auth');
Route::delete('/spj/kegiatan/create/detail/del&id={spjdet}', 'SPJCreateDetailController#destroy')->name('spj.detail.destroy')->middleware('auth');
// ------------ //
Here's my View.blade.php
#php $no = 0; #endphp
#forelse($data as $spj => $result)
#php $no++; #endphp
<tr>
<td class="text-center">{{ $spj + $data->firstitem() }}.</td>
<td class="text-center"><b>{{ $result->tipebelanjadetail }}</b></td>
<td class="text-center">{{ $total }}</td>
<td class="text-center">
<div class="dropdown">
<button data-id="{{ $result->id }}" data-idtipebelanja="{{ $result->tipebelanja }}" data-idtipebelanjadetail="{{ $result->tipebelanjadetail }}" data-toggle="modal" data-target="#editspj" class="btn btn-success btn-s" style="background-color:#138496;border-color:#138496"><i class="fad fa-pencil"></i></button>
<button data-id="{{ $result->id }}" data-toggle="modal" data-target="#deletespj" class="btn btn-success btn-s" style="background-color:#eb1c0f;border-color:#eb1c0f"><i class="fad fa-trash"></i></button>
<button type="button" data-toggle="dropdown" class="btn btn-warning dropdown dropdown-toggle" aria-haspopup="true" aria-expanded="false"><i class="fad fa-info"></i></button>
<div class="dropdown-menu">
<a class="dropdown-item" href="{{ Route('spj.detail', $result->id)}}"><i style="padding-right:5px" class="fad fa-eye"></i>Detail</a>
</div>
</div>
</td>
</tr>
#empty
<tr>
<td colspan="7" class="text-center">Tidak ada data <i class="far fa-frown fa-5"></i></td>
</tr>
#endforelse
Try this.
public function index($spjcreate)
{
$kegiatan = DPAKegiatan::with('dpatahun')->findorfail($spjcreate);
$data = SPJCreate::with('spjcreatedetail')
->where('id_dpakegiatan', $kegiatan->id)
->withCount(['spjcreatedetail AS total' => function ($query) {
$query->select(DB::raw('SUM(nominal) as total'));
}])
->orderBy('created_at', 'asc')
->paginate(10);
return view('spj.kegiatan.create.data', compact('kegiatan','data'));
}
#php $no = 0; #endphp
#forelse($data as $spj => $result)
#php $no++; #endphp
<tr>
<td class="text-center">{{ $spj + $data->firstitem() }}.</td>
<td class="text-center"><b>{{ $result->tipebelanjadetail }}</b></td>
<td class="text-center">{{ $result->total }}</td>
<td class="text-center">
<div class="dropdown">
<button data-id="{{ $result->id }}" data-idtipebelanja="{{ $result->tipebelanja }}" data-idtipebelanjadetail="{{ $result->tipebelanjadetail }}" data-toggle="modal" data-target="#editspj" class="btn btn-success btn-s" style="background-color:#138496;border-color:#138496"><i class="fad fa-pencil"></i></button>
<button data-id="{{ $result->id }}" data-toggle="modal" data-target="#deletespj" class="btn btn-success btn-s" style="background-color:#eb1c0f;border-color:#eb1c0f"><i class="fad fa-trash"></i></button>
<button type="button" data-toggle="dropdown" class="btn btn-warning dropdown dropdown-toggle" aria-haspopup="true" aria-expanded="false"><i class="fad fa-info"></i></button>
<div class="dropdown-menu">
<a class="dropdown-item" href="{{ Route('spj.detail', $result->id)}}"><i style="padding-right:5px" class="fad fa-eye"></i>Detail</a>
</div>
</div>
</td>
</tr>
#empty
<tr>
<td colspan="7" class="text-center">Tidak ada data <i class="far fa-frown fa-5"></i></td>
</tr>
#endforelse
I'm working with role based permission using Zizaco Entrust package with yajra datatables.
when i'm giving permission to some users i have to touch datatables also.
This is my code ,
Controller.php
Datatables::of(User::where('company_id',$company_id)->get())
->addColumn('action', '#permission('user-edit')
View#endrole
Edit')
->make(true);
when i use permission inside datatables it is getting error , any one having idea to solve this ??same question in
yajra datatables and entrust role permission laravel
Entrust::can() checks if the user is logged in, and then if user has the permission. If the user is not logged the return will also be false.
Check below code:
Datatables::of(User::where('company_id',$company_id)->get())
->addColumn('action', function($company){
$action = '';
if (!Entrust::can('user-edit')) {
$action = 'View';
}
$action .= 'Edit';
return $action;
})
->make(true);
Made correction into code for {{}} and quotes issue.
You can also follow this technique
->addColumn('actions', function($admin) {
return view('admin.user.partials.admin_action', compact('admin'))->render();
})
And in your blade admin_action, you can write your view code as follows
#permission("admin-edit")
<a href="#" title="Edit"
class="btn-sm btn-primary editData"
data-id="{{ $admin->id }}"
data-fname="{{ $admin->f_name }}"
data-lname="{{ $admin->l_name }}"
data-email="{{ $admin->email }}"
data-redeem_manager="{{ $admin->redeem_manager }}"
data-mobile="{{ $admin->mobile }}"
data-role_id="{{ $admin->role_id }}"
><i class="fa fa-edit"></i> </a>
#endpermission
Entrust::can() not working with me causes an error.
but \Auth::user()->can('user-edit') works well with me
return datatables()->of($data)
->addColumn('actions', function ($data) {
$button = '<a class="btn btn-sm btn-info" href="' . route('users.show', $data->id) . '" >Show <i class="fa fa-eye"></i></a>';
if (\Auth::user()->can('user-edit')) {
$button .= ' <a class="btn btn-sm btn-primary" href="' . route('users.edit', $data->id) . '" >Edit <i class="fa fa-edit"></i></a>';
}
if (\Auth::user()->can('user-delete')) {
$button .= ' <a id="' . $data->id . '" class="delete btn btn-sm btn-danger" href="#" >Delete <i class="fa fa-trash"></i></a>';
}
return $button;
})
->rawColumns(['actions'])
->make(true);
Iwant to insert this code
if($transaction_user_define_fields->udf_type == 'Selectbox'){
echo '<i class="fa fa-list fa-lg text-primary pull-left" data-toggle="tooltip" data-placement="right" title="View option" onclick="viewUDFOPT('.$transaction_user_define_fields->tran_udf_col_id.')"></i>';
}
inside
$this->table->add_row()
but im getting an error .please help
if($transaction_user_define_fields->udf_type == 'Selectbox'){
$this->table->add_row('<i class="fa fa-list fa-lg text-primary pull-left" data-toggle="tooltip" data-placement="right" title="View option" onclick="viewUDFOPT('.$transaction_user_define_fields->tran_udf_col_id.')"></i>');
}
This would make the tag clickable, if you want to make the entire cell clickable you will have to set it as a param in add row
as per codeigniter documentation
$cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);
$this->table->add_row($cell, 'Red', 'Green');
// generates
// <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
It shows 500 internal server error when retrieving data through ajax. But the whole code works well in my localhost. And i am facing this error for the first time, so i am not sure whether this error is caused due to fetching data through AJAX. If it is not the correct reason please get me the correct reason.
this is my coding
$.ajax({
type:"POST",
url:"api_fle/get_post",
data:{u_id:u_id,type:'all13'},
success:function(response){
if(response!=0){
var parsed = $.parseJSON(response);
date = new Array();
events = new Array();
$.each(parsed,function(i,parsed){
if(parsed.shred.length>15){var shred=jQuery.trim(parsed.shred).substring(0, 14) + '...';} else{var shred=parsed.shred;}
if(parsed.cmpny_name == parsed.shred){var sharedd=parsed.cmpny_name; var sha=""; var pic=parsed.pro_pic;}else{var sharedd=shred; var sha=' shared <input type="hidden" id="who_hid_id" value="'+parsed.id+'">'+parsed.cmpny_name+"'s Event"; var pic=parsed.pic;}
date[i]=parsed.SharedDate;
events[i]='<div class="col-md-10 post" style="background:#FFF"><span class="company-logo-small"><img src="'+pic+'" style=" width: 60px; height: 60px;"></span>'
+'<span class="fullhead"><span class="posted-name"><a id="who_shred" style="color:#fff;cursor:pointer;"><input type="hidden" id="who_hid_id" value="'+parsed.id+'"><span itemprop="hiringOrganization">'+sharedd+'</span></a>'+sha+'</span></span>'
+'<span class="post-status" style="color:#fff;">'+prettyDate(parsed.SharedDate)+'</span><div class="post-inner"><div class="col-md-12"><div class="panel panel-default event">'
+'<div class="panel-heading title">'+parsed.name+'</div><ul class="list-group"><li class="list-group-item"><i class="fa fa-globe"></i>'+parsed.location+'</li>'
+'<li class="list-group-item"><i class="fa fa-calendar-o"></i>'+parsed.date+'</li><li class="list-group-item"><i class="fa fa-clock-o"></i>'+parsed.time+'</li>'
+'<li class="list-group-item"><i class="fa fa-users"></i>Attendees '+parsed.attendies+'</li></ul><ul class="list-group"><div class="panel-body"><p>'+parsed.decs+'</p>'
+'<a class="btn btn-xs btn-info pull-left" target="_blank" href="eventview?evnt_id='+parsed.evnt_id+'">View</a>  '
+'<i class="fa fa-fw fa-facebook-square" style="font-size:20px;"></i>'
+'<a class="twitter popup" href="pagelink?evnt_id='+parsed.evnt_id+'" target="_blank"><i class="fa fa-fw fa-twitter-square " style="font-size:20px;"></i></a>'
+'<a class="twitter popup" href="pagelink?evnt_id='+parsed.evnt_id+'" target="_blank">'
+'<i class="fa fa-fw fa-linkedin-square" style="font-size:20px;"></i></a><a class="twitter popup" href="pagelink?evnt_id='+parsed.evnt_id+'" target="_blank">'
+'<i class="fa fa-fw fa-google-plus" style="font-size:20px;"></i></a></div></ul><div id="img"></div><div class="clearfix"></div></div></div></div></div>';
});
}
});
-------------------------
page : get_post
--------------------
if($_POST['type']=='all13'){
$update_time=mysql_query("UPDATE `share_post` SET `sharedDate`='".$_POST['time']."' WHERE `frm_id` = 'U005114608238'");
$sql=select_query("SELECT s.id,e.u_id,e.cmpny_name,n.pro_pic as pic,l.pro_pic,l.level as lv,m.evnt_id,m.name,m.location,m.decs,m.time,
m.date,m.attendies,s.frm_id,s.is_important,s.shred,s.SharedDate,s.lvl FROM employer_info e,login l,login n,`event` m, share_post s WHERE n.u_id=s.frm_id and e.u_id=l.u_id and m.u_id=l.u_id and m.evnt_id=s.post_id and s.to_id='".$_POST['u_id']."' order by s.id desc");
$count=count($sql);
$response=array();
for($i=0;$i<$count;$i++){
array_push($response,$sql[$i]);
}
echo json_encode($response);
}
when i have inspected the error , i got something like in this screenshot
This is my Controller:
public function anyData()
{
$myID = Auth::guard('cashier')->user()->id;
$players = User::where('parent_id','=', $myID)
->where('delete', '=', 1)
->orderBy(DB::raw('LENGTH(name),name'))
->get(['id', 'name', 'score', 'alarm', 'bonus_from_percentage']);
return Datatables::of($players)
->addColumn('actions4', function ($players) {
return
'
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#actions-modal" class="open-playerID btn btn-warning" data-toggle="modal"
data-target="#actions-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.SETTINGS').'"><span class="glyphicon glyphicon-cog"></span></a>
';
})
->addColumn('actions3', function ($players) {
return
'
<button type="button" class="'.changeClass($players->alarm).'"
value="OK" onclick="Enable('.$players->id.','.$players->alarm.')" id="enable"/><span class="glyphicon glyphicon-ok-circle"></span></button>
';
})
->addColumn('actions2', function ($players) {
return
'
<a href="#" data-toggle="modal" data-id="'.$players->id.'" data-target="#changeplayerpassword-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.CHANGEPLAYERPASS').'" class="playeridchangepass btn btn-warning">
<span class="glyphicon glyphicon-edit"></span></a>
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#delete-modal" class="open-delete btn btn-warning" data-toggle="modal"
data-target="#delete-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.DELETE').'" ><span class="glyphicon glyphicon-remove"></span></a>
';
})
->addColumn('actions', function ($players) {
return
'<div class="btn-group text-center">
<button onclick="changeValueIn(this);" class="btn btn-primary btn-sm" style="width: 50px;" data-score-id="' . AppHelper::ToEuroC($players->score) . '" data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#In">IN</button>
<button onclick="changeValue(this)"class="btn btn-danger btn-sm" data-score-id="' . AppHelper::ToEuroC($players->score) . '"data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#Out">OUT</button>
</div>
';
})
->addColumn('score', function ($players) {
return AppHelper::ToEuroC($players->score);
})
->addColumn('bonus_from_percentage', function ($players) {
return AppHelper::ToEuroC($players->bonus_from_percentage);
})
->addColumn('players', function ($players) {
return ($players->name) . ' ' .dikOn($players->id);
})
->make(true);
}
This script i run to refresh table each 5 seconds:
// REFRESH TABLE
function refreshptable() {
if (table != null)
table.ajax.reload(null, false);
setTimeout(refreshplayers
, 5000);
}
wen i upload on server my laravel project (i have active around 300 Users online)
CPU load going 200+
On my old PHP code (No Laravel) CPU load is 10-15
its something wrong with datatables or is laravel ?
i check the request is same no loops and all querys is same the only difference is laravel..
on TOP -C laravel use 3% my old code use 0.3% ..