use pagination in laravel api resource in laravel - laravel

I want to use pagination after getting my data from api resource
But the server responds:
Method Illuminate\Database\Eloquent\Collection::pagination does not exist.
public function index(Request $request )
{
$perPage=$request->per_page;
return response()->json(['user'=>UserResource::collection(User::with('roles')->get()->pagination($perPage))],200);
}

You don't have to call paginate on get function
Just
public function index(Request $request ) {
$perPage=$request->per_page;
return response()->json(['user'=>UserResource::collection(User::with('roles')->pagination($perPage))],200);
}
Even Better
public function index(Request $request ) {
$perPage=$request->per_page;
return new UserCollection(User::with('roles')->paginate($perPage));
}

return response()->json(['user'=>UserResource::collection(User::paginate($perPage))],200);
this is worked correctly !
100% ok

Related

Can I use return to a public function in controller in Laravel 8

I got an error which is undefined function.
I tried to use a public function in return:
public function CreateForm() //This the function that I want to use back
{
$names2 = DB::table('pendaftaran')
->where('isActive',0)
->orderBy('id','desc')
->get();
return view('contact')->with($variables);
}
So this a function I want to return to a function
public function AddUserSubmit(Request $request)
{
$this->validate($request,
[
'Nama'=>'required',
'NoKP'=>'required',
]);
Pendaftaran::create($request->all());
return CreateForm(); //Can I return to a public function ?
}
Yes this should work but you need to add context and use the $this keyword otherwise return CreateForm(); will be interpreted as trying to invoke a global function.
Try replacing:
return CreateForm();
With
return $this->CreateForm();
See this other question for more information: https://stackoverflow.com/a/17861505/4517964

I can't use onlyTrashed method on datapanel in laravel

I have a problem with Laravel datatable SoftDelete method in Laravel. I use onlyTrashed() function to get the data but all data is coming.I am using the latest version yajrabox.
Frist try:
public function trash(Request $request){
$datalists = DataTables::of(Datapanel::onlyTrashed())->onlyTrashed()->make(true);
return view('back.cancelpanel');
}
Second try:
public function trash(Request $request){
if ($request->ajax()) {
return Datatables::of(Datapanel::onlyTrashed())
->onlyTrashed()
->make(true);
}
return view('back.cancelpanel');
}
Third try:(In this try i reached the data deleted with json data but I didn't know how to direct it to datatable.)
public function trash(Request $request){
$datalists = DataTables::of(Datapanel::onlyTrashed())->make(true);
return response()->json($datalists);
}

I want to download a excel file through Checkbox Unique ID in laravel, I am using Maatwebsite\Excel Here

My Controller File
public function enquiryExport($id, Request $request)
{
$id[] = $request->explode(",",$id);
return Excel::download(new EnquiryExport($id), 'enquiry.xlsx');
}
and My Export File
protected $id;
function __construct($id) {
$this->id = $id;
}
public function collection()
{
return Enquiry::whereIn('id',explode(",",$this->id))->get();
/* return Enquiry::all(); */
}
Route is like
Route::get('enquiryExport', 'enquiryController#enquiryExport');
Still I am getting this error
"message": "Too few arguments to function App\\Http\\Controllers\\enquiryController::enquiryExport(), 1 passed and exactly 2 expected",
I am checkbox id through AJAX here.
The problems is your Route method.
Get method: the query string (name/value pairs) is sent in the URL of a GET request
Post method: the data sent to the server with POST is stored in the request body of the HTTP request
If you use Get method: try this (I have just read it, not tried)
Route::get('enquiryExport/{id}', 'enquiryController#enquiryExport')->name('enquiryExport');
Submit
If you use Post method: try this (I am used to use this)
Route::post('enquiryExport', 'enquiryController#enquiryExport');
public function enquiryExport(Request $request)
{
return Excel::download(new EnquiryExport($request->input('id')), 'enquiry.xlsx');
}
You can read more here: https://www.w3schools.com/tags/ref_httpmethods.asp
Try this
In controller:
public function enquiryExport(Request $request, $id)
{
return Excel::download(new EnquiryExport($request->id), ''.date('Y-m-d'). '.xlsx', \Maatwebsite\Excel\Excel::XLSX);
}
In Export File:
protected $id;
function __construct($id) {
$this->id = $id;
}
public function collection()
{
return Enquiry::where('id', $this->id)->get();
}
public function map($enquiry): array
{
return [
// $enquiry->WRITE YOUR RECORDS,
// ...
];
}
public function headings(): array
{
return [
//NAME HEADINGS(TITLE) OF YOUR RECORDS IN SIDE SINGLE QUOTATION,
// ...
];
}
In Route:
Route::get('enquiryExport/{id}', 'enquiryController#enquiryExport');

Laravel - Paramaters should be how when we use match route?

I want to use match method but I can't take $slug and $request in controller..
Here is my route
Route::match(['get','post] , 'category/{slug}' , ['as'=>'category.show' , 'uses'=>'CategoryController#categoryProducts'])
in Controller, our function will be how ? How we use that post and get data in the same function ? I tried like below, but dosen't work
public function categoryProducts($slug, $request) {
//codes
}
Please use Request::method();
public function categoryProducts(Request $request, $slug) {
//codes
}
to get request type GET/POST
$method = Request::method();
or
if (Request::isMethod('post'))
{
//
}
Since you're trying to inject Request object and not passing it, do this instead:
public function categoryProducts(Request $request, $slug)

Lravel 5.2 session::forget() and session::flush() not working

i have just make a sub authentication Middleware in my Laravel 5.2 application which use Laravel session to store data.
I can put my data to Laravel session
But when i want to delete that variable form session it working for only that request when page redirect or someone reload the page that variable still exists.
In My controller File
class SubmissionController extends Controller
{
public function login(Request $request){
if($request->session()->has('submission')) return redirect('/submission-directory');
return view('submission.login');
}
public function dologin(Request $request){
if(!$request->get('password') == "reader") return redirect('/submission-directory/login')->withErrors('errors.wrong-password');
Session::put('submission','yes');
$redirect = $request->session()->pull('submission_redirect','/submission-directory');
return redirect($redirect);
}
public function index(Request $request){
dump($request->session()->all());
$request->session()->forget('submission');
dump($request->session()->all());
die('coming here');
}
}
but when I reload the page You can session is still exists..
Notice :: I have put all the routs in web Middleware group
Route.php
Route::group(['middleware' => 'web'], function () {
Route::group(['prefix'=>'/submission-directory'],function(){
Route::get('/login','submissionController#login');
Route::post('/login',['as'=>'submission.login','uses'=>'SubmissionController#doLogin']);
Route::group(['middleware'=>'submission'],function(){
Route::get('/','SubmissionController#index');
});
});
Try this
IN Controller :
use Session;
public function index(Request $request)
{
dump(Session::all());
Session::forget('submission');
print_r((Session::all());
die;
}

Resources