Search code examples
phplaravellaravel-5laravel-5.3

How to use different variable names in view laravel


i have a form. If a user creates fills the form and submits it is meant to reply back 'Account created successfully. An activation code has been sent to your mail.' but if the user registers with an account number that is already taken it should redirect you saying 'Account number already exists. Login.' This is the code in the controller:

protected function storuser()
{
$account_number = $request['accountnumber'];
$user = DB::table('users')->where('account_number', $account_number)->first();
if ($user==NULL) {
return view('login', ['message'=>'Account created successfully. An activation code has been sent to your mail.'])
}else{
return view('login', ['message'=>'Account number already exists. Login.']);
}

}

this is the html code:

<!DOCTYPE html>
<html>
<head>
<title>this is a title</title>
</head>
<body>
<form action="login" method="GET">
@if($message)
<div class="alert alert-info">
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
<ul>
<li>{{$message}}</li>
</ul>
</div>
@endif
<input type="text" name="accountnumber">
<input type="submit" value="submit">
</form>
</body>

whenever i hit the login route which shows me the page, it tells me Undefined variable: message. This is frustrating and holding me back. However when i remove the @if and @endif code the page shows, then when i fill the form i add the code back before submitting and it works perfectly but when ever i hit the route with the blade extension syntax there it tells me undefined variable message. Plsssss help


Solution

  • Try to change from

    @if($message)
    

    to

    @if(isset($message))
    

    You can also check if $message isn't empty or something like that.