I use Laravel 5.8, and I want simply use a default value for description if it's empty, and take summary.
// summary variable request is equal to "test"
$summary = $request->get('summary', null);
$request->get('description', $summary)
But, the field is present, empty, and description give me null instead of summary value. Summary value is "test".
To get information from a request you should use get(), input() or the name directly. There is no documentation for the get method on requests for recent Laravel versions. For the input method on Laravel 5.8 the documentation says
You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request
It says it only works if it is not present so I would do it as simple as this
$description = $request->description ? $request->description : $request->summary
It really depends on what you want to achieve after all this and how you want your data.
Possible Solutions
My first impressions were that the data may not be being sent through correctly but upon looking over your code again, I realized you are using the more deprecated function ->get('description').
Try using ->input('description) instead. I personally have never used ->get(), so maybe this could be the problem.
https://laravel.com/docs/5.8/requests
Related
i'm trying to use old helper with updateorcreate. I have a textarea for an about field. If the user has published something, the old text should be displayed when he wants to update. When I try to use the old helper {{old('about', $profile->about)}}
I get this error: Attempt to read property "about" on null. When I remove the old helper, write something then add the old helper, then it works. I understand that it is trying to retrieve something that isn't in the database. I tried making the about field nullable but it still doesn't work
Seems that $profile is null in your case. Maybe a create form instead of edit?
Try to :
{{old('about', $profile->about ?? '')}}
or
{{old('about', optional($profile->about))}}
So that it uses empty string when old does not exist and profile either.
I am using Laravel 5.4 and building API for Android and Iphone. I have set default value to null in phpmyadmin but my mobile developers do not want null values in response. There are many APIs so I am looking for short and perfect method.
I tried to find something in Laravel model that can help me. I tried following code as well.
Also tried setting default value in phpmyadmin but it does not accepts default: "As defined" is equal to empty value.
Third option could be to loop through laravel Model response and convert null to empty or zero based on data type but that will increase processing time and will increase our most of work as well.
public static function boot() { parent::boot();
self::creating(function ($my_model) {
$my_model->some_prop = $my_model->some_prop >= 42 ? $my_model->some_prop: defaultValue();
});}
Is there something in Laravel in Model section or somewhere else where I can set default value to empty fields when field does contain any value or is there any other solution or suggestion means how I can handle this problem?
I'm creating the WebSocket based application with Codeigniter. So, data is coming as a JSON string(not a POST Method). I want to use the Codeigniter's built-in form_validation method to validate the data received as JSON.
this is what I tried so far to set the variable so I can access it through $this->input->post('variable_name') but.
Try #1
$_POST['variable_name'] = !isset($data['variable_name']) ? NULL : $data['variable_name'];
Try #2
$variable_name = !isset($data['variable_name']) ? NULL : $data['variable_name'];
$this->form_validation->set_value("variable_name", $variable_name);
But when I use $this->input->post('variable_name')
it returns NULL.
Here is how I achieved it. Although, It doesn't seems to be an accurate solution but its simple and there is no work around.
I added only one line before execution of form validations
$_SERVER["REQUEST_METHOD"] = "POST";
and then set the variable like this
$_POST['post_var'] = "value";
Now set the validation rules and perform validation.
that's it :)
There's a method called set_data() that was introduced specifically to allow validation of non-POST inputs.
http://www.codeigniter.com/userguide3/libraries/form_validation.html#validating-an-array-other-than-post
In this comment in the PHP manual, a user suggested a class which would implement an error handler capable to implement primitive type hinting for PHP.
I understand how that class operates, however, I would like to know if it is possible to bring it to another level: instead of only checking the argument value to see if it matches the "typehint", would it be possible to replace the value with another one if it does match, and how?
I was thinking about something along the line of:
if(gettype($funcValue) == $typeHint) {
// here replace $funcValue, for example $funcValue = new Example($funcValue);
}
and then have the function call proceed with the new value?
I have searched the manual but found nothing which could allow me to "hook" in the parameters' values, but it could be there and I simply didn't find it.
There is a sample in the bottom of the official documentation http://kohanaframework.org/3.2/guide/kohana/security/validation
But obviously it wont work at the request as long as $post['username'] in View is used but the $post array is empty on first request.
So how do you restore the values in this case? Any general solution?
PS: yes, I do understand I could do isset($post['username']) ? $post['username'] : ''; but it is just annoying
I use the model to display the data in the form. That way the initial form value is the initial value in the model.
I then update the model data with POST data in the controller, if there are validation errors, the model data will contain the POST data. This means I don't have to put any conditional logic in the view, and I just do: Form::input('name', $model->name)
Here's a more detailed explanation of this approach: Kohana ORM and Validation, having problems
I use Arr::get function:
echo Form::input('name', Arr::get($post, 'name'))
I was just looking at the old documentation on Building and Validating a Form.
You can see from the sample code that first you need to initialize an array with the form field names as the key and set the value to an empty string. And if there's an error, fill in the values of each element. In the views, you can simply call Form::input() normally without any if statement or some sort.
I guess Kohana has already been built this way from the start. And it doesn't seem to change. You'll probably just need to do the same thing.