Как проверить, является ли переменная пустой, и вернуть одно и то же флэш-сообщение с несколькими переменными

Кто-нибудь знает, как я могу сделать это короче?

if (empty($location->file))
return back()->with('danger', 'the image field is required');

if (empty($location->culture_file))
return back()->with('danger', 'the image field is required');

if (empty($location->history_file))
return back()->with('danger', 'the image field is required');

if (empty($location->gastronomy_file))
return back()->with('danger', 'the image field is required');

2

Решение

Вы должны попробовать это:

if (empty($location->file) || empty($location->culture_file) || empty($location->history_file) || empty($location->gastronomy_file)){
return back()->with('danger', 'the image field is required');
}
4

Другие решения

Я думаю, что вы хотите вернуть только один раз, поскольку это выражение ИЛИ (||) может быть использовано.

if (empty($location->file) || empty($location->culture_file) || empty($location->history_file) || empty($location->gastronomy_file))
return back()->with('danger', 'the image field is required');
0

if ($request->hasFile('image')) {
dd('write code here');
}

//or

if ($request->file('image')->isValid()) {
dd('write code here');
}

//There are few ways:
if (!empty($location->file))
if (!products->isEmpty())
if (count($products) > 0)
if ($products->count() > 0)

//flash messages in the session
Session::flash('message', 'This is a message!');
Session::flash('alert-class', 'alert-danger');

@if(Session::has('message'))
p class="alert {{ Session::get('alert-class', 'alert-info') }}"{{ Session::get('message') }}
@endif

Laravel Как проверить, является ли переменная пустой с Внедрить Flash-сообщения с примером

0

foreach (['file','culture_file','history_file','gastronomy_file'] as $file) {
if (empty($location->$file))
return back()->with('danger', 'the image field ['.$file.'] is required');
}
0

Попробуй это

if (!empty($location) && ($location->file || $location->culture_file || $location->history_file || $location->gastronomy_file)) {
return back()->with('danger', 'the image field is required');
}
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector