how to append `request()->input()` to GET form action - php

I have a filtration page to get the posts according to user choices, I want to give the user the ability to choose multiple ways to filter, such as the price, post date, category, ...etc.
therefore I should append the user's previous choices.
for example ex: example.com?category=1 and now the user wants to filter with price, it should be. ex: example.com?category=1&price=200
it works for me while trying <a></a> tag,
ex price 200.
so I need to do the same with form GET, here's how I tries to solve it:
{!! Form::open(
[
'route' => [Request::route()->getName(), request()->input()],
'method' => 'GET'
]) !!}
#foreach ($specialisms as $specialism)
<div class="custom-control custom-checkbox">
{!! Form::checkbox('specialism', $specialism->id, request('specialism') == $specialism->id, ['class' => 'custom-control-input', 'id' => Str::snake($specialism->title['en']), 'onchange' => '$(this).parent().parent().submit()']) !!}
<label class="custom-control-label" for="{{ Str::snake($specialism->title['en']) }}">{{ $specialism->lang_title }}</label>
</div>
#endforeach
{!! Form::close() !!}
but it doesn't work, it always removes all parameters and append-only the parameter for the form input

you should modify:
checkbox name :specialism[] and condition: in_array($specialism->id, request('specialism'))
to be :
{!! Form::open([
'route' => [Request::route()->getName(), request()->input()],
'method' => 'GET',
]) !!}
#foreach ($specialisms as $specialism)
<div class="custom-control custom-checkbox">
{!! Form::checkbox('specialism[]', $specialism->id, in_array($specialism->id, request('specialism')), ['class'
=> 'custom-control-input', 'id' => Str::snake($specialism->title['en']), 'onchange' =>
'$(this).parent().parent().submit()']) !!}
<label class="custom-control-label"
for="{{ Str::snake($specialism->title['en']) }}">{{ $specialism->lang_title }}</label>
</div>
#endforeach
{!! Form::close() !!}

Related

Laravel update how to update data in index

The problem is i want to skip that edit page. it works as fine. but i wanna edit my data in index view.
i tried this but i took this error
{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
<input class="form-control" value="#foreach ($choices as $choice){{ $choice->question_number }}#endforeach" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Trying to get property of non-object (View: C:\wamp64\www\zainsurgalt\resources\views\choices\index.blade.php)
index view
<td>Edit</td>
edit view
{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
<input class="form-control" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
controller update
public function update(Request $request,Choice $choice){
Choice::where('id', $choice->id)->update([
'question_number' => $request->input('number')
]);
return redirect()->route('choices.index');
}
All time when you get object property you must be check object exist or not
#if (!empty($duplicate->topic))
<td>Edit</td>
#endIf
also
#if (!empty($choice))
{!! Form::model($choice, ['method' => 'PATCH','route' => ['choices.update', $choice->id]]) !!}
<input class="form-control" type="number" name="number"></input>
{!! Form::submit('Update Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
#endIf
and finally you use
#foreach ($choices as $choice){{ $choice->question_number }}#endforeach
change $choice to another name for example $_choice , for not confusing above used $choice

Pagination after search only works on the first page

The first page will be sorted correctly. However, the second page (and further) will go back to being as if no fields in the search were filled at all.
I am collecting the search fields in the form below:
{{ Form::open(['route' => 'admin.users.search', 'method' => 'get', 'class' => 'navbar-form navbar-left form-inline', 'role' => 'search']) }}
<div class="form-group">
{{ Form::text('user_id', request('user_id'), ['class' => 'form-control', 'size' => '8', 'placeholder' => 'ID']) }}
</div>
<div class="form-group">
{{ Form::email('email', request('email'), ['class' => 'form-control', 'size' => '20', 'placeholder' => 'Email']) }}
</div>
<div class="form-group">
{{ Form::text('first_name', request('first_name'), ['class' => 'form-control', 'size' => '20', 'placeholder' => 'First Name']) }}
</div>
<div class="form-group">
{{ Form::text('family_name', request('family_name'), ['class' => 'form-control', 'size' => '20', 'placeholder' => 'Family Name']) }}
</div>
<div class="form-group">
<div class="selectize-lg">
{{ Form::select('institution_id', $institutions, request('institution_id'), ['class' => 'form-control', 'size' => '200', 'data-selectize']) }}
</div>
</div>
<div class="form-group">
<div class="selectize-lg">
{{ Form::select('exam_id', $exams, request('exam_id'), ['class' => 'form-control', 'data-selectize']) }}
</div>
</div>
<div class="form-group ">
{{ Form::submit('Search', ['class' => 'btn btn-default']) }}
</div>
Clear
{{ Form::close() }}
Once the form has been submitted it will hit a GET route
Route::get('members/search', 'UsersController#search')->name('admin.users.search');
Then the users controller:
$users = User::with('exam', 'institution');
if ($request->has('user_id')) {
$users->whereId($request->user_id);
}
if ($request->has('email')) {
$users->whereEmail($request->email);
}
if ($request->has('first_name')) {
$users->where('first_name', 'LIKE', "%{$request->first_name}%");
}
if ($request->has('family_name')) {
$users->where('family_name', 'LIKE', "%{$request->family_name}%");
}
if ($request->has('institution_id')) {
$users->whereInstitutionId($request->institution_id);
}
if ($request->has('exam_id')) {
$users->whereExamId($request->exam_id);
}
$users = $users->latest()->paginate(48);
$usersTotal = $users->total();
$exams = ['' => 'Exam...'] + Exam::orderBy('title')
->pluck('title', 'id')
->all();
$institutions = ['' => 'University...'] + Institution::orderBy('name')
->pluck('name', 'id')
->all();
return view('admin.users.index', compact('users', 'usersTotal', 'exams', 'institutions'));
Then, in the view I am adding the pagination links like this:
{{ $users->appends(array_filter(request()->except('page')))->render() }}
However, the search results only work on the first page. for example, the route on the first page will look like this:
search?user_id=&email=hello%40world&first_name=John&family_name=Smith&institution_id=1&exam_id=1
But the second page will look like this:
search?page=2
I am finding this pretty puzzling and not too sure what is causing the search to fail on the second page.
From the docs:
Appending To Pagination Links
You may append to the query string of pagination links using the
appends method. For example, to append sort=votes to each pagination
link, you should make the following call to appends:
{{ $users->appends(['sort' => 'votes'])->links() }} If you wish to
append a "hash fragment" to the paginator's URLs, you may use the
fragment method. For example, to append #foo to the end of each
pagination link, make the following call to the fragment method:
{{ $users->fragment('foo')->links() }}
You are almost on the right track with this:
{{ $users->appends(array_filter(request()->except('page')))->render() }}
Wat this peace of code does is carry over the page-request when you switch pages, which is necessary since that is done for you. So you will need to specify the request data that you would like to keep. So for example:
{{ $users->appends(Request::except('page'))->links() }}
will add all request data to the next page, except the pagination links because that is added by default.
Note: i notice that you are sending id's with GET requests , please be carefull doing that. Users with bad intentions can see you database structure and possible take use of it.

Getting a data from a table to insert to another database table

I have this table that contains data from the database table 'patient'.
<td>
<a href="{{ URL::to('/addvisit/'.$patient->pID) }}">
<img src="images/visit.png" style="width:30px; height:30px;">
</a>
</td>
when the circle icon is clicked it will redirect to a page with a form that will be inserted in a database table 'visit'.
below is the form:
{!! Form::model($patient, ['url'=>'visit/' . $patient->pID, 'method' => 'PATCH']) !!}
<div class="form-group">
{!! Form::label('fname', 'Full Name',['class' => 'control-label'])!!}
{!! Form::text('pName',null,['class' => 'form-control']) !!}
{!! Form::label('address', 'Address',['class' => 'control-label'])!!}
{!! Form::text('pAddress',null, ['class' => 'form-control']) !!}
{!! Form::submit('SUBMIT',['class' => 'form-control btn btn-success']) !!}
</div>
{!! Form::close() !!}
how can I add the patient ID from the 'patient' table to be set as a foreign key in the 'visit' table.
Pass it again to the view with the form through controller and store it on a hidden input

How do I populate Laravel/Blade checkboxes from json in a db

Laravel is great that it auto populates the fields for you on an edit form. However i seem to be having bother getting this to work for checkboxes.
I have a list of disciplines that get stored as a json array in the database like so ["FULL_CONTACT","K1"]
How do I get these to display as checked in the form?
{!! Form::model($official, array('method' => 'put', 'route' => ['officials.update', $official->id], 'class' => 'form-horizontal')) !!}
<div class="form-group">
<label for="" class="col-lg-3 control-label">First Name:</label>
<div class="col-lg-9 controls">
{!! Form::text('first_name', null, array('class' => 'form-control', 'max-length' => '50', 'required')) !!}
</div>
</div>
<label class="checkbox">
{!! Form::checkbox('disciplines[]', null) !!} Full Contact
</label>
<label class="checkbox">
{!! Form::checkbox('disciplines[]', null) !!} Low Kick
</label>
<label class="checkbox">
{!! Form::checkbox('disciplines[]', null) !!} K1
</label>
{!! Form::close() !!}
{{ Form::checkbox('disciplines[]', 'Low Kick', true) }}
this will generate following html
<input checked="checked" name="disciplines[]" type="checkbox" value="Low Kick">
Form::checkbox's arguments are
Form::checkbox($name, $value, $checked, $options)
You can use #foreach loop to display all the checkboxs, for example:
#foreach($disciplines as $discipline)
{!! Form::checkbox('disciplines[]', $discipline, null) !!} label
#endforeach`

Laravel 4 - 2 forms with the same input names

I have created 2 separate forms. One for signin and one for signup. They work fine on separate pages but if they are on the same page they print each others error messages. I'm guess its because they both contain the same input names.
They have separate controller methods though. Here is the example setup.
Signup form
{{ Form::open(['route' => 'signup']) }}
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', null, ['class' => 'form-control']) }}
{{ $errors->first('email', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::label('password','Paswword') }}
{{ Form::password('password', ['class' => 'form-control']) }}
<p class="help-block">Password needs to be between 6 - 8 characters</p>
{{ $errors->first('password', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::submit('Sign up', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
Login form
{{ Form::open(['route' => 'login']) }}
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::text('email', null, ['class' => 'form-control']) }}
{{ $errors->first('email', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::label('password','Paswword') }}
{{ Form::password('password', ['class' => 'form-control']) }}
{{ $errors->first('password', '<p class="error">:message</p>')}}
</div>
<div class="form-group">
{{ Form::submit('Login', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
routes.php
Route::get('/signup', [
'as' => 'signup',
'uses' => 'UsersController#getSignup'
]);
Route::post('/signup', [
'as' => 'signup',
'uses' => 'UsersController#postSignup'
]);
Just wondering if anyone else has come across this issue and how to solve it.
Thanks
http://laravel.com/docs/4.2/validation#error-messages-and-views
Named Error Bags
If you have multiple forms on a single page, you may wish to name the MessageBag of errors. This will allow you to retrieve the error messages for a specific form. Simply pass a name as the second argument to withErrors:
return Redirect::to('register')->withErrors($validator, 'login');
You may then access the named MessageBag instance from the $errors variable:
<?php echo $errors->login->first('email'); ?>

Categories