Laravel : Form Data Binding - php

I need to bind the data to the related field using Form data binding , but the data does not showed on the fields at all.I don't know what the problem exactly that prevent the data to appear.
Controller:
public function edit($id){
$datum = RatingDatum::findorfail(1);
return view('rating.index',compact('datum'));
}
View:
<div class="plan bg-plan">
{!! Form::model($datums,['method' => 'PATCH','action'=>'RatingDataController#update'],$datums->id]) !!}
<div class="radio">
<label>
{!! Form::radio('customize_question_id')!!}{{$datums->value}}
</label>
</div>
<div class="form-group">
{!! Form::label('comment','Comment :') !!}
{!! Form::textarea('comment' ,null,['class'=>'form-control', 'rows' => 4]) !!}
{!! Form::label('reference','Reference:') !!}
{!! Form::textarea('reference',null,['class'=>'form-control', 'rows' => 1]) !!}
</div>
{!! Form::submit('Submit Data', ['class' => 'btn btn-success submit']) !!}
{!! Form::close() !!}
</div>
dd($datum);
#attributes: array:15 [▼
"id" => 1
"organisation_id" => 8
"sector_id" => 1
"country_id" => 1
"dimension_id" => 12
"question_angle_id" => 1
"customize_criteria_id" => 33
"customize_question_id" => 7591
"question_weight" => 20
"actual_score" => 75
"value" => "The company has made a formal commitment to promoting voluntary community initiatives and has set up quantitative targets in this regard."
"comment" => ""
"reference" => ""
"created_at" => "2015-12-21 11:28:38"
"updated_at" => "2015-12-21 12:22:25"
]

The data is not showing up in the form is because you are referencing the wrong variable.
You are passing $datum and in the view form, you are accessing $datums. That is the reason why your form is empty.
Solution:
Do either of the following:
Update controller method's $datum to $datums
OR
Update form model $datums to $datum

Related

Laravel - Select input not keeping old value

I have a piece of code:
{!! Form::select('option_employee_review', old('option_employee_review', $employeeReviews), $employeeReviews, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
It saves the value to the database correct. When i go to edit the item again the select input does not keep the old value that's in the database. How do i make it so that the select input does keep its old value.
$employeeReviews:
[
2843 => "Medewerker review 1"
2849 => "Medewerker review 2"
]
I am not using your syntax, but something like this will do I think.
<option value="{{$channel->id}}" {{ (old("channel_id") == $channel->id ? "selected" : "" ) }}>{{$channel->title}}</option>
Second parameter to select function must be array of options.
Try changing it like this
{!! Form::select('option_employee_review', $employeeReviews, old('option_employee_review', $employeeReviews), ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
Or based on your parent object let's say employee you can try
{!! Form::select('option_employee_review', $employeeReviews, $employee->option_employee_review, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}

Show date data in edit form laravel

In edit form laravel, how to show date value in edit form?
<label class="control-label col-md-1">{!! trans('view.dataanggota.form.tgllhr') !!}</label>
<div class="col-md-3">
<div class="input-icon">
{!! Form::date('tgllhr', (isset($data->tgllhr) && $data->tgllhr ? $data->tgllhr : date('d-m-Y')), ['class' => 'form-control','id' => 'tgllhr','value' => '$data->tgllhr']) !!}
</div>
</div>
Try this
{!! Form::date('tgllhr', $data->tgllhr ?? date('d-m-Y'), ['class' => 'form-control','id' => 'tgllhr','value' => '$data->tgllhr']) !!}
it will work if you are using php 7.x

Laravel {{Form::Select}} not passing values to POST request parameters

this problem occured recently on a website on my development server, which runs Laravel 5.2.45. I have a form which I use to pass data through a POST request on a controller. On the form there are 2 multiselects, one for the available locations and one for the selected locations. The user can pass a location from the available locations to the selected locations. The problem is that once the form submits, the selected locations do not appear anywhere on the request parameters, but the available locations appear. This was working for a long time and suddendly it stopped working.
Available locations multiselect
<div class="form-group clearfix{{ $errors->has('available') ? ' has-error' : '' }}">
{!! Form::label('available', 'Available Locations', ['class'=> 'col-xs-12 pl-0 pr-0']) !!}
<div class="col-xs-12 pl-0 pr-0">
{{Form::select('available[]', $taskgroup['available'], null, ['id'=> 'multiselect', 'class'=> 'form-control', 'size'=> 8, 'multiple' => 'multiple']) }}
</div>
#if ($errors->has('available'))
<span class="help-block">{{ $errors->first('available') }}</span>
#endif
Selected locations multiselect
<div class="form-group clearfix{{ $errors->has('locations') ? ' has-error' : '' }}">
{!! Form::label('locations', 'Selected Locations', ['class'=> 'col-xs-12 pl-0 pr-0']) !!}
<div class="col-xs-12 pl-0 pr-0">
{{Form::select('locations[]', $taskgroup['locations'], null, ['id'=> 'multiselect_to', 'class'=> 'form-control', 'size'=> 8, 'multiple' => 'multiple']) }}
</div>
#if ($errors->has('locations'))
<span class="help-block">{{ $errors->first('locations') }}</span>
#endif
The part which handles the request in the controller
$taskgroup = Taskgroup::with('locations')->findOrFail($id);
$input = Input::all();
//dd($request,$input);
unset($input['available']);
if(!empty(array_values($input['media_url'])[0]['title'])){
$media_url = Taskgroup::sortArray($input['media_url'], 'order');
$input['media_url'] = json_encode(array('media' => $media_url));
} else {
unset($input['media_url']);
}
$messages = ['between' => 'The :attribute must be between :min - :max.'];
$validation = Validator::make($input, array_merge(Taskgroup::$rules, array('locations'=>'required|between:1,'.$input['max_users'])), $messages);
The uncommented data dump of the input data
"_method" => "PUT"
"_token" => "Token Here"
"name" => "Beer"
"short_description" => "Short Description<br>"
"_wysihtml5_mode" => "1"
"long_description" => "<div>Long Description</div>"
"pin_color" => "red"
"start_date" => "2018-12-03 09:00"
"end_date" => "2019-12-31 20:00"
"media_url" => array:1
"file_names" => array:1
"delete-file-85" => "0"
"delete-file-86" => "0"
"max_users" => "32"
"max_arrival_time" => "120"
"max_completion_time" => "30"
"client_id" => "22"
"q" => ""
"available" => array:977
"files" => array:1
This is my first post on the website, so please excuse any formatting errors. Thank you in advance.
Try to use
{!!Form::select(....)!!}

Laravel duplicate all fields of form using blade and insert multiple rows from a single query using eloquent

I want to have a add another form button that will duplicate all the fields of my form but will still remain one submit button and pass all the data of that into the controller to insert it using one single query.
Here is the create.blade.php that opens the form
#extends('encoder-dashboard.layouts.app')
#section('css')
<link rel="stylesheet" href="/css/datepicker.css">
#endsection
#section('content')
<section class="content-header">
<h1>
Add New Analysis Request
</h1>
</section>
<div class="content">
#include('adminlte-templates::common.errors')
<div class="box box-primary">
<div class="box-body">
<div class="row">
{!! Form::open(['route' => 'encoder.analysis-request.store']) !!}
#include('encoder-dashboard.analysis-request.fields')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
<script src="/js/datepicker.js"></script>
<script>
$('#datepicker').datepicker({
autoclose: true
});
</script>
#endsection
And here are the fields listed in fields.blade.php
<!-- Client Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('client_id', 'Client Name:') !!}
{!! Form::select('client_id[]', $client, null, ['class' => 'form-control','required'])!!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('sample_code', 'Sample Code:') !!}
{!! Form::text('sample_code[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('sample_description', 'Description:') !!}
{!! Form::text('sample_description[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('quantity', 'Quantity:') !!}
{!! Form::number('quantity[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('analysis_requested', 'Analysis Request:') !!}
{!! Form::text('analysis_requested[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Password Field -->
<div class="form-group col-sm-6">
{!! Form::label('special_instruction', 'Special Instruction:') !!}
{!! Form::text('special_instruction[]', null, ['class' => 'form-control','required']) !!}
</div>
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-success', 'onClick' => 'this.form.submit(); this.disabled=true; this.value="Saving…";']) !!}
Cancel
</div>
All the fields above from the form will be duplicated and pass it as an array right? How do I do that maybe using jQuery or simple vanila JS as well?
and if I pass this data it would look like these.
array:8 [▼
"_token" => "xEGtr4h0f4gim0sLvABZivvq36UNaRHWJ08PMWTI"
"client_id" => array:1 [▼
0 => "1"
]
"sample_code" => array:1 [▼
0 => "eqwewqeqewqrwereqwe"
]
"sample_description" => array:1 [▼
0 => "dasdsadsadsadasd"
]
"quantity" => array:1 [▼
0 => "232"
]
"analysis_requested" => array:1 [▼
0 => "dsadsadsadsadsad"
]
"special_instruction" => array:1 [▼
0 => "dwqewqewqewqewqeq"
]
]
This is my query below when inserting one single record.
$analysis_request = $this->analysisrequestRepository->create([
'client_id' => $input['client_id'],
'sample_code' => $input['sample_code'],
'sample_description' => $input['sample_description'],
'quantity' => $input['quantity'],
'analysis_requested' => $input['analysis_requested'],
'special_instruction' => $input['special_instruction'],
'status' => 'for_testing'
]);
$id = $analysis_request->id;
$request_actors = RequestActors::create([
'request_id' => $id,
'encoder_id' => Auth::guard('encoder')->user()->id,
'microbiologist_id' => null
]);
this is my query above for inserting one single record. I also performed another query to insert the id of the request which has a relationship to the analysis request that is inserted in the first query.
Now if I will make it to insert many I will do an array inside an array and insert it each record right? Like this.
$data = array(
array(
'client_id' => $input['client_id'],
'sample_code' => $input['sample_code'],
'sample_description' => $input['sample_description'],
'quantity' => $input['quantity'],
'analysis_requested' => $input['analysis_requested'],
'special_instruction' => $input['special_instruction'],
'status' => 'for_testing'
),
// and so on.
);
AnalysisRequest::insert($data);
But I had an error in the code above it says that Array to string conversion
Or will it be better if I do an for each?
My only goal here is to perform an insert many query depends on the entries to add.
Appreciate if someone can help.
Thanks in advance.
You are getting an array of array and you want to put (for example) into client_id one array [1]
For that reason you have "Array to String conversion".
You can do
$data = array(
array(
'client_id' => $input['client_id'][0],
'sample_code' => $input['sample_code'][0],
'sample_description' => $input['sample_description'][0],
'quantity' => $input['quantity'][0],
'analysis_requested' => $input['analysis_requested'][0],
'special_instruction' => $input['special_instruction'][0],
'status' => 'for_testing'
),
// and so on.
);
AnalysisRequest::insert($data);

how to make a update page with hasMany relatioins in laravel

I'm trying to create a form to update a model which has one to many relations. The structure is like
item => {[arg1, arg2], [arg1, arg2], [arg1, arg2]}
Each related model has two fields to be updated. I used the code below but I don't know how to code the controller side.
#extends('app')
#section('content')
{!! Form::open(array('route' => array('Topic.update', $topic->id), 'method' => 'PATCH')) !!}
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', $topic->title, array('maxlength' => 250)) !!}
#foreach($topic->pairs as $key=>$value)
<div>
{!! Form::label('title', 'Pair ' . $key . ': ') !!}
{!! Form::text('a' + $key, $value->arg1) !!}
{!! Form::text('b' + $key, $value->arg2) !!}
</div>
#endforeach
{!! Form::submit() !!}
{!! Form::close() !!}
#endsection
When I dd($response->all()), it shows below:
array:9 [▼
"_method" => "PATCH"
"_token" => "dInpiSa6O2KIMftFnICQtVM873nUF2Zp2HlzeN4S"
"title" => "Actors and Movies"
0 => "Rain Man"
1 => "Rush Hour"
2 => "Edward Scissorhands"
3 => "Titanic"
4 => "The Devil Wears Prada"
5 => "Fast & Furious"
]
The related items are not pairs but only arg1. So how should I code hasMany model? Thanks
The + sign in PHP is not used for concatenation of strings. It is math operator. To concatenate strings you should use dot (.).
So change your code like this:
Form::text('a' . $key, $value->arg1)
Form::text('b' . $key, $value->arg2)
Because previously you were calculating the sum of a and $key, thats why you get numerical keys in the request.
If you wish to access your pairs in array, from the form request you can change the inputs to be arrays, like this:
Form::text('items[' . $key . '][arg1]', $value->arg1)
Form::text('items[' . $key . '][arg2]', $value->arg2)
With last example the request will look like this:
items => [
[ arg1, arg2 ],
[ arg1, arg2 ],
....
]

Categories