0

Im currently learning to do sign in and sign up in laravel8 by referring some tutorials. But after sign up im getting this Undefined offset: 1 message.

Error line that showing is

$iteratee = trim($matches[1]);

This is my route file(web.php)

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;


Route::get('/signup',[
    'uses' =>'App\Http\Controllers\UserController@getSignup',
    'as'=>'user.signup'
   
]);

Route::post('/signup',[
    'uses' =>'App\Http\Controllers\UserController@getSignup',
    'as'=>'user.signup'
   
]);

And this is the signup page register part

  <header class="masthead"  >
            <div class="container">
                <div class="masthead-subheading"></div>
                <div class="masthead-heading text-uppercase"></div>
                <div class="container">

                    <div class="card bg-light">
                        <article class="card-body mx-auto" style="max-width: 400px;">
                            <h4 class="card-title mt-3 text-center">Create Account</h4>
                         

                            <p class="text-center">Get started with your free account</p>
                            @if(count($errors) > 0)
                            <div class="alert alert-danger">

                            @foreach($errors->all()as $error)
                            <p>{{$error}}</p>
                            @endforeach
                            </div>
                            @endif
                          
                            <form  action="{{ route('user.signup')}}">
                             
                        
                            <div class="form-group input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"> <i class="fa fa-envelope"></i> </span>
                                 </div>
                                <input name="email" class="form-control" placeholder="Email address" type="email">
                            </div> 
                          

                            <div class="form-group input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"> <i class="fa fa-lock"></i> </span>
                                </div>
                                <input name="password" placeholder="Create password" type="password">
                            </div>
                                                          
                            <div class="form-group">
                                <button type="submit" class="btn btn-primary btn-block"> Create Account  </button>
                                {{csrf_field()}}
                            </div> <!-- form-group// -->      
                            <p class="text-center">Have an account? <a href="login.html">Log In</a> </p>                                                                 
                        </form>
                        </article>
                        </div> <!-- card.// -->
                        
                        </div> 
                        <!--container end.//-->
                          
        </header>

UserController

class UserController extends Controller
{
    public function getSignup()
    {
         return view('user.signup');
 
    }
    public function postSignup(Request $request)
    {
         $this->validate($request,[
 
             'email'=> 'email|required|unique:users',
             'password'=>'required|min:4' 
 
         ]);
 
 
         $user= new User([
 
            'email'=>$request-> input('email'),
            'password'=> bcrypt($request-> input('password'))
 
         ]);
 
         $user-->save();
         return redirect()->route('shop.index');
 
     }
 
}

Please teach me a way to solve this.Thank you

  • 2
    `$matches`, which is an array (maybe? You don't show how you define it), doesn't have an index `1`, so you can't do `$matches[1]`. Is there something unclear about that error message? – Tim Lewis Nov 19 '20 at 16:43
  • 1
    Where you used this code `trim($matches[1]);`? – STA Nov 19 '20 at 16:43
  • I dont get it.never used something called {$matches} before.But it is the code line showing in laravel with Undefined offset: 1 error. Is it something wrong in my singin page coding? – tuneKawaii UwU Nov 19 '20 at 16:49
  • Did you write this code? What do you mean "never used something called `$matches` before"... – Tim Lewis Nov 19 '20 at 16:50
  • I think it is an inbuilt code that comes with laravel. Only thing i did is creating UserController,User model and signup.blade.php page. – tuneKawaii UwU Nov 19 '20 at 16:53
  • 1
    This looks wrong: `Route::post('/signup', ['uses' => 'App\Http\Controllers\UserController@getSignup', ...`, why would the `POST` route use `getSignup`? Should that be `postSignup`? Also, `$user-->save();` is a syntax error. You have 2 `--`, should be `$user->save();` – Tim Lewis Nov 19 '20 at 16:57
  • @TimLewis Thank you so much for showing me that.I fixed the things you said but the error message keeps coming :( – tuneKawaii UwU Nov 19 '20 at 17:08
  • 1
    `@foreach($errors->all()as $error)` is the cause of that error; there's no space between `()` and `as`, so the `@foreach()` directive fails (as shown in the answer below). This is a prime example of why indentation and spacing is so important when coding. Please try to be consistent and follow conventions; spaces before and after `=>` for arrays, no spaces around `->` for object access, etc etc. Consider using an IDE (VSCode, SublimeText, etc.) that has a linter/syntax highlighter available to avoid these edge issues. – Tim Lewis Nov 19 '20 at 17:19
  • Please post the entire stack trace of the error. – Hafez Divandari Nov 19 '20 at 17:23
  • @TimLewis corrected that as you guys said but error is same.Is something wrong how i used the loop there – tuneKawaii UwU Nov 19 '20 at 17:38
  • Check all of your `@` directives; it might be in a different one, in a view that is extended by your current view. You're not really showing us enough of your code for us to say that the `@foreach()` is the **only** one that could have this issue. – Tim Lewis Nov 19 '20 at 17:45

1 Answers1

3

You should add a space in the @foreach between the pieces:

@foreach($errors->all()as $error)

Should be:

@foreach($errors->all() as $error)

The Blade compiler parses the expression passed to this directive and does a preg_match on it to get the pieces of the expression as this directive does more than just creating a foreach loop. Here is the match on the expression:

preg_match('/\( *(.*) +as *(.*)\)$/is', $expression, $matches);

$iteratee = trim($matches[1]);
lagbox
  • 48,571
  • 8
  • 72
  • 83
  • 2
    the only place in the entire code base where `$iteratee = trim($matches[1]);` exists is in the Blade Compiler `@compileForelse` and `@compileForeach` (which comes from the `CompilesLoops` trait) – lagbox Nov 19 '20 at 17:22
  • Corrected the @foreach as you said but error keeps coming.Is it something wrong how i used the loop in my blade file – tuneKawaii UwU Nov 19 '20 at 17:33
  • 1
    @tuneKawaiiUwU The view could be cached, try clearing it out. – miken32 Nov 19 '20 at 18:47