I want to add a record to my DB using Laravel but after submitting a form I get a blank screen and no logs. Here's my code:
public function store(Request $request)
{
foreach(Auth::user()->test as $data) {
if($request->name == $data->name) {
return back()->with('wrong', trans('settings.isset.name'));
}
else {
$this->validate($request, [
'name' => 'required',
]);
$name = Name::store($request);
return back()->with('message', trans('settings.add.name'));
}
}
}
And there is, of course, a normal working form. Before when I didn't have foreach
it was working, but not it isn't.
I think that this can be caused when Auth::user()->test
is empty. Therefore code inside the foreach
loop is not executed and nothing is returned.
You could try putting the return
statement to the end of function.
I hope this will do the job
public function store(Request $request)
{
$error = false;
$errors = [];
foreach(Auth::user()->test as $data) {
if($request->name == $data->name) {
$error = true;
$errors['wrong'] = trans('settings.isset.name');
break;
}
else {
$this->validate($request, [
'name' => 'required',
]);
$name = Name::store($request);
}
}
if($error)
return back()->with($errors);
return back()->with('message', trans('settings.add.name'));
}
Use this below .htaccess Options +ExecCGI addhandler x-httpd-php5-cgi .php Options -MultiViews DirectoryIndex index.php <IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On RewriteBase / # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f #RewriteRule ^ index.php [L] RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule>
Options +ExecCGI addhandler x-httpd-php5-cgi .php Options -MultiViews DirectoryIndex index.php <IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On RewriteBase / # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f #RewriteRule ^ index.php [L] RewriteRule ^(.*)$ index.php?/$1 [L] </IfModule>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.