lundi 10 mai 2021

Laravel 5. Merge several Builders to paginate them with "paginate()"

I am trying to merge several builders to paginate search results of them, but this code returning me an error

My SearchController.php

public function index(Request $request)
    {
        $string = mb_strtolower($request->input('q'), "UTF-8");
        if ($string != "") {
            // $query = explode(" ", $string);
            $query = $string;
        } else {
            $query = null;
        }

        if ($query != null) {
            $news = $this->searchQuery(new News(), 'title', 'content', $query, false);
            $last = $this->searchQuery(new Last(), 'title', 'description', $query, false);
            $author = $this->searchQuery(new AuthorNews(), 'title', 'description', $query, false);
            $tv = $this->searchQuery(new TvBroadcast(), 'name', 'description', $query, false);
            $program = $this->searchQuery(new TvProgram(), 'name', 'description', $query, false);
            $project = $this->searchQuery(new SpecProject(), 'title', 'description', $query, false);
            $appointments = $this->searchQuery(new Appointment(), 'fio', 'position', $query, false);
            //Новости по авторам
            // $authors = $this->searchAuthor($query);
            // dd($authors);
            // 21520

            $item = $news->merge($last);
            $item = $item->merge($author);
            $item = $item->merge($tv);
            $item = $item->merge($program);
            $item = $item->merge($project);
            $item = $item->merge($appointments);

            $items = $item->paginate(21);

            $items_c = $items->total();
        } else {
            $items = null;
            $items_c = 0;
            $string = null;
        }

        $method = 'index';

        return view('front.search.index', array('items' => $items, 'count' => $items_c, 'q' => $string, 'method' => $method));
    }

    public function searchQuery($model, $attr1, $attr2, $query, $stat, $author = false)
    {
        $items = $model->with('nodes')->whereHas('nodes', function ($q) use ($query, $attr1, $attr2) {
            $q->where('language_id', '=', app()->getLocale())->where(function ($q) use ($query, $attr1, $attr2) {
                $q->where(\DB::raw('lower(' . $attr1 . ')'), 'like', '%' . $query . '%')
                    ->orWhere(\DB::raw('lower(' . $attr2 . ')'), 'like', '%' . $query . '%');
            });
        })->orderBy('created_at', 'desc');

        if ($stat == true) {
            $items = $items->where('stat', '=', 1);
        }

        if ($author) {
            $items = $items->with(['author', 'author.nodes'])->orWhereHas('author.nodes', function ($q) use ($query) {
                // foreach ($query as $elem) {
                //     $q->where(\DB::raw('lower(title)'), 'like', '%' . $elem . '%');
                // }
                $q->where(\DB::raw('lower(title)'), 'like', '%' . $query . '%');
            });
        }

        return $items;
    }

I have 7 Builders like this and I want to merge them

But when I am running my code I get an error like this

I also tried union() method and it doesn't work too



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3tIdXok
via IFTTT

Aucun commentaire:

Enregistrer un commentaire