I'm trying to create a website that showcases all search results from various e-marketplace. I'm using javascript to show the result, and it works fine before I try to pass parameter through route. But once I did, the result stopped showing. I assume it's something to do with the Javascript. Here's the code:
<?php
use Sunra\PhpSimple\HtmlDomParser;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.tokopedia.com/search?st=product&q=hp 24mh');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
$response = curl_exec($ch);
curl_close($ch);
file_put_contents("text.html", $response);
echo $keyword;
// $url = 'https://www.tokopedia.com/search?st=product&q=24g2';
// $content = file_get_contents($url);
// $first_step = explode('<div class="css-1d1aa4" data-testid="imgSRPProdMain">' , $content );
// $second_step = explode("</div>" , $first_step[1] );
// echo $second_step[0];
?>
@extends('layout.app')
@section('title')
<title>안녕</title>
@endsection
<!-- Custom CSS -->
<link href="/css/card.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
@section('body')
<div class="container-fluid bg-trasparent my-4 p-3" style="position: relative;">
<div class="row py-5">
<div class="col-sm d-flex justify-content-center">
<h3>Search Result</h3>
</div>
</div>
<div class="row row-cols-1 row-cols-xs-2 row-cols-sm-2 row-cols-lg-4 g-3" id="carda">
</div>
</div>
@endsection
<script>
$(document).ready(function(){
var img;
$( "#carda" ).load( "text.html .css-1d1aa4 > img", function() {
var ancestor = document.getElementById('carda');
var child = ancestor.getElementsByTagName('*');
var i;
for (i = 0; i < 3; i ) {
child[0].remove();
}
$( " #carda > img " ).removeClass("success fade").addClass("productImage");
$( " .productImage ").wrap("<div class='col'><div class='card h-100 shadow-sm'></div></div>");
$( " .card ").append("<div class='card-body'><div class='clearfix mb-3'> <span class='float-start badge rounded-pill bg-success'>Rp. 1,000,000.00</span></div><h5 class='card-title'>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Veniam quidem eaque ut eveniet aut quis rerum. Asperiores accusamus harum ducimus velit odit ut. Saepe, iste optio laudantium sed aliquam sequi.</h5><div class='text-center my-4'> <a href='#' class='btn btn-warning'>View Product</a> </div></div>");
});
});
</script>
And here's the code for the web.php:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SearchController;
Route::get('/', function () {
return view('main');
});
Route::post('/search', [SearchController::class, 'search'])->name('search');
Route::get('/search/{keyword}', [SearchController::class, 'index'])->name('result');
Route::get('/result', function() {
return view('result');
});
Route::get('/test', [SearchController::class, 'test'])->name('test');
?>
And the SearchController code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function index($a)
{
return view('result', [
'keyword' => $a
]);
}
public function search(Request $request)
{
return redirect()->route('result', [$request->keyword]);
}
public function test(){
return view('result');
}
}
?>
Can anyone tell me what's wrong with this? Any help would be appreciated! Thanks :D
CodePudding user response:
You don't need to pass the single parameter in an array, if that array is not associative.
Do this instead:
return redirect()->route('result', $request->keyword);
CodePudding user response:
First check $request->keyword
contains a value
return redirect()->route('result', ['keyword' => $request->keyword]);
Then use
public function index($keyword)
{
return view('result', [
'keyword' => $keyword
]);
}