if ($request->ajax()) {
$data = User::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('FullName', function($row){
$name = $row->fname.' '.$row->lname;
return $name;
})
->addColumn('action', function($row){
//#can('user-show')
$btn = '<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
<div class="btn-group" role="group">
<a href="'.route("users.showrole",$row->id).'" data-toggle="tooltip" title="Show" class="btn btn-default btn-flat btn-sm">
<span class="icon-size-fullscreen"></span>
Show
</a>';
//#endcan
//#can('user-edit')
$btn = $btn.'<a href="'.route("users.editrole",$row->id).'" data-toggle="tooltip" id="'.$row->id.'" title="Edit" data-id="'.$row->id.'" class="edit btn btn-primary btn-flat btn-sm CategoryEdit" onclick="CategoryEdit()">
<span class="icon-pencil"></span>
</a>';
//#endcan
$btn = $btn.'<a href="javascript:void(0)" data-toggle="tooltip" id="'.$row->id.'" title="Edit" data-id="'.$row->id.'" onclick="DeleteRole('.$row->id.')" class="btn btn-flat btn-danger btn-sm">
<span class="icon-trash"></span>
</a>
</div>
</div>';
return $btn;
})
->rawColumns(['FullName'])
->rawColumns(['action'])
->escapeColumns([])
->make(true);
}
Hello, I want to use #can('user-create') in Controller datatable Yajra so that a user does not see the button in he has no access.
I can do it in blade but not in controller.
I want to check if the role has the permission to perform tasks in that way.
Otherwise my code is running fine only this is making it tiresome.
You can do it in 2 ways :
checking permission in the controller
Eg. :
if ($request->ajax()) {
$data = User::latest()->get();
// get logged user
$user = auth()->user();
return Datatables::of($data)
->addColumn('action', function($row) use ($user) {
$btn = '';
if ($user->can('user-show') {
$btn = '<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups"><div class="btn-group" role="group">
<span class="icon-size-fullscreen"></span> Show';
}
if ($user->can('user-edit') {
$btn = $btn.'<span class="icon-pencil"></span>';
}
$btn = $btn.'<span class="icon-trash"></span> </div></div>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
or Another Way
if ($request->ajax()) {
$data = User::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('FullName', function($row){
$name = $row->fname.' '.$row->lname;
return $name;
})
->addColumn('actions', 'path.actions')
->rawColumns(['FullName'])
->escapeColumns([])
->make(true);
}
and your blade file be like
#can('user-edit')
Your code
#endcan
If you use spatie permission plugin, you can use $user->can('permission') in your controllers.
Here :
if ($request->ajax()) {
$data = User::latest()->get();
// get logged user
$user = auth()->user();
return Datatables::of($data)
->addIndexColumn()
->addColumn('FullName', function($row){
$name = $row->fname.' '.$row->lname;
return $name;
})
->addColumn('action', function($row) use ($user) {
$btn = '';
if ($user->can('user-show') {
$btn = '<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups"><div class="btn-group" role="group">
<span class="icon-size-fullscreen"></span> Show';
}
if ($user->can('user-edit') {
$btn = $btn.'<span class="icon-pencil"></span>';
}
$btn = $btn.'<span class="icon-trash"></span> </div></div>';
return $btn;
})
->rawColumns(['FullName'])
->rawColumns(['action'])
->escapeColumns([])
->make(true);
}
Related
I'm having a problem with Livewire passing an array to a route so I can print it to pdf. I've got:
public $selectedProducts = [];
public $selectedAlm = [];
public $nombreComponente, $data, $categoriaId;
public function mount()
{
$this->categoriaId = "0";
$this->nombreComponente = 'Reportes';
$this->data = [];
$this->artcero = 'si';
}
where $selectedProducts and $selectedAlm are arrays and my intention is to pass them to another controller to be able to print them to pdf, but I can't seem to find a way
here is my controller:
public function reportearticuloPDF($categoriaId, $artcero, $selectedAlm, $selectedProducts)
{
if ($selectedProducts > 0) {
$data = $selectedProducts;
} else {
if ($categoriaId == null) {
$data = Articulo::join('categorias as c', 'c.id', 'articulos.categoria_id')
->select('articulos.*', 'c.nombre as catnom')
->whereHas('detallearticulos', function($q) {
$q->whereIn('almacen_id', $selectedAlm);
})
->when($artcero == 'no', function($query){
return $query->where('stock', '>', 0);
})
->OrderBy('id', 'ASC')
->get();
} elseif ($categoriaId !== null) {
$data = Articulo::join('categorias as c', 'c.id', 'articulos.categoria_id')
->select('articulos.*', 'c.nombre as catnom')
->whereHas('detallearticulos', function($q) {
$q->whereIn('almacen_id', $selectedAlm);
})->with('detallearticulos')
->when($artcero == 'no', function($query){
return $query->where('stock', '>', 0);
})
->where('categoria_id', $categoriaId)
->OrderBy('id', 'ASC')
->get();
}
}
$pdf = PDF::loadView('admin.report.articulopdf', compact('data'));
return $pdf->stream('Reporte_de_articulo.pdf');
}
my routes in web.php:
Route::get('report/articulopdf/{categoriaId}/{artcero}/{selectedAlm}/{selectedProducts}', [ReportController::class, 'reportearticuloPDF'])->name('reportearticulo.pdf');
Route::get('report/articulopdf/{categoriaId}/{artcero}/{selectedAlm}', [ReportController::class, 'reportearticuloPDF'])->name('reportearticulo.pdf');
my button:
<a href="{{ url('report/articulopdf' . '/' . $categoriaId . '/' . $artcero . '/' . $selectedAlm . '/' . $selectedProducts) }}"
class="btn btn-danger btn-sm btn-block {{ count($data) < 1 ? 'disabled' : '' }}" target="_blank">
Exportar reporte a PDF <i class="fas fa-file-pdf"></i></a>
that's the one that gives me the Array to string conversion error. I also tried it this way:
<a href="{{ route('reportearticuloPDF', $categoriaId, $artcero, $selectedAlm, $selectedProducts) }}"
class="btn btn-danger btn-sm btn-block {{ count($data) < 1 ? 'disabled' : '' }}" target="_blank">
Exportar reporte a PDF <i class="fas fa-file-pdf"></i></a>
but it gives me the error Too few arguments to function App\Http\Controllers\ReportController::reportearticuloPDF(), 0 passed in C:\xampp\htdocs\sistemainventarioGDS\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 4 expected
In your routes you create two routes with the same method and same function reportearticuloPDF in the controller with different params !!
foreach not working, only displays the first data and does not display the next data, what is the solution? is there something wrong with the code?
this my database
and this my view
public function index(Request $request)
{
if ($request->ajax()) {
$data = Rek_medik::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$button = '</i> Edit';
$button .= ' ';
$button .= '<i class="far fa-paper-plane"></i> Detail';
$button .= ' ';
$button .= '<button type="button" name="delete" id="'.$row->id.'" class="delete btn btn-danger btn-xs"><i class="far fa-trash-alt"></i> Delete</button>';
return $button;
})
->addColumn('resep', function($row) {
$resep2 = json_decode($row->resep , true);
foreach ($resep2 as $data) {
return '<span class="badge badge-success">'.$data.'</span>';
}
})
->rawColumns(['action','resep'])
->make(true);
}
$register = Register::all();
$pasien = Pasien::all();
$pegawai = Pegawai::all();
$obat = Obat::all();
$koderekmed = IdGenerator::generate(['table' => 'rek_mediks','field'=>'kode_rekmed', 'length' => 9, 'prefix' =>'RKMD-']);
return view('admin/rek_medik/index', compact('koderekmed','register','pasien','pegawai','obat'));
}
If you use return, then it will die after the first loop. You need to change :
$resep2 = json_decode($row->resep , true);
$query = [];
foreach ($resep2 as $data) {
$query[] = '<span class="badge badge-success">'.$data.'</span>';
}
$query = implode(', ', $query);
return $query;
})
I have some data shown in my datatable view, I want to add button to each data to open detail page which can show more detail information
public function Task(Request $request)
{
if ($request->ajax()) {
$data = DB::table('posts')
->where('jabatan', Auth::user()->jabatan)
->select('user_id', 'name', DB::raw('count(user_id) as total'))
->selectRaw('SUM(status = "Selesai") as selesai')
->selectRaw('count(user_id) - SUM(status = "Selesai") as belum')
->groupBy('name')
->groupBy('user_id')->get();
return Datatables::of($data)
->addColumn('action', function ($row) {
$btn = ' <span class="fas fa-eye"></span>';
return $btn;
})
->rawColumns(['action'])
->addIndexColumn()
->make(true);
}
return view('task.Task');
}
the button can appear in my datatable, but it will open %7B%7Broute('detail.index',$row->user_id)%7D%7D ,
If in a html table I can use <a class="btn btn-info btn-sm" href="{{ route('detail.index',$post->id) }}">Show</a>
how to make the button to open /detail in url? thanks in advance
as your in already in php so don't use {{ }} blade syntax use
$btn = '<span class="fas fa-eye"></span>';
I have data from the API, I can bring up the data in view with datatables, but I'm confused to edit from ID based,
this my controller
$response = Curl::to('127.0.0.1/post/show')->get();
$data = json_decode($response, true);
$outputData = $data["data"];
return Datatables::of($outputData)
->addIndexColumn()
->addColumn('action', function ($row) {
$btn = '<i class="far fa-edit"></i>';
$btn = $btn . ' <i class="far fa-trash-alt"></i>';
return $btn;
})
->rawColumns(['action'])
->make(true);
f i use $ row-> id it will show an error trying to get property of 'id' of non-object
this my script datatable
$('#tAdmin').DataTable({
processing: true,
serverSide: true,
ordering: false,
ajax: "{{route('postshow')}}",
columns : [
{"data" : "id"},
{"data" : "author_id"},
{"data" : "articel"},
{"data" : "action"}
]
});
this is when dd($outputData)
data": [
{
"id": "2",
"author": "james",
"artikel" : "some artikel",
"action" : "a href=\"javascript:void(0)\" data-toggle=\"tooltip\" data-id=\"\" data-original-title=\"Edit\" class=\"edit btn btn-primary btn-sm btn-edit\"><i class=\"far fa-edit\"></i></a> <i class=\"far fa-trash-alt\"></i>"
"vendor_id": "1",
try to Use this to overcome your problem
$row['id']
Just change in your code
data-id="'.$row['id'].'
$btn = '<i class="far fa-edit"></i>';
$btn = $btn . ' <i class="far fa-trash-alt"></i>';
Okay I have a small problem.
When user clicks on a link it goes to website/create/business which is fine however if nothing is done but the link is clicked again, it goes to website/create/business/create/business for whatever reason.
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
Profile
Add Business
<a href="{{ url('/logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" <a href="{{ url('home') }}" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
Routes:
Route::get('profile/{user_id}', 'ProfileController#checkid');
Route::post('update', 'ProfileController#updateProfile');
Route::get('create/business', 'BusinessController#addBusiness');
Route::post('create', 'BusinessController#createBusiness');
ProfileController:
public function checkid($user_id) {
if (Auth::check())
{
$user_id = Auth::id();
return view('profile', [
'id' => $user_id
]);
}
}
function updateProfile(Request $request) {
$user = $request->user();
$twitter = $request->input('twitter');
$facebook = $request->input('facebook');
$instagram = $request->input('instagram');
$telephone = $request->input('telephone');
$user->twitter_personal = $twitter;
$user->facebook_personal = $facebook;
$user->instagram_personal = $instagram;
$user->telephone = $telephone;
$result = $user->save();
if($result) {
$message = 'success';
}else{
$message = 'error';
}
return redirect()->back()->withInput()->with('message', $message);
}
BusinessController:
function addBusiness() {
return view('addBusiness');
}
function createBusiness(Request $request) {
$name = $request->input('name');
$type = $request->input('type');
$email = $request->input('email');
$user_id = Auth::id();
$business = new Business();
$business->name = $name;
$business->type = $type;
$business->email = $email;
$business->user_id = $user_id;
$business->save();
$address1 = $request->input('address1');
$address2 = $request->input('address2');
$town = $request->input('town');
$city = $request->input('city');
$postcode = $request->input('postcode');
$telephone = $request->input('telephone');
$address = new Address();
$address->firstline_address = $address1;
$address->secondline_address = $address2;
$address->town = $town;
$address->city = $city;
$address->postcode = $postcode;
$address->telephone = $telephone;
$address->save();
$result = $business->save();
$result2 = $address->save();
$business_id = $business->id;
$address_id = $address->id;
DB::table('business_address')->insert(array('business_id' => $business_id, 'address_id' => $address_id));
DB::table('user_business')->insert(array('user_id' => $user_id, 'business_id' => $business_id));
if($result && $result2) {
$message = 'success';
}else{
$message = 'error';
}
return redirect()->back()->withInput()->with('message', $message);
}
<a href="create/business/"> must be <a href="/create/business/"> to solve this, because your current link is relative, not absolute, so when you click it again, the same reference is added at the end of your current URL.
Anyway, you should generate links in the Laravel way to avoid other issues in the future:
Route::get('create/business', 'BusinessController#addBusiness');
<a href="{{ url('create/business') }}">
or
Route::get('create/business', 'BusinessController#addBusiness')->name('createBusiness');
<a href="{{ route('createBusiness') }}">
Personally I prefer the second one, so if I change the route URL the links will still work, but it requires to add name('yourRoute') on your route definition.
Try to use the route() function. For this example: Add Business. When your route changes it is automatically changed throughout the application.
You can name your routes like this:
Route::get('create/bussiness', [
'as' => 'my_route_name',
'uses' => 'BusinessController#createBusiness'
]);