I want to load data to my view, particularly a variable in my controller items.
But in my controller I want to dynamically set what items is before it is push to the view.
I tried the code below but it doesn't work. The view loads up with an error "Undefined variable: items" .
public function index(){
if ($this->input->post('filter'))
{
$search = $this->input->post('filter');
$test=$this->upload_model->test_r();
$data['items']=$test;
}
else
{
$data['items']=$one;
}
$data=array ('other'=>$othrs, 'links'=>$links);
$this->load->view('gallery_view', $data);
}
How I would like this to work is that $data['items'] is set to $one by default, when the page loads up, but on the page I have a select box, so I want that if the select box is is changed that $data['items'] would be set to something else. But this is only if the select box is used, else, it should look up with the $data[items]=$one. The $data array has other values that need to be loaded in the view such as "others" and "links".
The select box on my view
<?php echo form_open(base_url().'page') ?>
<form class="form-inline" role="form">
<select class="form-control" id="filter" name="filter" onchange="this.form.submit()">
<option value="1">1</option>
<option>2</option>
<option>3</option>
</select>
</form>
<?php echo form_close(); ?>
The index controller function above is for my controller "Pages". The value from the select box is captured fine, when I do an echo on the value passed it shows up correctly.
The problem is getting the view data "items" to change depending on if the select box is used.
How do I fix this?
Do this line before if condition
$data=array ('other'=>$othrs, 'links'=>$links,'items'=>"");
Remove below line in controller.
$data=array ('other'=>$othrs, 'links'=>$links);
Because you are re-assign $data after setting $data["items"]
Related
I am trying to store specific value in laravel. Here Select User not required field but in foreach loop hidden field proj_id must has value. Suppose there are 5 rows and I want to store only 2nd and 5th user along with proj_id from hidden field. Here I want to mention that in controller I also kept delete operation so that I can remove previously inserted SAME PROJECT ID related record. For example If I want to store 2nd and 3rd user, that time only 2nd and 3rd users record will remove first then insert. In my code there are logical error but could not find solution. Thanks in advance
<form action="{{ url('/save-project') }}" method="POST">
<tr>
#foreach($projects as $val)
<td>
<input type="hidden" name="proj_id[]" value="{{$val->id}}">
<select name="user_id[]">
<option value="">Select User</option>
<option value="2">x</option>
<option value="4">y</option>
</select>
</td>
#endforeach
</tr>
<input type="submit">
</form>
Controller
$countUserID = count($user_id);
assign_project::where('flag','Y')
->WhereIn('proj_id',$request->proj_id)
->delete();
for($i=0;$i<$countUserID;$i++){
$assign_project = new assign_project();
$assign_project->proj_id = $request->proj_id[$i];
$assign_project->user_id = $request->user_id[$i];
$assign_project->save();
}
JSON fields are gaining more popularity since they became officially supported in MySQL 5.7.8. Even the popular Spatie Laravel Medialibrary package use them, so why shouldn’t we?
To create a JSON field, all we need to do in Laravel migration is use ->json() method:
$table->json('array_data');
Next, you need to tell your model to cast that column from JSON to an array automatically:
class Product extends Model
{
protected $casts = [
'array_data' => 'array'
];
}
This way, you will receive $array_data as array and don’t need to do json_decode() at all.
My dynamic drop down list is not working when I am trying to navigate to update page by using url id and then its showing errors.The result suppose to be a name of city that is associated with id in the database.
controller class
public function updateAddress($edit_id)
{
$data=[];
$data['edit_id']=$edit_id;
$address_data = $this->address->find($edit_id);
$data['first_name']=$address_data->first_name;
$data['last_name']=$address_data->last_name;
$data['street']=$address_data->street;
$data['zip_code']=$address_data->zip_code;
$data['city']=$address_data->city;
return view('update/edit',$data);
}
Here is my blade file. The rest of the fields are working properly except drop down list. I am using foreach loop to display data into the drop down list.
<select id="city" name="city" >
#foreach($data as $cities)
<option id="city" value="{{$cities->edit_id}}" selected="selected">{{$cities->city}}</option>
#endforeach
</select>
Update the code in the controller so that:
return view('update/edit',$data);
Is changed to:
return view('update/edit', ['data' => $data]);
change your controller code to
return view('update/edit',compact('data'));
and change your view page like
{{ $data['edit_id'] }}. you dont want to use #foreach.
I found how to accomplish this on another site and I got it working. My issue is though that I need to use it in my edit form (preferably in the same form as my create form, since my create and edit views use the same form). When I go to my edit page, the "Section" dropdown value is selected as it should be. But the "Subsection" is not, because ofcourse I did not click it. Probably it's a fix too easy for me to see, but I am not quite good with javascript. I basically need the second dropdown (the subsection one) to show the correct options based on which "section" (first dropdown) is selected, on my edit view.
Section Model:
public function subsections()
{
return $this->hasMany('App\Subsection');
}
Subsection Model:
public function section()
{
return $this->belongsTo('App\Section');
}
View:
{!! Form::select('section_id', [null=>'-- Select Section --'] + $sections, null, array('id' => 'section')) !!}
<select id="subsection" name="subsection_id" class="select">
<option>-- First Select Section --</option>
</select>
From my controller: $sections = Section::lists('section', 'id');
Script:
<script>
$(document).ready(function($){
$('#section').change(function(){
$.get("{{ url('api/dropdown')}}",
{ option: $(this).val() },
function(data) {
$('#subsection').empty();
$.each(data, function(key, element) {
$('#subsection').append("<option value='" + key +"'>" + element + "</option>");
});
});
});
});
</script>
Route:
Route::get('api/dropdown', function(){
$id = Input::get('option');
$subsections = Section::find($id)->subsections;
return $subsections->lists('subsection', 'id');
});
If you just want to select the first option in jquery , you can add :
$('#subsection option:eq(1)').prop('selected', true);
Or else if you want to select a particular value, you can do that by
$('#subsection option[value="value_to_be_selected"]').prop('selected', true);
Use this after you have appended all the values to the subsection
In my controller I added:
$subsections = Subsection::where('section_id', '=', $transaction->section_id)->orderBy('subsection', 'asc')->lists('subsection', 'id');
And in my view I did this:
#if(isset($transaction))
{!! Form::select('subsection_id', [null=>'-- Select Subsection --'] + $subsections, null, array('class' => 'select', 'id' => 'subsection')) !!}
#else
<select id="subsection" name="subsection_id" class="select">
<option>-- First Select Section --</option>
</select>
#endif
Problem solved :)
So, what we will do is initially we will send value to the dependent drop-down as "<option value="">Select Something</option>"
and when returning on error create complete drop-down with selected as below value and return, this will reduce lots of javascript calls.
Controller:
if($error){
//create get array of second drop-down, suppose $second_drop
// create complete second drop-down like
$selectbox="";
foreach($second_drop as $option){
$selectbox.="<option value='". $option->id."'";
if($request->option2==$ $option->id){
$selectbox.=" selected ";
}
$selectbox.=">".$option->value."</option>";
}
}
return view('routename', ['selectbox2' => $selectbox]);
View:
<select id="subsection" name="subsection_id" class="select">
{!! $selectbox !!}
</select>
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.