Being newer to Laravel 5, I am trying to update a table, then i show all fields into a form using blade. I am also able to populate the drop-down list (select) in order to show values from another table, but I can't make the drop-down to point to the value in the main table (the one am trying to update).
Controller side: I use $user to pass all user data to the view and $profiles to pass data to fill the drop-down
public function edit($id){
$user = User::find($id);
$profiles = \DB::table('profiles')->lists('desc_profile', >'id_profile');
return view('user.edit')->with('user', $user)->with('profiles', $profiles);
}
View Side:
<div class="col-xs-6">
{!!Form::select('profiles', $profiles)!!}
</div>
In addition, I can update all the fields in the table BUT the values from the drop-down ... So basically everything is working fine for show and update values except for the ones in the .... drop downs...
need help! THX a lot
You can pass an optional 3rd argument to the select method that specifies which option you want to have selected. In other words, you pass in the specific id_profile that you want selected.
<div class="col-xs-6">
{!!Form::select('profiles', $profiles, $user->id)!!}
</div>
I passed in the user id as the 3rd argument only because we have no idea how you are structuring your database, but you should adjust that to match what you need.
Related
I have two tables
tenant=>id,name,email,property_id
property=>id,propertyname,location
The ‘tenant’ and ‘property’ table has a one to relation.
My tenant/create.blade.php has a drop-down list of all the properties in the database available for rent.
For example if in the database the following properties are inserted A,B,C,D,E,F. The drop-down list displays all properties including those already taken. As in figure
I want it to display only properties not taken. For example if out of all properties available A,B,C has previously been assigned the it should only display a drop-down list of D,E,F
I think I can do this in the controller but dont know how.
Here is my TenantsController create function
public function create()
{
$property = Property::pluck('propertyname','id');
return view('tenants.create',compact('property'));
}
The create.blade.php has the following code to display the drop-down list
<div class="form-group">
{{Form::label('property name','Property Name')}}
{{Form::text('propertyname','',['class'=>'form-control','placeholder'=>'Property Name'])}}
</div>
There could be more solutions, but right now I can think of $collection->diff() to quickly solve it within your laravel app.
$properties = Property::all(); // write query to fit your needs
$taken = Property::taken(); // write query to retrieve the unavailable properties
$diff = $properties->diff($taken);
$options = $diff->pluck('name', 'id')->toArray();
Now, pass the $options to your blade view & loop through it to generate <option>.
My main page renders a list of data coming from controller with foreach
#foreach ($sales as $sale)
<button id="{{$sale->id}}" #click="editClicked({{$sale}})">
#endforeach
I have an edit component placed on the page like this, I display it modally via showEditModal conditional
<edit v-if="showEditModal" #hide="showEditModal=false"></edit>
The component in brief is declared in Edit.vue:
<template name="edit">
...
</template>
This is simply a standard form with input fields, bound via v-model to the sale.
Essentially it is an update form, I intend to load the data from the main page into each input field to be edited.
My app.js simply sets showEditModal = true, in order to display the edit form on top of the main page.
Basically i don't want to have to call controller via GET method on loading the modal since i already have the data in the main page as $sale object, so im just wondering how do I pass in the $sale to the Edit.vue component ?
I thought that in the <edit> component usage, it would need to bind the sale object, however I'm unsure how that would work since it comes from the foreach loop.
I do, also have the data in the app.js method as passed in via #click="editClicked({{$sale}})", but again, i'm unsure how to use that to pass through ?
You're right, you would want to pass the current sale item as a property to the edit modal. What I would do is add a data property to your main Vue called selectedSale.
data:{
selectedSale: null
}
Then in your editClicked method, set selectedSale
methods:{
editClicked(sale){
this.selectedSale = sale
...
}
}
Finally, pass it as a property.
<edit :sale="selectedSale" v-if="showEditModal" #hide="showEditModal=false"></edit>
I have a section of a form that dynamically loads different sets of fields based on the user's selection in a control. I'm using a javascript event handler to detect when the selection changes, and using AJAX (with HTML payload) to pull in the proper set of fields.
I would like to be able to use Laravel's Form::getValueAttribute() method to automatically fill in the form fields' values in both the static and dynamic form parts. However, the partial view that is loaded by my AJAX call does not have the same instance of the Form class as the view with my main Form, so I can't simply call getValueAttribute() in the partial.
My thought is to make the AJAX call a POST, and serialize the necessary data (a subset of Input::old() or the model data depending whether the page is loaded as the result of validation errors, or an UPDATE request) to send along with the POST so that the HTML fragment I get back has the values set properly.
Is this the best way to get what I want? If so, does Laravel have any tools to help with the serialization of form data? If not, what might be a better approach?
I've found an approach I like better. When the view is loaded normally I use AJAX as usual to load the partial. But when the view is loaded for a validation post-back or for editing, I use Laravel's Views' nest method to nest the partial view containing the proper fields directly into the response. The partial then has access to all the Input and error data I need. The user is still able to change the field set as usual but I put up a confirm prompt for them if they have already set some values in a field set they previously selected. If they decide to proceed anyway, the field set is cleared and a new field set is brought in via AJAX as usual.
My code looks something like this:
Controller:
public function newThing() {
if ( Request::session()->has('errors') ) {
// this is a validation post-back
return View::make('thing')
->nest('fields', 'fields_partial');
} else {
// just a normal unfilled form
return View::make('thing');
}
}
public function editThing() {
return View::make('thing')
->nest('fields', 'fields_partial');
}
View: thing.blade.php (just a snip of it)
...
<form>
...
<select id="picker">...</select>
<div class="sub-fields">
{{ isset($fields) ? $fields : '' }}
</div>
...
</form>
...
<script>
$('#picker').change(function() {
// if any .sub-fields inputs have been changed, get confirmation from the user
// if user confirms, do ajax stuff to replace .sub-fields contents with new field set
// otherwise cancel the change
});
</script>
I am new to Yii framework.
I have a form with three fields. I need one of those be a select drop down element that its data comes from previously added data which are in mysql table.
How can I do it ?
If you have a model set up for the table that contains the data you want to use in your dropdown list you can use the CHtml::dropDownList() method the render a dropdown list, and CHtml::listData() to render that model into items for the list, for example;
echo CHtml::dropDownList(
'attribute_name',
'',
CHtml::listData(MyOtherModel::model()->findAll(),'id','name')
);
I use Gii a lot, which uses CActiveForm widget to display forms, if your form uses CActiveForm too you could render your dropdown something like;
$form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
));
...
echo $form->label($model,'attribute_name');
echo $form->dropDownList(
$model,
'attribute_name',
CHtml::listData(MyOtherModel::model()->findAll(),'id','name')
);
...
$this->endWidget();
Note that CActiveForm uses CHtml::activeDropDownList() rather than CHtml::dropDownList() that I used in my first example, hence the slight difference in syntax between my two examples.
The issue is what I say in the title. The parameter of the index selected in the first dropdown box is not sent to the controller. Therefore the controller cannot pass any value to the model etc. If I harcode saying $pais_id = 1 and send that to the Model it works, so this means, the issue is in the controller not getting it from the jquery.
VIEW
<script type="text/javascript">
//jquery code for source list
$(document).ready(function(){
$('#country').change(function() {
if ($(this).val()!='') {
$("#source").load("/CI-3/controllers/control_form.php",{pais_id: $(this).val()});
}
});
}); // end of country and city function
</script>
The problem must be there because I don't visualize the process:
Jquery detects the changing in the select dropdown list and fetches the selected id. Alright, but what happens next ? it sends it to the controller, yes, and? the controller forwards it to the model, the model does a sql search and returns an array back to the controller and the controller forwards it to the view, but, how does #source in the line above get affected after all that?, so it will not modify itself
$source['source'] = $this->model_form->get_source($pais_id);
should be
$data['source'] = $this->model_form->get_source($pais_id);
in controller. third parameter in view is if it's returned or echo'd. all values are passed in the 2nd parameter as an array.