how to display old datetime-local value in laravel? - php

how to display old datetime-local value in laravel ? in database i have datetime data with format 2021-09-29 14:07:00, and now i want to show datetime data when open edit page.
I've tried displaying datetime data with code like below, but still not working.
<div class="form-group">
<label for="datetime">Datetime</label>
<input type="datetime-local" name="datetime" class="form-control #error('datetime') is-invalid #enderror" id="datetime" value="{{old('datetime') ? old('datetime') : Carbon\Carbon::parse($data->datetime)->isoFormat('MM/DD/YYYY hh:mm A')}}">
#error('datetime')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
what is the correct way to display it? thx

Related

How do I display my error message as a link in Laravel

I'm trying to create a validation such that if model_name is not unique, it will display a html link to view the existing model_name row. Something like this image, however my current code displays the message like this. What do I have to change in my code?
Here's my code validation code:
'model_name.unique'=>'Model Already exists. Go to page.',
Thanks for any help.
Edit:
Here's the error part of my form:
<input type="text" name="model_name" class="form-control" id="exampleFormControlInput1" placeholder="Model Name">
#error('model_name')
<span class="text-danger">{{ $message }}</span>
#enderror
</div>
That escaped character. You can know more about it here and Laravel Docs.
So to be quick, you can try:
<input type="text" name="model_name" class="form-control" id="exampleFormControlInput1" placeholder="Model Name">
#error('model_name')
<span class="text-danger">{!! $message !!}</span>
#enderror
</div>
Pass the error like this in your blade file:
#error ('model_name')
<span class="text-danger">{!! $message !!}</span>
#enderror
That ensure the link is parsed correctly, right now it's being escaped.
Check this for examples
I suggest using this code: ‌
<input type="text" name="model_name"
class="form-control #error('model_name') is-invalid #enderror"
id="exampleFormControlInput1" placeholder="Model Name">
#error('model_name')
<span class="text-danger">{!! $message !!}</span>
#enderror
You can also add a: ‌is-invalid to the relevant field class if there is an error and style it.

old() value function using data from database not working in Blade

I have a form with 4 input fields, all of them to which I applied an old() function so whenever users load the page after submitting the form, the data that they filled in would retain in the fields for better UX.
My issue is that out of these 4 fields, only 1 has the old() function working. The difference between this field and the others is that:
The name and id attributes for this field are the same, while other fields have different names and IDs respectively.
The input type for this field is type="number" while the other fields are type="text".
Based on these differences, I tried modifying the code to standardize them, and only one solution works which is that I change the input types to "number", however, not all my inputs are numbers, so it is not an ideal solution.
Form blade file:
<div class="form-group mb-3">
<input type="text" class="form-control" name="emp_name" id="company_name"
value="{{ old('emp_name' , (isset($user->emp_detail->emp_name) ? $user->emp_detail->emp_name : old('emp_name')) ) }}">
#if ($errors->has('emp_name'))
<span class="text-danger">{{ $errors->first('emp_name') }}</span>
#endif
</div>
<div class="form-group mb-3">
<input type="number" class="form-control" name="emp_length" id="emp_length"
value="{{ old('emp_length' , (isset($user->emp_detail->emp_length) ? $user->emp_detail->emp_length : old('emp_length')) ) }}">
#if ($errors->has('emp_length'))
<span class="text-danger">{{ $errors->first('emp_length') }}</span>
#endif
</div>
<div class="form-group mb-3">
<input type="text" class="form-control money-prefix" name="emp_monthly_gross_salary" id="monthly_salary"
value="{{ old('emp_monthly_gross_salary' , (isset($user->emp_detail->emp_monthly_gross_salary) ? $user->emp_detail->emp_monthly_gross_salary : old('emp_monthly_gross_salary')) ) }}">
#if ($errors->has('emp_monthly_gross_salary'))
<span class="text-danger">{{ $errors->first('emp_monthly_gross_salary') }}</span>
#endif
</div>
<div class="form-group mb-3">
<input type="text" class="form-control money-prefix" name="monthly_allowance" id="bonus"
value="{{ old('monthly_allowance' , (isset($user->emp_detail->emp_monthly_allowance) ? $user->emp_detail->emp_monthly_allowance : old('monthly_allowance')) ) }}">
#if ($errors->has('monthly_allowance'))
<span class="text-danger">{{ $errors->first('monthly_allowance') }}</span>
#endif
</div>
To be clear, I also have several other forms in which I applied the same old() function, with texts as well as numbers, and they work fine, so I am unsure why the old() function won't work on this form specifically. I also dd() the variables I wanted to output as the old function, so I am sure my controller is already passing the values, my blade just isn't showing it.
Can anyone advise me on this problem?
maybe the issue is the default value, may be set but empty
try to check it with old directly without the default value
ex:
replace
{{ old('emp_name' , (isset($user->emp_detail->emp_name) ? $user->emp_detail->emp_name : old('emp_name')) ) }}
with
{{ old('emp_name') }}
if it works fine with you then you need to enhance the default value condition
ie: (isset($user->emp_detail->emp_name) ? $user->emp_detail->emp_name : old('emp_name')

Populate a form with a model object after redirecting in Laravel

I have a basic CRUD structure for a model that works well. Now I want to redirect to the create method and populate the form with a pre-existing model. It would work by a user selecting an id and then I would select that model and redirect to the create page and populate the form.
This is what I have tried so far
$order = Orders::find($id);
$inventory = Inventory::where(['id' => $order->inventory_id])->first()->toArray();
return redirect()->route('backend.inventory.create', $inventory['bag_id'])->withInput($inventory);
In the above example, it finds the order and selects the single inventory item related to it (I have confirmed that it's definitely selecting an inventory item as expected) and redirects to the inventory creation page. However, when using the ->withInput() method this doesn't seem to populate my form with the data as I expected.
How do I pass data to a form using a redirect?
Adding the form code as requested below. This is just one column of the form as its a huge block of code
<div class="form-group row" id="item-name" v-if="type != '3'">
<label for="name" class="col-md-2 col-form-label text-md-right">Item Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required>
#if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
You can use this :
return Redirect::back()->withInput(Input::all());
If you're using Form Request Validation, this is exactly how Laravel will redirect you back with errors and the given input.
Excerpt from \Illuminate\Foundation\Validation\ValidatesRequests:
return redirect()->to($this->getRedirectUrl())
->withInput($request->input())
->withErrors($errors, $this->errorBag());
try this
<div class="form-group row" id="item-name" v-if="type != '3'">
<label for="name" class="col-md-2 col-form-label text-md-right">Item Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') ?? $inventory['name'] }}" required>
#if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
{{ old('name') ?? ($inventory['name'] ?? '') }} if old data is present it will show old data otherwise $inventory->name which is coming from withInput with is also optional that's why i put ??
ref link https://laravel.com/docs/6.x/requests#old-input
You can use compact() function that creates array containing variables and their values.
$order = Orders::find($id);
$inventory = Inventory::where(['id' => $order->inventory_id])->first()->toArray();
return view('backend.inventory.create', compact('inventory'));
In your view page you can use it like,
<input id="name" type="text" class="form-control" name="name" value="{{ $inventory['name'] }}" required>

How to edit form with data from database tables in laravel

I am doing project in laravel. I want edit form with database table values.
In controller,
$event_note = DB::table('event_note')->where('event_id',$id)->first();
I have blade.php file for particular textarea as,
<div class="form-group clearfix{{ $errors->has('event_note') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Note</label>
<div class="col-md-6">
<textarea class="form-control" name="event_note" rows="4" cols="50">{{ $event_note->note }}</textarea>
#if ($errors->has('event_note'))
<span class="help-block">
<strong>{{ $errors->first('event_note') }}</strong>
</span>
#endif
</div>
</div>
but as event_note doesn't have any record it throws an error as,
Trying to get property of non-object (View:....
for variable $event_note->note.
I don't know how to edit this form if database table dont have any value it should show an empty textarea.
You can use or '' clause:
{{ $event_note->note or '' }}
It's a Blade shortcut for this solution:
{{ isset($event_note->note) ? $event_note->note : '' }}
"first()" will return "null" if nothing is found in database for this id.
Try this:
<textarea class="form-control" name="event_note" rows="4" cols="50">{{ is_null($event_note) ? null : $event_note->note }}</textarea>

Laravel schema builder, update nullable timestamp field

I am developing a project where I have to store information of a job. For example job_start_date,job_end_date,payment_received_date. It will be nullable initially. As soon as the job starts admin will update job_start_date and job_end_date column. So I cannot set current date and time as default to those field. The problem is, while updating the job_start_date field, if laravel finds that the job_start_date column is null, it throws an error. Call to a member function format() on a non-object. Even though I have declared protected $dates = ['job_start_date','job_end_date','payment_received_date']; in my model. Any suggestion to overcome this problem?
example code:
<!-- job start date -->
<div class="form-group {{ $errors->has('job_start_date') ? 'has-error' : '' }}">
<label class="col-md-4 control-label" for="job_start_date">Job Start Date</label>
<div class="col-md-4">
<input id="job_start_date" name="job_start_date" type="date" value="{{ $job_account_detail->job_start_date->format('Y-m-d') }}" class="form-control input-md">
{!! $errors->first('job_start_date','<span class="help-block">:message</span>') !!}
</div>
</div>
You can check for null value in your view to avoid the error
<input id="job_start_date" name="job_start_date" type="date" value="{{ !is_null($job_account_detail->job_start_date) ? $job_account_detail->job_start_date->format('Y-m-d') : "" }}" class="form-control input-md">
I would add a method in the model to handle validation and formatting to make my life easier:
// in your model
public function getJobStartDateInDateStringFormat() {
return !is_null($this->job_start_date)
? $this->job_start_date->format("Y-m-d")
: "";
}
Then in your view you can just call the method:
<input id="job_start_date" name="job_start_date" type="date" value="{{ $job_account_detail->getJobStartDateInDateStringFormat() }}" class="form-control input-md">

Categories