0

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".

2
  • 1
    Could you 'dd($request)' and send the output here? In the $request you can see the parameters being sent through and we can check if its null while being passed in. Commented May 11, 2019 at 13:01
  • Laravel has a default middleware that converts empty strings to null, you might want to comment that out. github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/… Commented May 11, 2019 at 13:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct. You can also use a short ternary to simplify this, i.e $request->description ?: $default;.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.