Hi @msaied you can keep the step in session, for example. So after user fill the step - validate the data, store it (in session too, or in db, not sure how it works for you), set next page value in session and render next step
How To Make pagination without Parameter on URL ( Pretty URL pagination )
Hello I have quiz script so the user should answer every question and click on Next To See Next question I made this with pagination But the problem is the user can jump to other questions without answer it just by change
?page=X
The parameter on URL, so I need a way to hide this Parameter I search but I didn't found any answer :)
Hello @silencebringer ,
this is what I was thinking for but the question is how :D for example
public function index()
{
$users = User::Paginate(10);
return view('welcome',compact('users'));
}
and In View
<div class="container">
<ol>
@foreach ($users as $user)
<li>{{ $user->name }}</li>
@endforeach
</ol>
</div>
{{$users->links()}}
@msaied I do not understand your example. You show me list of users.
By the way, do you have just next/prev buttons, or you want to show pages too (just disable not available)?
@silencebringer yeah, I just gave you an example, not real code it big one :D btw if I did next/prev I will get URL with ?page=x number so the user can play with this number and escape next/prev
Pretty URL won't fix that, you probably need to do the quiz using Ajax.
A pretty URL is still a URL.
What's stopping you from using an if statement to make sure a previous answer was checked or whatever prior to the next question.
Just put a little logic in there to make sure an answer took place.
@jlrdw So I have to use Ajax pagination and pass page number with hidden input via it ? right
@jlrdw if yes i found this package to make Json API
https://github.com/spatie/laravel-json-api-paginate
@msaied as example
in routes/web.php
Route::get('quiz', [QuizController::class, 'form'])->name(quiz.form);
Route::post('quiz', [QuizController::class, 'process'])->name(quiz.process);
in QuizController
public function form () {
$step = session('step', '1');
$method = 'step' . $step . 'form';
return $this->{$method};
}
public function step1form () {
/// render step 1
return view (step1form);
}
/// methods for every step
public function step1form () {
...
}
public function process () {
$step = session('step', '1');
$method = 'step' . $step . 'process';
return $this->{$method};
}
public function step1process () {
// do validation
// and every stuff you need
session(['step' => 2]);
return redirect(route('quiz.form'));
}
possible will have some mistakes, but it should give you an idea how to implement this. So, 2 endpoints (to display form and to process form submit), and render appropriate step according to step value in session
@msaied Did you work with vue js to help you with this?
@silencebringer Hello, quizzes are dynamic so if I can't make method for every question
@mohammad_ranjbar49 No I didn't use vue
perhaps if you sign the URL for the previous and next buttons and only allow the user to access the question via signed URL
@snapey good answer, but I also a pretty URL without any parameter or anything just with quiz slug
Then switch to Livewire component
@snapey or vue ?
or ajax / axios / fetch
@snapey yeah I was thinking in ajax with this package: https://github.com/spatie/laravel-json-api-paginate
any other approach would likely not require pagination.
Like I said above you could set up some if statements.
Pseudocode
If the request does not have this answer then do something
// redirect back with errors or whatever
I've seen many other laravel uses have these types of quizzes.
My first answer was a two part, suggesting Ajax or setting up some good if statements.
Have you looked at some packages on GitHub that do this stuff, and get some ideas from their code on how they handle a non answered question.
Create a hidden field on the form that contains the id they are on. Retrieve the field's value, using it to determine the next form, rather than using the parameter. A possible solution....
Please or to participate in this conversation.