1

I want to make a search function of user name and shift pattern id. Search by user shift pattern id works well but not for search by name. For more information, the name is inside the user table while shift pattern id is inside the user shift pattern table.

UserShiftPattern model:-

    public function uspname(){
      return $this->belongsTo(User::class, 'user_id');
    }

UserShiftPatternController:-

public function index(Request $req)
{
    $usershiftpattern = [];

    if($req->filled('searching')){
        $usershiftpattern = $this->fetch($req);
    }

    return view('admin.usershiftpattern', ['usps' => $usershiftpattern]);
}

public function fetch(Request $req)
{
    $fname = explode(",", str_replace(' ','',$req->inputName));
    $fshiftpatternid = explode(",", str_replace(' ','',$req->inputShiftPatternId));

    $usershiftpatternlist = UserShiftPattern::query();
    $userusp = UserShiftPattern::with('uspname')->get();

    if(isset($req->inputName)){
        $usershiftpatternlist = $userusp->whereIn('name',$fname);
    }
    
    if(isset($req->inputShiftPatternId)){
        $usershiftpatternlist = $usershiftpatternlist->whereIn('shift_pattern_id',$fshiftpatternid);
    }

    $usershiftpatternlist = $usershiftpatternlist->has('uspattern')->get();

    return $usershiftpatternlist;
}    

VIEW:-

<tbody>
                @foreach($usps as $uspslists => $uspslist)
                <tr>
                    <td>{{ $uspslist->uspattern->name }}</td>
                    <td>{{ $uspslist->shift_pattern_id }}</td>
                </tr>
                @endforeach
            </tbody>

Got an error:-

Symfony\Component\Debug\Exception\FatalThrowableError Call to a member function get() on bool

Please help thanks

Daisy
  • 527
  • 1
  • 5
  • 14
  • the data did not appear but got this tab error --> maximum execution time of 30 seconds exceeded – Daisy May 31 '21 at 04:28
  • Dont you think we need to make relationship insisde the usershiftpattern model? what do you think? – Daisy May 31 '21 at 04:36
  • i already get my query log, then @JohnLobo? How to read and understand the query? hmm :/ – Daisy May 31 '21 at 04:47
  • `name` in (?) [ShaharuddinBinMohamad] select * from `users` where `users`.`id` = ? limit 1 [55763] --> means? – Daisy May 31 '21 at 04:51

1 Answers1

2

I thin you need to modify your query

    $fshiftpatternid = explode(",", str_replace(' ','',$req->inputShiftPatternId));
    
    $usershiftpatternlist = UserShiftPattern::query();
       
    
  if(isset($req->inputName)&&!empty($req->inputName)){
    $usershiftpatternlist = $usershiftpatternlist->whereHas('uspname',function($q)use($req){
     $q->where('name','like',  "%{$req->inputName}%");
    });
}
    
    if(isset($req->inputShiftPatternId)){
        $usershiftpatternlist = $usershiftpatternlist->whereIn('shift_pattern_id',$fshiftpatternid);
    }
    
    $usershiftpatternlist = $usershiftpatternlist->has('uspattern')->get();
    
    return $usershiftpatternlist;
John Lobo
  • 14,355
  • 2
  • 10
  • 20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233118/discussion-between-daisy-and-john-lobo). – Daisy May 31 '21 at 06:15