Show date data in edit form laravel - php

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

Related

Why do I have such field names in my laravel project, despite all others are OK?

I'm trying to configure simple laravel booking form for my project, but the names of fields are like this "quickadmin.bookings.fields.first_name*" but not like I want them to be, like this "first name".
I've tried to look through all files of project, where those forms could be configured, but due to lack of experience I can't find anything.
Here's some code from create.blade.php file
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('first_name', trans('quickadmin.bookings.fields.first_name').'*', ['class' => 'control-label']) !!}
{!! Form::text('first_name', old('first_name'), ['class' => 'form-control', 'placeholder' => '', 'required' => '']) !!}
<p class="help-block"></p>
#if($errors->has('first_name'))
<p class="help-block">
{{ $errors->first('first_name') }}
</p>
#endif
</div>
</div>
I want the field to be named more common, like "First name".
That's not a field name but an item from the translation files. So you should have in the resources/lang/en (en, or whatever language you use) a quickadmin.php file that contains an array like this:
return [
'bookings' => [
'fields' => [
'first_name' => 'First name'
]
]
];
If you want to simplify this, then remove some of the arrays.
And you get this "quickadmin.bookings.fields.first_name*" on the screen because the file and the array that I mentioned above are probably missing. So you can avoid that by creating what I just said, or create your own, just remember the first part is the filename, then it follows the items from the array.

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);

Laravel : Form Data Binding

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

How to include a button within a form that does not act as the Submit button

I have a form that I would like a user to fill out with some basic information. However, before submitting the info, I want to include a button I am using to activate Stripe to request payment.
I have tried this so far, however, this button before the submission button is acting as the submit button.
Here is some code:
{{ Form::open(array('route' => 'fans.store')) }}
{{ Form::label('name', 'Name:') }}
{{ Form::text('name', null, array(
'class' => 'input',
));}}
{{ Form::label('email', 'Email:') }}
{{ Form::text('email', null, array(
'class' => 'input',
));}}
<div class="button_row">
<button id="customButton" class="button">Purchase</button>
</div>
<div class="button_row">
{{Form::submit('Submit', ['class' => 'button'])}}
</div>
{{Form::close()}}
Any work arounds? The syntax is blade php (I'm using Laravel 4). Thank you.
Try the following:
{{ Form::button('purchase', 'Purchase with Stripe', array( 'id' => 'purchase', 'onclick'=>'myFunction()')) }}
That creates a purchase button,
labeled "Purchase with Stripe"
Calls myFunction() that launches stripe
<button type="button">
See: http://www.w3schools.com/tags/att_button_type.asp

Categories