Laravel array validation old values - validation

Using Laravel 5.4 I have a form with array inputs called: coffee_kg_bags
In my form:
<input type="text" name="kg_bags[{{$cof->id}}]" value="{{ old('kg_bags.' .$cof->id) }}">
In my controller I do all kinds of validations and I get an error, but I don't get the old value in the form.
I entered 2 values on fields:
$cof->id = 1 and $cof->id = 2
I get an error on the field with $cof->id = 1 but I don't get the old values.
In the update method in the controller:
dd(Input::get('kg_bags.2'));
Gives the value I entered
But:
dd(Input::old('kg_bags.2'));
Gives null.
dd(Input::old());
Gives an empty array
Why is that?

try
dd($request->old);
Instead
dd(Input::old());

With the help of #phpdroid I did this in my form:
<input type="text" name="kg_bags[{{$cof->id}}]" value="{{ Request::old('kg_bags.' . $cof->id) }}">

Related

Laravel collection querying

I have written a query to grab a collection of results, i've added a check to say if a record contains this field hide the record with the id of 2.
Controller method
$purchasedProducts = $user->products()->where('purchased', 1);
if ($user->products()->where('includes_bonus', 1)->first()) {
$purchasedProducts->where('benefits.id', '!=', 2);
}
$purchasedProducts->get();
Blade
in here i wrote out the foreach loop to be displayed within the blade.
#foreach($purchasedProducts as $product)
<div class="col-xl-6 p-0 p-xl-4 mb-5 mb-xl-0">
<form action="{{route('cancel.product', $product->id)}}" method="POST">
#csrf
error received
Trying to get property 'id' of non-object
<form action="<?php echo e(route('cancel.product', $product->id)); ?>" method="POST">
Can you see where i am going wrong?
You haven't assigned the results of the query to anything, you are just calling get() on the query you are building but are not doing anything with the returned results. Perhaps assign it to a variable:
$purchasedProducts = $purchasedProducts->get();

Laravel old() directive with conditional default value

I'm using Laravel 5.8, and have several input fields which of course has an old() directive on every value="" tag.
This is my example right now:
<input class="form-control input-md" name="contact_name" type="text" value="#if($edit){{ $ad->contact_name }}#else{{ old('contact_name')}}#endif">
I now that if I use this: {{ old('contact_name', "John")}}
The default value will be "John"
But I want to do a check if there is a user logged in and prefill that input with the User contact name.
My idea is something like this:
value="#if($edit){{ $ad->contact_name }}#else{{ old('contact_name', Auth::user()->name)}}#endif
And it works! But of course, it throws: Trying to get property 'name' when I get an incognito window.
So, how do I evaluate logged in users and prefill this?
You can use the optional helper:
{{ old('contact_name', optional(Auth::user())->name) }}

Cannot inject request paraemter into blade view file

I have the following URL:
https://example.com/?email=test#test.com
then I have the following blade template
<input value="{{Request::query('email') or old('email')}}">
But it's not displaying the email I passed in the get parameter into the input. Instead, it's displaying the value 1
Tried searching haven't found a solution that works.
That's because you have {{Request::query('email') or old('email')}} which is conditional. It will always return true or false.
Try any of these:
{{Request::query('email') || old('email')}}
or
{{Request::query('email') ? Request::query('email') : old('email')}}
You can try this:
<input value="{{ request()->email }}">

Pass old input after Form Request validation in laravel 5

if the validation fails it's not clear to me how i could pass the old input to fill again the form.
I mean, I know how to pass the data when using the Validator class and redirecting after fail with the method withInput(), but I'm trying to learn how to use Form Requests provided in laravel 5. Thanks
$username = Request::old('username');
or in view:
{{ old('username') }}
Read more: http://laravel.com/docs/5.0/requests#old-input
You can redirect with old data like below with validation error
return redirect()->back()->withErrors($validator)->withInput();
<input type="text" name="username" value="{{ old('username') }}">
you must also define withInput() for the http route when redirecting to a page for example
return back()->withInput();

Laravel 4 - Showing edit form with OLD data input as well as DB information

Im making a edit form for my app and i was wondering if someone could tell me how to get the data from the database into my text field.
I can locate the record i need to edit based on the users click, and i can display the information if i do the following:
value="{{ $letter->subject }}"
BUT, the problem im having is that when i run it through the validation and there is an error, it comes back with the database information instead of the OLD data.
So my questions is. Is there a way to serve up the database information first and then when it goes through the validatior, validate the information the user has edited?
Currently to validate the text field and bring the data back incase of error, im using
Input::old('subject')
Is there a parameter for that old bit that allows me to put in the DB data?
Cheers,
Hey you could validate and return ->withInput() and then in your actual form, check if there is Input::old() and display it, otherwise display from the db.
example:
<input type="text" name="subject"
value="{{ (Input::old('subject')) ? Input::old('subject') : $letter->subject }}">
Or you could go the other way and define the variable and do a regular if statement, instead of the ternary one! Up to you to decide what you want to use!
All you need is form model binding http://laravel.com/docs/html#form-model-binding:
{{ Form::model($letter, ['route' => ['letters.update', $letter->id], 'method' => 'put']) }}
// your fields like:
{{ Form::text('someName', null, ['class' => 'someHTMLclass' ...]) }}
// no default values like Input::old or $letter->something!
{{ Form::close() }}
This way you form will be populated by the $letter data (passed from the controller for example).
Now, if you have on your countroller:
// in case of invalid data
return Redirect::back()->withInput();
then on the redirect your form will be repopulated with input values first, not the original model data.
Make it more simple and clean
<input type="text" name="subject" value="{{ (Input::old('subject')) ?: $letter->subject }}">
I'm not sure for Laravel 4 but in Laravel 5, function old takes second param, default value if no old data in session.
Please check this answer Best practice to show old value

Resources