Pic 1
Pic2
The situation looks like this.
When sorting, I choose options "Like" and click submit. Everything is sorted nicely, but after refreshing the page, the value in the field is "Lastest".
Is there any possibility that the value I chose would remain until the next select.
You can use a session to keep the last selected value as the default value.
Here you can learn about the sessions.
I fixed it.
I add this to select:
<option href="/?sortMypost=1" value="1" {{ $option == '1' ? "selected" : "" }} >Latest</option>
And this to controller:
$option = request()->input('sortMypost');
And this to the end of the controller:
return view('posts.mypost', compact('posts', 'option'));
Related
I have a register form where users can sign up and I recently added two radio buttons to define users type.
Example:
(*) I want to buy (*) I want to sell
Here's how I was thinking to do this: I added a boolean field in the users table and I was thinking to check if I want to buy is checked then I store 1 in the database if the other one is checked then I store 0.
The thing is, I'm not sure how to check in the controller if the radio button is selected or not.. I tried to look in the documentation and on google but all I found were some ideas using the Form facade which as I know is no longer used in 5.4...
HTML
{{ Form::radio('result', 'buy' , true) }}
{{ Form::radio('result', 'sell' , false) }}
Without Form Facade
<input type="radio" name="result" value="buy" checked>
<input type="radio" name="result" value="sell">
Controller
$fields = Input::get('result');
if($fields == 'buy'){
// logic
}
else{
// logic
}
Remeber: Checkbox and radio button sends values on server end, if and only if they are marked as checked, if not checked that, then no values will be sent out to controller end. so you can do this check in your controller
eg:
public function myMethod(Request $request){
//2nd parameter means, if radio is not selected then use default value
$radio = $request->get('radion_button', 0);
}
I'm trying to use blade to display a dropdown list from table data. The problem I have, is that I want to display the results of two fields in the table concatenated, not just one.
So I want something to render something like;
<select id="agreement_type" name="agreement_type">
<option value="1">Agreement Field 1 - Agreement Field 2</option>
<option value="2">Agreement Field 1 - Agreement Field 2</option>
<option value="4">Agreement Field 1 - Agreement Field 2</option>
</select>
My controller currently looks like this;
$agreements = Agreement::lists('agreement_type','id');
return View::make('client_agreements.create')
->with('agreements', $agreements);
My blade view currently looks like this;
<div class="form-group">
{{ Form::label('agreement_type', 'Agreement type') }}
{{ Form::select('agreement_type', $agreements) }}
</div>
I have tried to amend the view and controller in various ways to get the desired output. But can't get it to work.
I want to display agreement_type and level and have the id set as the value so I tried;
$agreements = Agreement::lists('agreement_type'.'level','id');
But this just displays level as the value and completely ignores id.
This is the simplest method. Just use a foreach loop to build the options array:
$agreements = Agreement::all();
$agreementOptions = array();
foreach($agreements as $agreement){
$agreementOptions[$agreement->id] = $agreement->agreement_type.' '.$agreement->level;
}
return View::make('client_agreements.create')
->with('agreements', $agreementOptions);
However you can also define an attribute accessor in your model. You can use that to create new properties that can be accessed normal, but you can run logic to generate them (like combining to attributes)
public function getSelectOptionAttribute(){
return $this->attributes['agreement_type'].' '.$this->attributes['level'];
}
And then you can use lists:
$agreements = Agreement::all()->lists('select_option', 'id');
(Laravel converts SelectOption (studly case) from the method name to select_option (snake case) for the attribute, so don't get confused by that)
Within my update profile form, I have a select list for title. Admittedly, the items should be derived from the database however for the moment, I would like this particular select list to do the following..
Show the selected item on initial page load. So, if the $user['title'] is a 'Mrs', this is displayed.
Should laravel validation bring up errors on other form fields, and the user has changed the title to 'Mr', the selected state should be on the item 'Mr'.
I have the following so far, but it isn't working correctly.
<select name="title" id="title">
<option value="Mr" #if(($user['title']) == 'Mr') selected #else #if((Input::old('title')) == 'Mr') selected #endif #endif>Mr</option>
<option value="Mrs" #if(($user['title']) == 'Mrs') selected #else #if((Input::old('title')) == 'Mrs') selected #endif #endif>Mrs</option>
....
</select>
You should pass the data for the select from your Controller and also uset the Form component to build the select in the View, for example:
In Controller method:
$users = User::lists('title', 'id');
return View::make('user')->with('users', $user);
In the View:
// By default, item with id 1 (the first item) will be selected
echo Form::select('title', $users, Input::old('title', 1), ['id'=>title]);
If you are using Blade then:
// By default, item with id 1 (the first item) will be selected
{{ Form::select('title', $users, Input::old('title', 1), ['id'=>title]) }}
Read the documentation properly. You are using Laravel with old school PHP coding.
Here is my Select Box, Here All Company will be loaded,
But i want to display the Particular company as default selected which i have it in session.
Here is my Code
$sessioncompany = 'ABCcompany'
$comp[''] = 'Company';
#foreach($company_list as $row)
<?php
$comp[$row->CompanyID] = $row->CompanyName;
?>
#endforeach
{{ Form::select('CompanyID', $comp, '', array('id' => 'seCompanyID')); }}
What i tried is
{{ Form::select('CompanyID', $comp, '', array('id' => 'seCompanyID'), array(id=>$sessioncompany)); }}
But i ended with failure
What is the mistake i am doing and How can i fix that ?
Note : I want it in the native laravel method
$selectedId = ... // get the id from the session
{{ Form::select('CompanyID', $comp, array($selectedId), array('id' => 'seCompanyID')) }}
The Form::select() method has the following parameters:
$name - the nameHTML attribute
(array) $list - the list of options, key-value pairs for actual value => displayed value
$selected - the selected item's value
(array) $options - any extra HTML attributes, as per other Form/HTML helper methods
So, the third parameter $selected is the one you want to pass your session value to. Copying your example and replacing:
{{ Form::select('CompanyID', $comp, Session::get('my_session_var'), array('id' => 'seCompanyID')); }}
Where 'my_session_var' is the name of the session variable you store the value of the item that should be selected by default.
The great thing about using Laravel's Form helpers is that you get the 'old' input for free. For example, if you set a default here and the user changes it, submits the form and there's a validation error. When the form is redisplayed (assuming you redirected back with return Redirect::back()->withInput();) the value the user selected, rather than your explicit default will be set.
(Thanks to #JarekTkaczyk for putting me right on the old input thing.)
You are passing wrong parameters. max parameters should be 4. Try this:
{{ Form::select('CompanyID', array('default' => $sessioncompany)+$comp, 'default') }}
Just beware, if you're on a page with the form with the select box, make sure that with each page refresh while testing you do a full page refresh without using cache - Ctrl + Shift + R, otherwise the last selected value may remain selected.
This is example form code:
{{Form::checkbox('selection[]', '1')}}
{{Form::checkbox('selection[]', '2')}}
{{Form::checkbox('selection[]', '3')}}
{{Form::checkbox('selection[]', '4')}}
This is example code for saving:
$selection = json_encode(Input::get('selection'));
It will be then save into MySQL table in 'selection' column.
So now, how can I retrieve the data into the form edit mode?
Thanks.
$selections = json_decode($selectionFieldFromDB); // pass it to the view
// then just:
#foreach (range(1,4) as $i)
{{ Form::checkbox('selection[]', $i, in_array($i, $selections)) }}
#endforeach
Mind that if you have an attribute on your model selection, then Laravel will ignore in_array() part, since it first checks the values on the bound object.
So if you want it to work like I suggested, don't use selection name for the checkboxes if that's the name of your model attribute.