Undefiend Variable In yajra - php

Below is the code to show current exam to student.If he already applied then he cannot apply for the same.When user logged in I stored there value in Session.put('Student.id',$user[0]->id).Now when i run below code I am getting the error:
undefiend variable:$admission_id in alert(I used YAJRA DATATABLE).
**$admission_id** = Session::get('student.id');
$exam=DB::table('exam')->where('id_exam_category','2')->get();
return Datatables::of($exam)
->addIndexColumn()
->addColumn('exam_name', function($row){
return $row->exam_name;
})
->addColumn('action',function($row){
$stu=DB::table('exam_student')
->where('id_exam',$row->id)
->where('admission_id',**$admission_id**)
->get();
if(count($stu) > 0) {
$ret='<button class="btn btn-block btn-default" disabled>Appered <i class="fa fa-caret-right"></i></a>';
}else{
$ret='Start <i class="fa fa-caret-right"></i>';
}
return $ret;
})
->rawColumns(['action'])
->make(true);
Even if i print $admission_id...It is printing. Session.id has student id and not empty..
Any Suggesstion ?
Thank You

Related

Laravel Yajra DataTable Error if I added more than 1 record

I've create datatatable with Yajra on Laravel, here I want to display data on the table, here is my source code :
$model = SaranaPrasaranaRuang::with('jenis_ruang')->where('dibuat_oleh',$this->user->id)->get();
$dTable = Datatables()->of($model);
$dTable = $dTable->addIndexColumn()
->editColumn('jenis_ruang',function($ruang){
$jenis_ruang = $ruang->jenis_ruang->jenis_ruang;
return $jenis_ruang;
})
->editColumn('nama_ruang',function($data){
return $data->nama_ruang;
})
->addColumn('standard_prasarana',function($data){
$btn = '<button class="btn btn-warning detail_prasarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Prasarana</button>';
return $btn;
})
->addColumn('standard_sarana',function($data){
$btn = '<button class="btn btn-warning detail_sarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Sarana</button>';
return $btn;
})
->addColumn('action',function($data){
$btn = "";
$btn .= '<i class="fa fa-pencil"></i> Edit ';
$btn .= '</i> Hapus ';
$btn .= '<button class="btn btn-info" onclick=detailRuang('.$data->id.')><i class="fa fa-eye"></i> Detail</button>';
return $btn;
})
->rawColumns(['standard_prasarana','standard_sarana','action']);
return $dTable->make(true);
My code above is working fine if my record only 1, but when I added one more record on my table , Datable give me response json like this :
"error": "Exception Message:\n\nUndefined index: "
I've try with eloquent(),query() but still didn't work
Anyone can help me out ?
Try this
$model = SaranaPrasaranaRuang::with('jenis_ruang')->where('dibuat_oleh',$this->user->id)->get();
return DataTables::of($model)
->editColumn('jenis_ruang',function($ruang){
$jenis_ruang = $ruang->jenis_ruang->jenis_ruang;
return $jenis_ruang;
})
->editColumn('nama_ruang',function($data){
return $data->nama_ruang;
})
->addColumn('standard_prasarana',function($data){
$btn = '<button class="btn btn-warning detail_prasarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Prasarana</button>';
return $btn;
})
->addColumn('standard_sarana',function($data){
$btn = '<button class="btn btn-warning detail_sarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Sarana</button>';
return $btn;
})
->addColumn('action',function($data){
$btn = '';
$btn .= '<i class="fa fa-pencil"></i> Edit ';
$btn .= '</i> Hapus ';
$btn .= '<button class="btn btn-info" onclick=detailRuang('.$data->id.')><i class="fa fa-eye"></i> Detail</button>';
return $btn;
})
->rawColumns(['standard_prasarana','standard_sarana','nama_ruang','jenis_ruang','action']);
->addIndexColumn()
->make(true);

OneToOne Relationship in DataTables

I am using datatables from https://datatables.net/examples/server_side/simple.html and I am able to display all the information to the tables.
Now I have a column which stores foreign key as shown below.
The other table which is being pointed by source_of_member is
so here i have these kind of relationship and in frontend datatables my data are visualized as
Howeber here instead of foreign keys in Member Source column I want those Name to appear from another table which matches the id
my member model is as:
class Member extends Model
{
protected $fillable = ['name','mobile_number','organization_name','source_of_member','relationship_manager','referred_by'];
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'id');
}
}
and my memberSource model is as:
class MemberSource extends Model
{
protected $fillable = ['name'];
public function member()
{
return $this->hasOne('App\Member', 'id');
}
}
what i tried is:
public function getdata()
{
$members = Member::all();
return Datatables::of($members)
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
}
i dont know how to do this any help would be greatly appreciated.
In your Member model:
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'source_of_member');
}
In Controller:
$members = Member::with('memberSource')->get();
return Datatables::of($members)
->addColumn('source_of_member', function ($member) {
$member_name = '';
if(!empty($member->memberSource->name)){
$member_name = $member->memberSource->name;
}
return $member_name;
})
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
With the relationships working properly, you can simply edit the column in the datatable:
$members = Member::with('memberSource');
return Datatables::of($members)
->editColumn('source_of_member', function ($member) {
return $member->memberSource->name;
})
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
However, your relationship configuration seems wrong, because your foreign key is called source_of_member and therefore laravel can't automatically detect it.
In your Member model:
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'source_of_member');
}
In your MemberSource model:
public function member()
{
return $this->belongsTo('App\Member', 'source_of_member');
}

Yajra Datatables and Entrust package

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);

Laravel datatables not returning html link

I have the following function
public function getTasks()
{
$users = User::select(['id','users_full_names', 'email', 'users_telephone_number', 'users_credits', 'users_last_seen_carbon_object','users_profile_picture','updated_at']);
return Datatables::of($users)
->addColumn('action', function ($user) {
return '<i class="glyphicon glyphicon-edit"></i> Edit <i class="glyphicon glyphicon-trash"></i> Delete <i class="glyphicon glyphicon-ban-circle"></i> Moderate <i class="glyphicon glyphicon-search"></i> View <i class="glyphicon glyphicon-user"></i> Impersonate';
})->editColumn('updated_at', function(User $user) {
$dt = $user->updated_at->toDateTimeString();
$datetime = Carbon::parse($dt);;
return $datetime->diffForHumans();
})->editColumn('users_profile_picture', function(User $user) {
$picture = $user->users_profile_picture;
return 'View Image';
})->make(true);
}
that i am using to get the pictures stored in the database. This is the particular section that is creating the link
->editColumn('users_profile_picture', function(User $user) {
$picture = $user->users_profile_picture;
return 'View Image';
})->make(true);
but the link generated looks like this
View Image
instead of a clickable html link.
How can i fix this?.
Please use rawColumns as used below,
->editColumn('users_profile_picture', function(User $user) {
$picture = $user->users_profile_picture;
return 'View Image';
})->rawColumns(['users_profile_picture'])->make(true);
Please refer, https://github.com/yajra/laravel-datatables/issues/949

CodeIgniter ajax post only working once

Hello I'm trying to delete a row in the database using ajax post but the problem is that the row gets deleted only once per page load. The row get detach from the HTML DOM everything I press the delete button, but the database only deletes one record. I want to delete therecord in the database every time I press the delete button. How can I achieve this ?
My view
<tbody id="tbody"><?php
$count = 1;
foreach($rows as $row):?>
<tr>
<td><?=$count?></td>
<td><a href="basic_table.html#">
<?=$row->FirstName." ".$row->LastName?></a>
</td>
<td class="hidden-phone"><?=$row->Email?></td>
<td><?=$row->Password?></td>
<td><span class="label label-warning label-mini">
<?=date("m/d/Y", strtotime($row->Created))?></span>
</td>
<td>
<button class="btn btn-success btn-xs">
<i class="fa fa-check"></i></button>
<button class="btn btn-primary btn-xs">
<i class="fa fa-pencil"></i>
</button>
<button id="delete" data-id="<?=$row->id?>"
data-url="<?=base_url()?>" class="btn btn-danger
btn-xs"><i class="fa fa-trash-o "></i>
</button>
</td>
</tr><?php
$count++;
endforeach?>
</tbody>
script
$(document).ready(function()
{
$("#tbody").on('click','#delete', function()
{
var id = $("#delete").data("id");
var url = $("#delete").data("url");
$.post(url + "users/delete", { id : id, cache: false } , function()
{
},"json");
$(this).closest('tr').detach();
});
});
Please give the codeigniter controller script
You must change to anchor tag and add preventDefault() to prevent reloading page
$(document).ready(function(){
$("#tbody").on('click','#delete', function(e){
//You must add e.preventDefault() to avoid reloading page
e.preventDefault();
var id = $("#delete").data("id");
var url = $("#delete").data("url");
$.post(url + "users/delete", { id : id, cache: false } , function(){
},"json");
$(this).closest('tr').detach();
});
});

Categories