Can't use dd() and var_dump inside function in Laravel + Vue project?

1.2k Views Asked by At

I am trying to correct an error in a search input in Vue + Laravel project.

For some reason, when more than one word is used in the search input the product is not displayed, but the projects that feature the products are.

For example: I have 4 types of desks: Desk Green, Desk, Desk Tall and Desk Grey. If I search for "Desk" the products will all be displayed and the interior design projects in which these desks are featured are also displayed. If I search for "Desk Green" specifically, the function returns an empty array of products, however the interior design projects that feature "Desk Green" are shown normally, even though the same functions are being used.

I am using two main functions which I will paste below. One is inside the Vue component and it´s called as soon as the component is mounted. This function uses axios to post data to the other function, which is in the controller. It seems that I am not able to use var_dump() or dd() in the controller function and I don´t know why. I am a begginer trying to work in a rather complex project, so it is a little difficult for me.

This is the funcion inside the component:

search(){
            let search = window.location.href.split('=');

            this.searchValue = search[1].replace('%20', ' ');

            search = search[1].replace('%20', '+').toLowerCase()            

            if(localStorage.getItem('userFormData')){
                let form = JSON.parse(localStorage.getItem('userFormData'));

                if(form.email){

                    this.emailClient = form.email;
                }
            }
            
            axios.post('search', {search : search, emailClient : this.emailClient})
            .then(response => {

                console.log(JSON.parse(JSON.stringify(response.data.products)))

                this.products      = JSON.parse(JSON.stringify(response.data.products))
                this.projects      = JSON.parse(JSON.stringify(response.data.projects))
                this.ebooks        = JSON.parse(JSON.stringify(response.data.ebooks))
                this.countProducts = response.data.countProducts
                this.countProjects = response.data.countProjects
                this.countEbooks   = response.data.countEbooks

            }).catch(error => {
                console.log(error)
            })
        }

This is the function in the controller:

public function searchFunction(Request $request)
    {
        try {

            $lang   = App::getLocale();
            $lang   = new LangMiddleware;
            $locale = $lang->getLocale();

            $search = $request->search;
            $search = str_replace('+', ' ', $search);

            if(Translations::where('group','LIKE','product%')->where('key','LIKE','%descSeo')->where('value', 'like', '%'.$search.'%')->where('locale', $locale)->exists()){
                $prodsArrayTrans = Translations::where('group','LIKE','product%')->where('key','LIKE','%descSeo')->where('value', 'like', '%'.$search.'%')->where('locale', $locale)->groupBy('group')->get();

                foreach($prodsArrayTrans as $key => $trans){
                    $product = explode('_', $trans->group);

                        $products = Products::with('categories')->where('id', $product[1])->where('visible', 1)->get();                    

                            $dataProducts[$key] = $products->map(function ($products) use ($locale) {
                                if($products->id_category != 5)
                                {
                                    $category = 'heritage';
                                }
                                else
                                {
                                    $category = 'graphic-lamps';
                                }

                                return [
                                    'id'          => $products->id,
                                    'name'        => $products->name,
                                    'nameSeo'     => trans($products->title_seo),
                                    'video'       => $products->video_url,
                                    'category'    => $products->categories->category,
                                    'description' => $products->description,
                                    'weight'      => $products->weight,
                                    'model'       => $products->model,
                                    'dimensions'  => $products->dimensions,
                                    'priceWW'     => $products->price_ww,
                                    'priceUSA'    => $products->price_usa,
                                    'titleSeo'    => $products->title_seo,
                                    'descSeo'     => trans($products->desc_seo),
                                    'keywordsSeo' => trans($products->keywords_seo),
                                    'bulbs'       => $products->bulbs,
                                    'certificate' => (bool)$products->ul_certificate,
                                    'new'         => (bool)$products->new,
                                    'contract'    => (bool)$products->contract,
                                    'onlineFlag'  => (bool)$products->online_flag,
                                    'onlineLink'  => $products->online_link,
                                    'image'       => $products->thumbnail,
                                    'imageAlt'    => $products->thumbnail_alt,
                                    'link'        => strtolower($category . '/' . $products->categories->category . '/' . str_replace(' ', '-', $products->name))
                                ];
                            });  

                }
            }else{
                $array = explode(' ', $search);

                $translationsArray = [];

                foreach ($array as $key => $value) {
                    if($value != ''){
                        if(Translations::where('value', 'like', '%'.$value.'%')->exists())
                        $translationsArray = Translations::where('value', 'like', '%'.$value.'%')->groupBy('group')->get();
                    }
                }
           
                if(count($translationsArray) > 0)
                {
                    foreach ($translationsArray as $key => $trans) {
                        if(strpos($trans->group, 'product') !== false)
                        {

                            $product = explode('_', $trans->group);

                            $products = Products::with('categories')->where('id', $product[1])->where('visible', 1)->get();


                                $dataProducts[$key] = $products->map(function ($products) use ($locale) {
                                    if($products->id_category != 5)
                                    {
                                        $category = 'heritage';
                                    }
                                    else
                                    {
                                        $category = 'graphic-lamps';
                                    }

                                    return [
                                        'id'          => $products->id,
                                        'name'        => $products->name,
                                        'nameSeo'     => trans($products->title_seo),
                                        'video'       => $products->video_url,
                                        'category'    => $products->categories->category,
                                        'description' => $products->description,
                                        'weight'      => $products->weight,
                                        'model'       => $products->model,
                                        'dimensions'  => $products->dimensions,
                                        'priceWW'     => $products->price_ww,
                                        'priceUSA'    => $products->price_usa,
                                        'titleSeo'    => $products->title_seo,
                                        'descSeo'     => trans($products->desc_seo),
                                        'keywordsSeo' => trans($products->keywords_seo),
                                        'bulbs'       => $products->bulbs,
                                        'certificate' => (bool)$products->ul_certificate,
                                        'new'         => (bool)$products->new,
                                        'contract'    => (bool)$products->contract,
                                        'onlineFlag'  => (bool)$products->online_flag,
                                        'onlineLink'  => $products->online_link,
                                        'image'       => $products->thumbnail,
                                        'imageAlt'    => $products->thumbnail_alt,
                                        'link'        => strtolower($category . '/' . $products->categories->category . '/' . str_replace(' ', '-', $products->name))
                                    ];
                                });  



                        }else{
                            $dataProducts = [];
                        }
                    }
                }else{

                    $dataProducts = [];
                }
            }

            if(Translations::where('group','LIKE','project%')->where('key','LIKE','%details')->where('value', 'like', '%'.$search.'%')->where('locale', $locale)->exists()){
                $projectsArrayTrans = Translations::where('group','LIKE','project%')->where('key','LIKE','%details')->where('value', 'like', '%'.$search.'%')->where('locale', $locale)->groupBy('group')->get();
                foreach($projectsArrayTrans as $key => $trans){
                    $project = explode('_', $trans->group);

                        $projects = Projects::where('id', $project[1])->get();

                        $dataProjects[$key] = $projects->map(function ($projects) {
                            return [
                                'id'       => $projects->id,
                                'name'     => $projects->name,
                                'image'    => $projects->cover,
                                'imageAlt' => trans($projects->cover_alt),
                                'preview'  => trans($projects->details_preview),
                                'title'    => trans($projects->name),
                                'subTitle' => trans($projects->subTitle)
                            ];
                        });
                }
            }else{
                $array = explode(' ', $search);

                $translationsArray = [];

                foreach ($array as $key => $value) {
                    if($value != ''){
                        if(Translations::where('value', 'like', '%'.$value.'%')->exists())
                        $translationsArray = Translations::where('value', 'like', '%'.$value.'%')->groupBy('group')->get();
                    }
                }         

                if(count($translationsArray) > 0)
                {
                    foreach ($translationsArray as $key => $trans) {
                        if(strpos($trans->group, 'project') !== false)
                        {
                            $project = explode('_', $trans->group);

                            $projects = Projects::where('id', $project[1])->get();

                            $dataProjects[$key] = $projects->map(function ($projects) {
                                return [
                                    'id'       => $projects->id,
                                    'name'     => $projects->name,
                                    'image'    => $projects->cover,
                                    'imageAlt' => trans($projects->cover_alt),
                                    'preview'  => trans($projects->details_preview),
                                    'title'    => trans($projects->name),
                                    'subTitle' => trans($projects->subTitle)
                                ];
                            });



                        }else{
                            $dataProjects = [];
                        }
                    }
                }else{

                    $dataProjects = [];
                }
            }


            if(Translations::where('group','LIKE','ebook%')->where('key','LIKE','%details')->where('value', 'like', '%'.$search.'%')->where('locale', $locale)->exists()){
                $ebooksArrayTrans = Translations::where('group','LIKE','ebook%')->where('key','LIKE','%details')->where('value', 'like', '%'.$search.'%')->where('locale', $locale)->groupBy('group')->get();
                foreach($ebooksArrayTrans as $key => $trans){
                    $ebook = explode('_', $trans->group);


                        $ebooks = Ebooks::where('id', $ebook[1])->get();


                        $dataEbooks[$key] = $ebooks->map(function($ebooks)use ($locale){
                            return [
                                'id'       => $ebooks->id,
                                'name'     => trans($ebooks->name),
                                'details'  => trans($ebooks->details),
                                'image'    => trans($ebooks->img),
                                'imageAlt' => trans($ebooks->alt)
                            ];
                        });
                }
            }else{
                $array = explode(' ', $search);

                $translationsArray = [];

                foreach ($array as $key => $value) {
                    if($value != ''){
                        if(Translations::where('value', 'like', '%'.$value.'%')->exists())
                        $translationsArray = Translations::where('value', 'like', '%'.$value.'%')->groupBy('group')->get();
                    }
                }
           
                if(count($translationsArray) > 0)
                {
                    foreach ($translationsArray as $key => $trans) {


                        if(strpos($trans->group, 'ebook') !== false)
                        {

                            $ebook = explode('_', $trans->group);



                            $ebooks = Ebooks::where('id', $ebook[1])->get();


                            $dataEbooks[$key] = $ebooks->map(function($ebooks)use ($locale){
                                return [
                                    'id'       => $ebooks->id,
                                    'name'     => trans($ebooks->name),
                                    'details'  => trans($ebooks->details),
                                    'image'    => trans($ebooks->img),
                                    'imageAlt' => trans($ebooks->alt)
                                ];
                            });




                        }else{
                            $dataEbooks = [];
                        }
                    }
                }else{
                    $dataEbooks = [];
                }
                
            }       

      
            $countProducts = count($dataProducts);
            $countProjects = count($dataProjects);
            $countEbooks   = count($dataEbooks);


            if($countProducts > 0){
                $flagProducts = 1;
            }else{
                $flagProducts = 0;
            }

            if($countProjects > 0){
                $flagProjects = 1;
            }else{
                $flagProjects = 0;
            }

            if($countEbooks > 0){
                $flagEbooks = 1;
            }else{
                $flagEbooks = 0;
            }


            $keywords                   = new SearchKeyword;
            $keywords->email            = $request->emailClient;
            $keywords->locale           = $locale;
            $keywords->keyword          = $search;
            $keywords->products_results = $flagProducts;
            $keywords->projects_results = $flagProjects;
            $keywords->ebooks_results   = $flagEbooks;
            $keywords->save();


            return response()->json([
                                        'products'      => $dataProducts,
                                        'projects'      => $dataProjects,
                                        'ebooks'        => $dataEbooks,
                                        'countProducts' => $countProducts,
                                        'countProjects' => $countProjects,
                                        'countEbooks'   => $countEbooks,
                                    ]);
            
        } catch (Exception $e) {
            return response()->json($e->getMessage(), 500);
        }
    }
0

There are 0 best solutions below