Get selected value in form, seletbox - php

I have a problem and I can't resolve it, please help me. So I have my form :
{{ Form::open(array('url'=>'/administration/student/addMarks','method' => 'post')) }}
#foreach($aObjectsInGroupe as $object)
{{ Form::hidden('id_object[]',$object->id) }}
{{ Form::label($object->name) }}
{{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}
<br />
#endforeach
{{ Form::hidden('id',$aOneStudent['id']) }}
{{ Form::submit('Add mark',array('class'=>'btn btn-primary')) }}
{{ Form::close() }}
In my StudentController I have a method for get the mark from student_id and object_id:
public function getMarkByStudentAndObject($nIdStudent, $nIdObject){
$aMark = \Mark::where('student_id', '=', $nIdStudent)
->and('object_id', $nIdObject)
->get()
->toArray();
}
$aMarsks it's a table :
$aMarks = array(
'0'=>'0',
'1'=>'1',
'2'=>'2',
'3'=>'3',
'4'=>'4',
'5'=>'5',
'6'=>'6',
'7'=>'7',
'8'=>'8',
'9'=>'9',
'10'=>'10',
);
It's possible to call the method getMarkByStudentAndObject in :
{{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}
to get the selected value?
Help me please. Thx in advance.

maybe you need to do this:
<select name="note[]" class="form-control">
#foreach($aMarks as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
</select>
You should also check your Eloquent query, instead of ->and use another ->where

Try the lists() function of the Query Builder
\Mark::where('student_id', '=', $nIdStudent)
->and('object_id', $nIdObject)
->lists('column_1', 'column_2');
You then get the values of column_1 as the keys of the array and column_2 as the values.
http://laravel.com/docs/4.2/queries#selects

You should be able to directly call this from the Form::select. A call to
Mark::getMarkByStudentAndObject($aOneStudent['id'], $object->id)->note
would give you the Mark object and then you would need to get the note column which stores the value in the Mark object. Resulting in a call like this:
{{
Form::select(
'note[]',
$aMarks,
Mark::getMarkByStudentAndObject(
$aOneStudent['id'],
$object->id
)->note, array('class'=>'form-control')
)
}}
At the moment I can't verify if this is working because I have no possibility to test it but it should work as Blade is just a wrapper for PHP calls.

Related

how to pass variable in route name in laravel?

I have defined route
Route::get('/edit-industry/{id}', 'Industries#edit')->name('admin.editIndustry');
And passing variable by
{{ route('admin.editIndustry', ['id'=>1]) }}
OR
{{ route('admin.editIndustry', [1]) }}
This is not working. How to pass variable here?
wow, why are wrong answers (or answers for questions which were not asked in this case) upvoted?
EkinOf is correct, you can do
{{ route('admin.editIndustry', 1) }}
Btw your first one works too and is necessary, if you have more than 1 parameter
{{ route('admin.editIndustry', ['id'=>1]) }}
{{ route('admin.editIndustry', ['id'=>1, 'something'=>42]) }}
If you have only one parameter you can do that :
{{ route('admin.editIndustry', 1) }}
Passing single parameter:
##Defining Route:##
Route::get('edit-industry/{id}', ['as' => 'admin.editIndustry', 'uses' => 'Industries#edit']);
##Calling Route:##
{{ route('admin.editIndustry',[$id]) }}
Passing multiple parameter:
##Defining Route:##
Route::get('edit-industry/{id}/{step}', ['as' => 'admin.editIndustry', 'uses' => 'Industries#edit']);
##Calling Route:##
{{ route('admin.editIndustry',[$id, $step]) }}
Simply try like this
View
{{URL::to('/edit-industry/1')}}
Route
Route::get('/edit-industry/{id}', 'Industries#edit')
Controller
public function edit($id){
// use $id here
}
Hope you understand.
Using generating URLs from Named Routes route():
{{ route('admin.editIndustry', 1) }}
Using URLs url():
{{url('/edit-industry', [1])}}
You can Directly pass with route name
{{ URL::to('/edit-industry/1') }}

Laravel : how to check the check-box if it's value is exist in a array

I have an services in an array, and I have a list of checkbox which have the same values in this array no I need to make each checkbox to be checked if it's in the array
here is my Blade code
{{ Form::checkbox('pro_serves[]', 'pool', null) }} Pool <br/>
{{ Form::checkbox('pro_serves[]', 'gym', null) }} Gym <br/>
{{ Form::checkbox('pro_serves[]', 'maintenance', null) }} Maintenance <br/>
{{ Form::checkbox('pro_serves[]', 'dish', null) }} Dish <br/>
{{ Form::checkbox('pro_serves[]', 'kidsArea', null) }} KidsArea <br/>
{{ Form::checkbox('pro_serves[]', 'parking', null) }} Parking
and here is my Controller
public function editProject($id)
{
$proId = Projects::findOrFail($id);
$proImg = ProjectsImages::where('image_id', $id)->get();
$proPln = ProjectsPlans::where('image_id', $id)->get();
$services = $proId->pro_serves;
$service = explode(',', $services);
return View::make('admin.manageProject.editProject', compact('proId', 'proImg', 'proPln', 'service'));
}
now as you see I am sending the service as an array to the view how can I implement this?
and I welcome any new ideas
Assuming you're either using an old version of laravel, or the laravelcollection/html package, you merely need to pass a third argument.
This is a checked checkbox:
{{ Form::checkbox('pro_servers[]', 'pool', true) }}
So, to have it checked if the value is in the array you can simply do:
{{ Form::checkbox('pro_servers[]', 'pool', in_array('pool', $theArray)) }}
There you go.
This is actually covered in the documentation, which I always recommend taking a look at before posting on SO.

form model binding in laravel 5.2

I have been reading about form model binding https://laravelcollective.com/docs/5.0/html#form-model-binding
It's very cool to populate DB values in html form.
I tried like this and this works fantastic.
{{ Form::model($university,array('url' => admin_path('universities/edit'),'id' => 'add_university','name' =>'add_university','data-validate'=>"parsley")) }}
{{ Form::label('university_name', 'University name',array('class'=>'control-label')) }}
{{ Form::text('university_name')}}
{{Form::close()}}
But the problem is here, Cause i want to add more attributes in input like class SO i am using
{{ Form::label('university_name', 'University name',array('class'=>'control-label')) }}
{{ Form::text('university_name','',array('class' => 'form-control'))}}
If i leave blank valuecolumn then nothing populate in textbox and if i using like this
{{ Form::label('university_name', 'University name',array('class'=>'control-label')) }}
{{ Form::text('university_name',$university->university_name,array('class' => 'form-control'))}}
Then what is use of model binding.
Please explain.
Thanks
{{ Form::text('university_name','',array('class' => 'form-control'))}}
It should be:
{{ Form::text('university_name',null,array('class' => 'form-control'))}}
'' means the real string, not null.
thanks, mathielo, for helping me on grammar

perplexing check box behavior

I have a edit form with some checkboxes that I am trying to make checked when the associated many to many relationship is established.
Distributors belong to many beers
Beers belong to many distributors
in my controller I have:
$breweries = Brewery::lists('name', 'id');
$all_dist = Distributor::all();
$beer = Beer::find($id);
$distributions = [];
foreach ($beer->distributors as $distributor)
{
$distributions[$distributor->id] = BeerDistribution::where('beer_id', '=', $beer->id)
->where('distributor_id', '=', $distributor->id)->first()->price;
}
return View::make('beers.edit', ['beer' => $beer, 'distributors' => $all_dist, 'distributions' => $distributions, 'breweries' => $breweries, 'styles' => $styles]);
and I in my edit form I have:
{{ Form::model($beer, ['route' => ['beers.update', $beer->id], 'method' => 'PATCH']) }}
#foreach ($distributors as $distributor)
<?php $carried = in_array($distributor->id, array_keys($distributions)) ? true : false ?>
{{ Form::checkbox('distributors[]', $distributor->id, $carried); }}
{{ Form::label($distributor->name) }}
{{ Form::label('price' . $distributor->id, 'Retail:') }}
<?php $price = $carried ? $distributions[$distributor->id] : null ?>
{{ Form::text('price' . $distributor->id, $price ) }}
#endforeach
{{ Form::submit('Save') }}
{{ Form::close() }}
Basically I am passing an associated array of each distributor_id => price. This array also tells me which distributors the beer already belongs to so that I can mark those checked off in my edit form.
Here's where things get wonky. When I load this form, all the checkboxes will be checked no matter what. If I change my controller loop to this:
foreach ($beer->distributors()->lists('distributor_id') as $distributor_id)
Then I can do create my array.
Why does calling $beer->distributors in the controller would result in all the checkboxes being checked?
The problem is with the last argument : $carried
{{ Form::checkbox('distributors[]', $distributor->id, [ "checked" => $carried ]); }}
Pass the last parameter as array, and tell it exactly which attribute to modify and the attribute value.

Laravel: how do I pass a value from a form to a controller?

I have a form:
{ Form::open(array('action' => 'RatesController#postUserRate', $client->id)) }}
{{ Form::text('rate', '', array('placeholder' => 'Enter new custom client rate...')) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
How do I pass my $client->id value through the form to my controller method?
I currently have a controller method that looks like this:
public function postUserRate($id)
{
$currentUser = User::find(Sentry::getUser()->id);
$userRate = DB::table('users_rates')->where('user_id', $currentUser->id)->where('client_id', $id)->pluck('rate');
if(is_null($userRate))
{
...
}else{
....
}
}
And the error log says "Missing argument 1 for RatesController::postUserRate()"
Any ideas on how to pass this $client->id into my controller so I can use it as I want to above?
Add {{ Form::hidden('id', $client->id) }}to the form. Then, when it's posted, you can fetch its value per usual with Input::get('id').
Also, remove the postUserRate method's argument.
You simply use :
Form::open(array('action' => array('Controller#method', $user->id)))
the variable $user->id is passed as argument to the method method, also this last one should recieve an argument as well, like so : method($userId)
Source : Laravel documentation

Categories