How to edit form with data from database tables in laravel - php

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>

Related

Summernote Editor in Laravel Blade Inputs by itself

helloo..
i had a quesiton plis..so i have this summer note editor in laravel php blade
<div class="form-group">
<label class="required" for="description">{{ trans('cruds.course.fields.description') }}</label>
<textarea class="form-control {{ $errors->has('description') ? 'is-invalid' : '' }}" name="description" id="summernote"
required>{{ old('description') }}</textarea>
#if ($errors->has('description'))
<div class="invalid-feedback">
{{ $errors->first('description') }}
</div>
#endif
<span class="help-block">{{ trans('cruds.course.fields.description_helper') }}</span>
</div>
and this JS for the summernote script
<script>
$('#summernote').summernote({
placeholder: 'Description...',
tabsize: 2,
height: 100
});
</script>
but the problem is when i am typing in the editor the first two words gets capitilised adn then the cursor move to the starts and write from there as well as stops me from inputting any further after few character are typed..may i know why this is happening as i got the same code in my other php view feature and it works there..
Any help would be appreciated..thanks

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 show error message below input field in blade file - Laravel

I am validating input using rules in laravel, Currently, I am using the below code to show errors in my view if validation fails.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
The problem with the above code is all the errors are coming in one place. How can I show errors below their respective inputs (Like Name, Email, Password, etc). And where can I define my custom error messages? Thanks
As of Laravel 5.8.13, you can use the #error blade directive. Just place your desired error markup below your input field:
#error('field-name')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
If there was an error validating field-name, the message related to the error will be displayed.
As for customising your validation error messages, check the Laravel documentation on Custom Error Messages for further information.
You can use like that for every field
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}"
name="name" value="{{ old('name') }}" autofocus>
#if ($errors->has('name'))
<span class="invalid feedback"role="alert">
<strong>{{ $errors->first('name') }}.</strong>
</span>
#endif
</div>
</div>

laravel 5.2 authentication with username

I'm trying to authenticate user with username, not with email.
I tried to add protected field $username to authenticate users class,but didn't work.
How can I fix it?
Hey if you use Laravel default Authedication then you have to change your login form like Email filed to Name field this
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">User Name</label>
<div class="col-md-6">
<input type="name" class="form-control" name="name" value="{{ old('name') }}">
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
and go AuthenticatesUsers Controller change your function like
public function loginUsername()
{
return property_exists($this, 'username') ? $this->username : 'name';
}

Categories