protonemedia / laravel-cross-eloquent-search

Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.
https://protone.media/blog/search-through-multiple-eloquent-models-with-our-latest-laravel-package
MIT License
1.09k stars 80 forks source link

"Contact" is not a valid backing value for enum App\Enums\ContactType #74

Closed lucacastelnuovo closed 1 year ago

lucacastelnuovo commented 1 year ago

Hi, superb package!

However, I have a problem adding multiple models to the search. I think it has something to do with my implementation. When I run the following code, I get this error message: "Contact" is not a valid backing value for enum App\Enums\ContactType.

<?php

namespace App\Http\Controllers;

use App\Models\Contact;
use App\Models\Person;
use App\Models\Zaak;
use ProtoneMedia\LaravelCrossEloquentSearch\Search;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class SearchController extends Controller
{
    /**
     * Show search page
     *
     * @param  Request  $request
     * @return Response
     */
    public function __invoke(Request $request)
    {
        $zaak = Zaak::query()
            ->when($request->query('lines'), fn ($query, $lines) => $query->whereIn('lines', $lines))
            ->when($request->query('states'), fn ($query, $states) => $query->whereIn('states', $states))
            ->when($request->query('user'), fn ($query, $user) => $query->where('user_id', $user))
            ->when($request->query('country_from'), fn ($query, $country_from) => $query->where('country_from', $country_from))
            ->when($request->query('country_via'), fn ($query, $country_via) => $query->where('country_via', $country_via))
            ->when($request->query('country_to'), fn ($query, $country_to) => $query->where('country_to', $country_to))
            ->when($request->query('codes'), fn ($query, $codes) => $query->whereIn('codes', $codes));

        $person = Person::query()
            ->when($request->query('created_from'), fn ($query, $created_from) => $query->whereDate('created_at', '>=', $created_from))
            ->when($request->query('created_to'), fn ($query, $created_to) => $query->whereDate('created_at', '<=', $created_to));

        $contact = Contact::query()
            ->when($request->query('created_from'), fn ($query, $created_from) => $query->whereDate('created_at', '>=', $created_from))
            ->when($request->query('created_to'), fn ($query, $created_to) => $query->whereDate('created_at', '<=', $created_to));

        $results = Search::new()
            ->add($zaak, 'description', 'reference')

            // TODO: https://github.com/protonemedia/laravel-cross-eloquent-search/issues/74
            ->add($person, ['name', 'organisation'])
            ->add($contact, ['summary', 'description'])

            ->includeModelType()
            ->beginWithWildcard()
            ->paginate(
                perPage: 10,
                page: $request->query('page', 1)
            )
            ->search($request->query('search', 'verylongunfindablestring'));

        $query = $request->query();
        $results->appends($query);

        return view('search.index', compact('results', 'query'));
    }
}

Can you spot what went wrong?

lucacastelnuovo commented 1 year ago

No error is generated if I comment out ->includeModelType().

lucacastelnuovo commented 1 year ago

Solved 🎉 , ->includeModelType('search_type') was the solution. The property type existed on my models and interfered with the package.