Sorry if I explained it poorly. I’m very new to both Laravel and React and it would be best displayed through these examples. Essentially, I’m sending a search to Laravel from React:
const { data, setData, post, processing, errors, reset } = useForm({
searchTerm: "",
searchBy: "",
});
const submit = (e) => {
e.preventDefault();
post(route("searchCompletion", [data.searchTerm, data.searchBy]));
};
The search term is the search term and the search method is either category or caption. I have a post route which works, but I’m trying to implement get so that I can re-retrieve the posts that are displayed (i.e. not get an error from http://localhost:8000/search/i?Caption=):
Route::post('/search/{searchTerm}', [SearchController::class, 'index'])->name('searchCompletion');
Route::get('/search/{searchTerm}', [SearchController::class, 'index'])->name('getSearchCompletion');
Also, here is my SearchController for reference:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsPost;
use InertiaInertia;
class SearchController extends Controller
{
public function index(Request $request) {
$postsFromSearch = Post::latest()->get();
$posts = [];
$userArr = [];
if ($request->searchBy == "Caption") {
foreach($postsFromSearch as &$post) {
if(str_contains($post->caption, $request->searchTerm)) {
array_push($posts, $post);
array_push($userArr, $post->user->load('profile'));
}
}
}
return Inertia::render('Posts/Index')->with(compact(['posts', 'userArr']));
}
}
How to re-render posts with url info.