Laravel ajax pagination set page - php

In my laravel project i use hashes in the url to keep track on selections of the user, so they are able to go back to a filter result. I dont want the page to refresh so I DONT UPDATE THE URL with Page=someid (except the hash) the response of the server looks like.
if ($request->ajax()) {
//The page I want to select posted with ajax
$paginaId = $request->input('page');
//some query like
$query = Table::Query();
//get 9 items
$rooms= $query->paginate(9);
//Return the response
return Response::json(View::make('front.rooms.render', array('rooms' => $rooms))->render());
}
The $paginaId represents the selected pagination page. Everything works fine except one part. I cant set the current page of the pagination. I viewed the documentation but couldn't find any solutions.

Try the following:
$rooms = $query->paginate(9, ['*'], 'page', $paginaId);
In this case you can set $paginaId manualy

Related

Laravel collection not saving added data

I'm building a Laravel 9x application where i will need to populate some pages with the google places API.
Reading at the ToS of Google Places, i can only store in my db, for at most 30 days, the place_id.
Now, I'm facing an issue; I have a restaurants.index blade page where I'm showing in a table all the records from the restaurants table.
The issue is that i need to show in each table row the name of the restaurant that i don't have in my db table but i need to fetch it from Google Places Api.
So far, in my RestaurantsController I have done the following :
public function index()
{
$getAllRecords = Restaurant::all();
$google_places_api = new PlacesApi(env('GOOGLE_MAPS_API_KEY'));
$restaurants = new Collection();
foreach($getAllRecords as $restaurant){
$response= $google_places_api->placeDetails($restaurant->google_place_id);
$restaurant['name'] = $response['result']['name'];
$restaurants->prepend($restaurant);
}
dd($restaurants);//When dd here, the collection shows correctly the added value.
$restaurants->paginate(8);
$categories = Category::where('type', 'Restaurant')->cursor();
return view('tenant.restaurants.index', compact('categories', 'restaurants'));
}
And it seems to work pretty well if I dd($restaurants) inside the RestaurantsController as you can see from the picture below :
But when i try to #dd($restaurants) from the restaurants.index page, the added name is completely disappeared as you can see from the picture below :
It's now about a whole day that I'm tryng to understand why this behavior happen, is there anybody who has an idea of what is happening?
Thank you

Twilio Paging Information

Im using the Twilio PHP Sdk.
$AccountSid = env('TWILIO_SID');
$AuthToken = env('TWILIO_TOKEN');
$this->twi = new TwilioClient($AccountSid, $AuthToken );
And Im retrieving call logs like so.
$calls = $this->twi->calls->read(["to" => "+".$number->number], 15);
This is fine, and is returning 15 records of the latest calls.
But how do I access paging information such as how many total records are available, and how many pages are available to view. So that I can display some sort of pagination on my page??
I tried just accessing the Calls.json object, via CURL without using the SDK, like this...
$url = "https://api.twilio.com/2010-04-01/Accounts/".$AccountSid."/Calls.json?PageSize=15";
And that gave me a nice JSON object, with some paging info such as next_page_uri and previous_page_uri.
[previous_page_uri] =>/2010-04-01/Accounts/AC##########/Calls.json?PageSize=15&Page=0
[page_size] => 15
[start] => 0
[next_page_uri] => /2010-04-01/Accounts/AC##########/Calls.jsonPageSize=15&Page=1&PageToken=PACA27b63143f18c458f2abd35ef90753e5a
[page] => 0
But still, no totals such as total records, or total pages in the query??? So I cant display a nice bar of pagination at the bottom of my table, to show how many actual pages there are?
This seems dumb.
Is there no way to get this information?? Without actually storing the calls individually in my own database, so I can provide this manually?
Also.....one other question (assuming I cant retrieve that information)
I still want to be able to navigate from page to page, via the SDK instead of CURL requests. So....how do I pass the page number to the Twilio SDK? Ive tried...
$calls = $this->twi->calls->read(["To" => "+".$number->number,"Page"=>22], 15);
But it still just returns the first page. So how do I paginate these records via the SDK??
I am not sure about total pages, but for pagination i have used TWILIO PHP SDK's function,
Here how it will work,
//this will fetch first 10 calls
$calls = $twilio->calls->page([], 10, \Twilio\Values::NONE, 0);
//then you just need to call 2 methods for next and previous page
$nextPageData = $calls->nextPage(); //this will return data of next page
$previousPageData = $calls->previousPage(); // this will return data of previous page
// to check if current page has valid data
if(!$nextPageData->valid()){
//Invalid page
}
This is what i used for pagination on my project.

Laravel get static array of random numbers and use it in pagination

I am developing an application in Laravel 5.5 ... it is kind of a testing system...
What I want to achieve is that when user clicks on RUN TEST, it will generate random set of questions (let's say 5) then I want them to be served to the user one by one ... I did a pagination in AJAX so the questions can be served by pages .. and after that user will click on SUBMIT which will submit the form ..
I am able to get the random list of questions
In the TestController within the method "exec_test" is
// get random id
$q_id = Question::select('id')
->where('test_id','=',$test_id)
//->where('id','=',114)
->orderByRaw("RAND()")
->take(3)
->get();
This will get 3 random ID's for that specific test ...
Following code in the same method is
// get data
$data = array (
'tests' => Test::select('id','code','name',DB::raw('duration_m * 60 as duration_m'),'questions','passing_score')
->where('id','=',$test_id)
->orderBy('code', 'asc')
->paginate($this->page_limit),
'test_questions' => Question::select('questions.id','questions.question_header','questions.question_detail_local_url')
->join('tests','tests.id','=','questions.test_id')
->where('test_id','=',$test_id)
->whereIn('questions.id',$q_id)
//->orderByRaw("RAND()")
//->take(4)
->paginate($this->test_question_page_limit),
'test_answers' => Answer::select('id','answer_text_cleaned')
->whereIn('question_id',$q_id)
->get()
);
// check for ajax requests
if ($request->ajax()) {
return view('/custom/test/run_test_quests', $data)->render();
}
return view('/custom/test/run_test_info',$data);
But I don't know how can I make that first query static and to make it served one by one (per page) to the user .. so he can go trough all questions
Do you have any suggestions please?
while paginating the first page would be 1, it would be specified in the url too.
try this:
if($request->input('page') ==1)
{
//code for first page
}else{
//code for random
}
this is just a suggestion. Sorry if I understood the question wrong

Laravel 5 user defined settings only needs to be called once

For my personal website (online administration) I have an appointments page, with certain settings, which is just for me, so I don't need it to be secure.
For example, I can change my view (to show all appointments or to sort them by label, or status). I can also exclude a status to make sure it's not being shown. Everything works, no problem there.
My issue is this. I have a simple field in my user database called "view". When I go to my appointments page, I check the value of my "view" field and if it is "status" for example, in my controller I set "$view = status", to return my "status" view. This works, with the following simple check:
$getUserView = \Auth::user()->view;
if($getUserView){
$view = $getUserView;
}
In my view itself I have a dropdown to change the view. Now, when I go to my view, it shows the "status view" just fine. But when I want to change the view to "default" or "label" using my dropdown, it should change the view to what I selected. So basically what I want to achieve is, when I go to my appointments page for the first time, it should show the view that I have set in my database, but only that one time. I could set it in a session maybe for that but I am just not sure how to accomplish this. Any pointers would be helpful!
Edit:
Still struggling with this, because I am using GET for everything, also the dropdown. Example, when I change the value in the dropdown, a javascript simply calls the URL again, but with the status that was selected in the dropdown. So, for example, my default URL is the following:
http://example.com/appointments/status/default
Now, I select "completed" in the dropdown, the following URL is called:
http://example.com/appointments/status/completed
In my appointments controller I put the following:
$status = session()->get("status", \Auth::user()->status);
In my routes I have the following:
Route::get('appointments/status/{status}', array('as' => 'appointments', 'uses' => 'Appointments\AppointmentsController#index'));
Maybe I should change "{status}" in the route to "$status" and use the put method to set the "$status"? Not sure what the best method would be.
When using the get method on the Session, the second argument is intended for a default value, which will be returned if the session key is not found.
You could do something like this:
$user = \Auth::user();
$view = session()->get("appointments_view", $user->view);
That will get the view set in the session, and if that is not set, it'll return $user->view. Now, when the users picks another view in the dropdown, just do:
session()->put("appointments_view", $dropDownValue);

Php - Multiple Pages Same Url Different Ids?

I've been working on a project for which i want to create a page leading to different pages but i want to use same URL i have created all the pages but now i am stuck...
the Thing i want is.
i have pages **
index324.php , index34.php , index378.php , index345.php ,
index365.php , index334.php
What i want is to use a url -
index.php?product_id=21
index.php?product_id=23
Something Like Shown Above.
On your index.php, check if $_GET['product_id'] is set. If it is, use it to retrieve the desired ID which in turn you will use for checking against a database/file/whatever.
if (isset($_GET['product_id']))
$product_id = $_GET['product_id'];
Of course, you'll want to do some validation on the fetched ID to be sure it won't harm anything in your system since it can be altered by anyone issuing a GET request to your page.
Suppose that you have product detail pages for each product like: product1.php, product2.php etc ... You can include your desired file in index.php by passing GET parameter. e.g. (index.php?product_id=21) :
if(isset($_GET['product_id'])){
$product_id = $_GET['product_id'];
include('product' .$product_id. '.php');
}
else{
// product_id is not set ...
// do more stuff...
}

Categories