yajra DataTable with join not working in laravel 5? - laravel

relation of customer with job
public function customer() {
return $this->belongsTo('App\Customer','customerid');
}
public function jobs(){
return $this->hasMany('App\Job','customerid');
}
in controller
protected function getJobs(){
$jobs = Job::Join('customer','jobs.customerid','=','customer.id')
->select(array('jobs.id','customer.firstname','customer.lastname','jobs.jobstatus','jobs.trialdate','jobs.deliverydate'));
return Datatables::of($jobs)
->addColumn('action', '<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" title="Edit" href="{{ URL::to(\'updatejob/\'.$id) }}"><i class="fa fa-pencil"></i></a>')
->make();
}
it throw following error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'order clause' (SQL: select `jobs`.`id`, `customer`.`firstname`, `customer`.`lastname`, `jobs`.`jobstatus`, `jobs`.`trialdate`, `jobs`.`deliverydate` from `jobs` inner join `customer` on `jobs`.`customerid` = `customer`.`id` order by `0` asc limit 10 offset 0)
i'm stuck in this issue from 2 day please help me to get out from this

I think you missed "order by" in your query, try this :
protected function getJobs(){
$jobs = Job::Join('customer','jobs.customerid','=','customer.id')
->select(array('jobs.id','customer.firstname','customer.lastname','jobs.jobstatus','jobs.trialdate','jobs.deliverydate'))
->orderBy('customer.lastname')->get();
return Datatables::of($jobs)
->addColumn('action', '<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" title="Edit" href="{{ URL::to(\'updatejob/\'.$id) }}"><i class="fa fa-pencil"></i></a>')
->make();
}

I simply update the composer ->php composer.phar update
Its working fine now
Thanks

Related

Laravel: How to set join to get data from two different tables and pass it to Yajra datatable

I'm trying to load data into Yajra datatables in laravel from two different tables. In my module table Ive kept membership_id as key to maintain one to many relationship, and now I'm trying to show membership name in modules yajra datatable.
My Controller code:
function module(Request $request,Module $module)
{
$memberships = Membership::where('status', true)->get();
if ($request->ajax()) {
$data = Module::select('*');
return \DataTables::of($data)->addColumn('action', function($data) {
return '<a class="btn btn-sm btn-outline-secondary" id="getEditmoduleData" data-id="'.$data->id.'" title="Edit"><i class="fa fa-edit"></i></a>
<a class="btn btn-sm btn-outline-danger js-sweetalert" title="Delete" data-id="'.$data->id.'" data-toggle="modal" data-target="#DeletemodulesModal" class="btn btn-danger btn-sm" id="getDeleteId"><i class="fa fa-trash-o"></i></a>';
})->addColumn('membership', function () {
return
})->editColumn('created_at', function ($request) {
return $request->created_at->format('Y-m-d'); // human readable format
})->filterColumn('created_at', function ($query, $keyword) {
$query->whereRaw("DATE_FORMAT(created_at,'%Y-%m-%d') like ?", ["%$keyword%"]); //date_format when searching using date
})->rawColumns(['action'])->make(true);
}
return view("admin.module.module")->with(['memberships' => $memberships]);
}
Basically I'm not able to figure it out how to do join here $data = Module::select('*');
I've checked different links for solution, but no success. Any kind of help is welcome.
how to get data from two different join tables
How to Join two different tables in Laravel
Laravel yajra/datatables & yajra/datatables button
laravel Yajra Datatable

Yajra Ajax Datatables Including Dynamic URL with parameter

I am using Yarja datatables for quite a complex table set and also have the ajax part returning two buttons:
{
$user = Auth::user();
$cl = $user->client_id;
$jb = DB::table('job')
->join('job_status', 'job.jobStatus_id', '=', 'job_status.id')
->join('customers', 'job.customer_id', '=', 'customers.id')
->join('users', 'job.operative_id', 'users.id')
->where('job.client_id', $cl)
->select(['job.id as id', 'job_status.status as status', 'job.customer_id as customer_id', 'customers.customer as customer', 'users.name as operative','job.address as address','job.postcode as postcode','job.slug as slug','job_status.id as jobStatusID'])
->get();
return Datatables::of($jb)
->addColumn('action', function($pubs){
$btn = '<div style="float:right">
<i class="fas fa-book" ></i><i class="fas fa-edit" ></i></div>';
return $btn;
})
->make(true);
}
This works fine but now I want to add an action to the buttons, initially the edit, which is the route and the id of the row.
As you can see I have replaced the # with the route so I have
"admin\jobView"
but I cannot seem to work out a way of adding a field from the query (specifically jb->id) so that the action would be something like
admin\jobView\123
Just can't seem to get it and would greatly appreciate some help!
$pubs should contain your job, so you should be able to use $pubs->id, to get the job id.
What I would do though is replace this giant string with a view so it becomes easier to maintain:
return Datatables::of($jb)
->addColumn('action', function($job) {
return (string)view('admin.jobs.action', compact('job'));
})
->rawColumns(['action'])
->make(true);
Create a view, admin/jobs/action.blade.php for example:
<div style="float:right">
<a href="{{ route('admin.jobview', $job->id) }}" class="btn btn-outline-secondary btn-xs" title="show details" style="margin-right:.5em;font-size:.75em">
<i class="fas fa-book"></i></a>
<a href="{{ route('admin.jobview', $job->id) }}" class="btn btn-outline-secondary btn-xs" title="show details" style="margin-right:.5em;font-size:.75em">
<i class="fas fa-edit"></i>
</a>
</div>

How to update column in database in blade.php file Laravel

Hello I have row in a database table have an id and name and etc.. and active = 1
I want in blade.php file when someone click a button it change into 0 in 1 click
What I tried is {{$referral_detail->update('active', 1)}}
And
{{$referral_detail->IDT->where('active', 0)->update('active', 1)}}
Function in Controller is
public function DriverReferAll()
{
$query = DriverReferralDiscount::where([['referral_driver_id', '!=', 0],
['referral_sender_id', '!=', 0], ['active', '<>', 1]
]);
$referral_details = $query->paginate(25);
return view('merchant.driver.driver_referall', compact('referral_details', 'query'));
}
I got it thedit('{{$referral_detail->IDT->update(['active' => 1])}}')
and function
function thedit(a) {
$("input:submit").val(a);
}
But when I do it , it change the first 2 column not only 1 tho I put the specific id any idea?
its in onClick""
<span onClick=" thedit('{{$referral_detail->IDT->update(['active' => 1])}}')
" data-target="#addMoneyModel"
data-toggle="modal" id="{{ $referral_detail->GetterDriver->id }}"><a
data-original-title="Add Money"
data-toggle="tooltip"
id="{{ $referral_detail->GetterDriver->id }}" data-placement="top"
class="btn text-white btn-sm btn-success menu-icon btn_detail action_btn"> <i
class="fa fa-money-bill"></i> </a></span>
For that you must create a link in your blade template when that link is clicked It must have an href which point to the laravel route which will perform the update of your active state.
<a href="{{ route('your_route_name', ['id' => $referral_detail->id]) }}>Change</a>
the route can be define like this
Route::get('/route_path/{id}', 'ControllerName#action_name')->name('route_name');
Or If you want the browser to not refresh you can perform and Ajax

Having issue while sorting and pulling large data (yajra datatables)

I'm having an issue while pulling large data from yajra datatable server-side while applying the column sorting too.
For column sorting, I've used get() in the query builder. If I avoid it and use orderBy, it works fine and fetches records faster but the issue is column sorting doesn't work then.
$query = DB::table('clients');
if(Auth::user()->role_id==2)
{
$clients= $query->whereRaw('user_id',Auth::id());
}
else if(Auth::user()->role_id==3)
{
$getCompanyId = User::where('id',Auth::id())
->first();
$clients= $query->whereRaw('user_id',$getCompanyId['added_by']);
}
$clients= $query->orderBy('id');
$clients = $query->select();
$clients = $query->get();
$datatables = datatables()->of($clients)
->addColumn('action', function ($clients) {
return '<button class="btn btn-xs buttonhover" data-id='.$clients->id.' data-backdrop="static" onclick="editClient(this);"><i class="fa fa-edit" aria-hidden="true"></i> Edit</button>
<button class="btn btn-xs buttonhover2" data-id='.$clients->id.' onclick="viewDetails(this);"><i class="fa fa-eye" aria-hidden="true"></i> View</button>
<button class="btn btn-xs buttondeletehover"data-id='.$clients->id.' onclick="confirmAlertBox(this)"><i class="fa fa-trash" aria-hidden="true"></i> Delete</button>';
})
->make(true);
return $datatables;
Don't use of in datatables and don't get() data. Instead use eloquent to generate datatables object and query the sellect:
$users = User::query();
return DataTables::eloquent($users)
->toJson();
Or just use order by to generate eloquent object:
$users = User::orderBy('id');
Eloquent Data Source

Yajra DataTable addColumn

I use yajra datatable but i've get problem when I use the method "addColumn".
the one I use that method is work properly, but the other doesn't
this is my source code :
->addColumn('action', function($arrProduct){
return
'<center><a class="btn btn-success btn-sm" href="'.route('packaging.edit',['id' => $arrProduct['id'], 'product_id'=> $arrProduct['product_id']]).'">
<i class="fa fa-refresh"></i> Proses</a></center>';
})
->addColumn('status', function($arrProduct){
if($arrProduct['status_produksi']){
return ucwords($arrProduct['status_produksi']);
}else{
$tag = "<center><a class='btn btn-danger btn-sm'>
<i class='fa fa-refresh'></i>Belum Diproses</a></center>";
return $tag;
}
})
->make(true);
and this is the problem :
one of them (column action) is work properly, but why in the other (column status) the "addColumn" doesn't work?
pls somebody help me.. thank's anyway
Add ->rawColumns(['status', 'action'])
before ->make(true);

Resources