Estou tentando emendar um erro em uma ingressão de pesquisa no projeto Vue + Laravel.
Por qualquer motivo, quando mais de uma termo é usada na ingressão de pesquisa, o resultado não é exibido, mas os projetos que apresentam os produtos são.
Por exemplo: Eu tenho 4 tipos de mesas: Mesa Virente, Mesa, Mesa Subida e Mesa Cinza. Se eu pesquisar por “Mesa”, todos os produtos serão exibidos e os projetos de design de interiores nos quais essas mesas são apresentadas também serão exibidos. Se eu pesquisar por “Mesa Virente” especificamente, a função retornará um array vazio de produtos, no entanto, os projetos de design de interiores que apresentam “Mesa Virente” são exibidos normalmente, mesmo que as mesmas funções estejam sendo usadas.
Estou usando duas funções principais que colarei inferior. Uma está dentro do componente Vue e é chamada logo que o componente é montado. Esta função usa axios para postar dados para a outra função, que está no controlador. Parece que não consigo usar var_dump() ou dd() na função do controlador e não sei por quê. Sou iniciante tentando trabalhar em um projeto bastante multíplice, logo é um pouco difícil para mim.
Esta é a função dentro do componente:
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)
})
}
Esta é a função no controlador:
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);
Sua visita nos ajuda a continuar oferecendo o melhor para você! $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 . "https://stackoverflow.com/" . $products->categories->category . "https://stackoverflow.com/" . 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,
Sua visita nos ajuda a continuar oferecendo o melhor para você! 'link' => strtolower($category . "https://stackoverflow.com/" . $products->categories->category . "https://stackoverflow.com/" . str_replace(' ', '-', $products->name))
];
});
}else{
$dataProducts = [];
}
}
}else{
$dataProducts = [];
}
}
Sua visita nos ajuda a continuar oferecendo o melhor para você! 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);
}
}
Tags:
Crédito: Nascente Original