-2

/* ErrorException in CoachController.php line 135: Undefined offset: 1 */

 else if(!empty($request->coach_name))
        {
           $name=explode(' ',$request->coach_name);
           $fname=$name[0];
           $mname=$name[1];
           $lname=$name[2];
           $coach=Tbl_Coach::select('tbl__coaches.*')
                ->where('tbl__coaches.coach_fname','=',$fname) 
                ->where('tbl__coaches.coach_mname','=',$mname)
                ->where('tbl__coaches.coach_lname','=',$lname)
                ->get();
            return view('sport_organization_user.coach_master.index',compact('coach'));
        }  
sangay
  • 45
  • 1
  • 8
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Kirk Beard Apr 27 '17 at 08:59
  • I am getting an error as 'ErrorException in CoachController.php line 135: Undefined offset: 1' when i enter 'Sangay' in input field and search. If i enter 'Sangay w Lhamo' in the input field then i can search using the above code. I don't know exactly what's wrong with my logic and how should i search using name in laravel 5.3?? – sangay Apr 27 '17 at 09:01
  • Then read the duplicate question. Your error is "undefined offset" and it's not the first time someone has asked it on Stack Overflow. If the error happens when you type in "Sangay" then it's pretty obvious why the error occurs. You need to read up on how to debug PHP, and learn how `explode` and arrays work. – Kirk Beard Apr 27 '17 at 09:03
  • I know that when I enter Sangay then array will have only Sangay at index 0 whereas, index 1 and index 2 will not have any data that's why the above error has occurred. Now, my question is how should i modify my code so, that it can be searchable when I enter Sangay only in the input field. Any help would be appreciated. In other words, how should i assign $mname and $lname according to the explode array results?? – sangay Apr 27 '17 at 09:16
  • That depends on how you want to design your system. What happens if someone enters the first name or last name only? You could use `->orWhere()` instead of `->where()` to compare the value entered against any of the name fields. – Kirk Beard Apr 27 '17 at 09:31
  • thank you I will try using in that ways. – sangay Apr 27 '17 at 09:41

1 Answers1

0

i think the array $name is matter of the error! make sure $name[1] $name[2] is exist!