0
votes

I have this statement in my search function

if(!empty($project) && !empty($type)){
            $result = Project::where('id', 'LIKE', "%{$project}%")->with(['type', function ($query) use ($type) {
                $query->where('id', 'like', $type);
            }])->get();
}

it returns

mb_strpos() expects parameter 1 to be string, object given

Logic

  1. $project = project.id
  2. $type = type.id
  3. If $project and $ type both have values then get project where id = $project
  4. Then eager load type to that project where id = $type

is like we say types, where types.project_id = projects.id and where types.id = $type but with eager load instead of query.

any idea?

1

1 Answers

3
votes

You're getting this error because with() takes an associative array in order to constrain eager loads. try this

if(!empty($project) && !empty($type)){
            $result = Project::where('id', 'LIKE', "%{$project}%")->with(['type'=> function ($query) use ($type) {
                $query->where('id', 'like', $type);
            }])->get();
}