phplaravellaravel-5.8

Argument 1 passed, must be of the type array, string given


I'm working with Laravel 5.8 and I made a page to see all uploaded files which are stored at the media_library table.

And I get all the data like this:

public function __construct(MediaLibraryRepository $mediaLibraryRepoObject)
    {
        $this->mediaLibraryRepoObject = $mediaLibraryRepoObject;
    }

    public function index()
    {
        $res = $this->mediaLibraryRepoObject->select();
        $media = $res["media"];
        $files = $res["files"];
        $extensions = [
            'jpg', 'png', 'bmp', 'gif', 'jfif', 'mp4', 'mkv', 'flv', 'mov', 'avi', 'wmv', 'mp3', 'pdf', 'doc', 'docx', 'pptx', 'xls', 'xlsx', 'zip', 'rar'
        ];
        return view('admin.mediaLibrary.index', compact('media','files', 'extensions'));
    }

As you can see I have called a repository here and at the index() method I called the select() method of that repo which goes here:

public function select()
    {
        $media = MediaLibrary::where('med_dimension', 'full');

        if (request()->has('mediaType') && request('mediaType') != "")
            $media = $media->whereIn('med_extension', request('mediaType'));
        
        if (request()->has('fileName') && request('fileName') != "")
            $media = $media->whereIn('med_name', request('fileName'));

        $media = $media->latest()->paginate(15);

        $files = [];
        foreach ($media as $key=>$value) {
            $files[$key]['med_name'] = $value->med_name;
            $files[$key]['med_group'] = $value->med_group;
            $files[$key]['med_size'] = $value->med_size;
            $files[$key]['med_extension'] = $value->med_extension;

            foreach (MediaLibrary::where('med_group', $value->med_group)->get() as $val) {
                $files[$key]['path'][$val->med_dimension] = $val->med_path;
            }
        }
        return ["files"=>(object) $files,"media"=>$media];
    }

So I needed to add a search form at the Blade to search the file names, therefore I tried this code:

if (request()->has('fileName') && request('fileName') != "")
            $media = $media->whereIn('med_name', request('fileName'));

But it gives me this error:

Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, string given, called in

So whats going wrong here? How can I fix this issue?

And here is my form:

<div class="row">
    <div class="col-md-4" style="padding-right: 0px !important;">
        <label for="mediaType">File type:</label>
        <select type="text" class="form-control select2" name="mediaType[]" multiple>
            <option value="">Select</option>
                @foreach($extensions as $ext)
                    <option value="{{ $ext }}"
                        @if(request()->has('mediaType') && request('mediaType') != "")
                            @foreach(request()->query('mediaType') as $f)
                                @if($ext == $f)
                                    selected
                                @endif
                            @endforeach
                        @endif
                    >
                    {{ $ext }}
                    </option>
                @endforeach
        </select>
    </div>
    <div class="col-md-4" style="padding-right: 0px !important;">
        <label for="mediaType">File name:</label>
            <input type="text" class="form-control" name="fileName" value="{{ request()->query('fileName') }}">
        </div>
        <div class="col-md-4" style="padding-top: 25px; padding-left: 0px !important;">
            <button class="btn btn-primary float-left">Search</button>
        </div>
    </div>
</div>  

Solution

  • According to Laravel Docs

    The whereIn method verifies that a given column's value is contained within the given array:

    $users = DB::table('users')
                        ->whereIn('id', [1, 2, 3])
                        ->get();
    

    Change your code from:

    $media = $media->whereIn('med_name', request('fileName'));
    

    to:

    $media = $media->where('med_name', request('fileName'));