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.
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 got value from database and then i need to set those value to textbox. I have created a controller file with the method name of edit look like below
userdata.blade.php:
public function edit($id)
{
echo "You have clicked edit link".$name;
$editdata = DB::table('newuser')->where('Id','=',$id)->get();
return View::make('editdata',array('list' => $editdata));
}
I have passed array of value as parameter to the view file.now i need to diaplay the value of name to textbox.how to do that in html page using laravel. My html page look like below
editdata.blade.php:
<html>
<head></head>
<body>
<div>
{{Form::open(array('url' => 'login', 'method' => 'post'))}}
{{Form::label('name','Name',array('id'=>'label-name'))}}
{{Form::text('name',{{$list->Name}}}}
{{ Form::close() }}
</div>
</body>
</html>
can anyone tell me that what mistake i did.Thanks in advance
Just remove the curly brackets, you are already "inside" PHP code and don't need them:
{{ Form::text('name',$list->Name) }}
Also you get a collection from your controller you probably want to do:
$editdata = DB::table('newuser')->where('Id','=',$id)->first();
Or even:
$editdata = DB::table('newuser')->find($id);
get() returns a collection (multiple rows) and not the model itself. You can use User::find($id) which gives you direct access to the model with the specified Id.
When not using eloquent just replace get() with first()
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)
I am attempting to create a drop down selection box for various attributes of a product which the user would select on a product page, such as weight or flavour or colour. My database for the products is currently as follows:
products(id,productname,productprice)
productattributes(id,productid,attributename,value)
I'm using Laravel. So far in my controller I have:
public function show($id)
{
$product = DB::table('products')->where('slug', $id)->take(1)->get();
$prodid = DB::table('products')->where('slug', $id)->pluck('id');
$prodattr = DB::table('productattributes')->where('prodid', $prodid)->get();
return View::make('product/singleproduct', ['product' => $product, 'prodattr' => $prodattr]);
}
And in the view I have:
#foreach ($prodattr as $prodattribute)
{{ $attrname = $prodattribute->attributename }}
{{ $attrvalue = $prodattribute->value }}
#endforeach
This gives me the output:
Flavour Unflavoured
Flavour Strawberry
Weight 1kg
So I have the data I need, but my problem is how to get this into a select box for each attribute. I won't always know what the attribute names will be (colour may be added, for example) or how many there will be, so I don't want to hard code 'Flavour' etc in. I essentially want to generate HTML like this:
<select name="flavour">
<option value="Unflavoured">Unflavoured</option>
<option value="Strawberry">Strawberry</option>
</select>
<br>
<select name="weight">
<option value="1kg">1kg</option>
</select>
Does anyone have any ideas? I'm thinking a loop within the loop, or maybe my table layout is less than ideal? I thought about adding another query within the foreach loop, but this seems counterproductive as I think I have all the data I need. I also wondered if it would be possible to combine or merge the arrays somehow? Any help is much appreciated!
You could create a third table in order to seperate custom attributes and available values for each attributes.
Example of structure I would use:
products(id, name, price)
product_meta_name(id, name)
product_meta_value(id, product_meta_name_id, product_id, value)
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.