I am making a web app using Laravel 8.
All of my controller functions are working, apart from the following:
public function show(Request $id)
{
return response()->view('locations.show', compact('id'));
}
I have made sure that this controller is being reached, but nothing happens and the page does not redirect to the view I specified.
web.php:
Route::get('/getlocation', [App\Http\Controllers\LocationController::class, 'show']);
When I try to access the route manually, the page displays the view.
Please let me know if you need additional information. Thanks.
EDIT: As requested some additional code.
javascript
$("#getLocation" + spot.ID).click(function () {
$.get(getLocation, spot.ID);
});
- getLocation is a variable declared in a script tag in
app.blade.php
:
var getLocation = '{{action('App\Http\Controllers\LocationController@show')}}';
Things I've Tried
Different ways of returning the view such as
return view('locations.show', compact('id));
andreturn response()->view('locations.show', compact('id));
.Different ways of sending the variable to the view such as instead of
compact()
I tried['id'=>$id]
.Adding a variable to the route (
'/getlocation/{id}
) which required me changing the way the get request occurs to$.get('/getlocation/' + spot.ID, spot.ID);
EDIT2:
I have made a few changes, but the page is still not redirecting.
javascript:
$("#getLocation" + spot.ID).click(function () {
$.post("/getlocation", JSON.stringify(spot));
});
web.php:
Route::post('/getlocation', [App\Http\Controllers\LocationController::class, 'show']);
Route::get("/showspot", function (Request $location) {
return view('locations.show', compact('location'));
})->name("showLocation");
LocationController:
public function show(Request $location)
{
return redirect()->route('showLocation', json_decode($location));
}
I was getting errors regarding parameters being missing after clicking the button, so I had to use JSON.stringify()
and json_decode()
in my javascript file and controller respectively. Now I am getting no errors when clicking the button, but the page is still not redirecting to the view requested.
I did notice in my cmd window running php artisan serve
that when I click the button, requests are occurring: [Sun Dec 5 16:32:09 2021] 127.0.0.1:64795 Accepted [Sun Dec 5 16:32:10 2021] 127.0.0.1:64795 Closing
But again, not redirecting.
EDIT 3
More Details: What I'm trying to accomplish here is send the object from my javascript code to the locations.show view and redirect to that view. The javascript code shown is within a get request that fetches a JSON object and loops through it, displaying each object within the object.
$.each(jsonSpots, function (key, spot) {
if (spot.NAME.search(inputExpression) != -1 || spot.TYPE.search(inputExpression) != -1) {
results.push(spot);
$('h3').html("Total: " + resultsCount);
$('#result').append(
'<li id="spot' + spot.ID + '" class="spot list-group-item">' +
'<strong>Name: </strong>' + spot.NAME + '<br/>' +
'<strong>Address: </strong>' + spot.ADDRESS + '<br/>' +
'<button class="btn btn-info" id="getLocation' + spot.ID + '" >More Details</button>' +
'<br>'
);
$("#getLocation" + spot.ID).click(function () {
$.post(getLocation, spot, function (data) {
if (data.success) { // data.success is a boolean
window.location.href = data.url;
}
});
});
if (authenticated) {
$("#spot" + spot.ID).append(
'<button type="button" class="btn btn-dark" id="addSpot' + spot.ID + '" >Add To Map</button>'
);
$("#addSpot" + spot.ID).click(function () {
$.post(addSpot, spot);
drawPins("all");
});
}
$('#result').append('</li>');
}
});
Thanks to @Daedalus response, I have more of an understanding of why what I was originally attempting to accomplish was not working, and since been able to accomplish my goal, with a few alterations to his suggestion:
javascript:
NOTES:
- getLocation is a hardcoded action link within app.blade.php that points to my desired controller.
- Sending the entire object instead of just ID, because one of the issues I was having was that I am only adding a Location object to the database if the user decides they want to save it to their map. Therefore, when Laravel would look for the Location associated with that ID, it wouldn't find anything and the request would fail.
$("#getLocation" + spot.ID).click(function () {
$.post(getLocation, spot, function (data) {
)
if (data.success) {
window.location.href = data.url;
}
});
});
web.php:
NOTES:
- Sent a compacted Request to the controller instead of a Location object, for the reason stated above
Route::post('/getlocation', [App\Http\Controllers\LocationController::class, 'show']);
Route::get("/showspot", function (Request $location) {
return view('locations.show', compact('location'));
})->name("showLocation");
Controller:
NOTES:
- At first I tried to just send the $location, but I able to access all of the properties I needed except for the first one for some reason, which was NAME. So instead, I sent in an object with all the properties.
public function show(Request $location)
{
return [
'success' => true,
'url' => route('showLocation',
[
'NAME' => $location->NAME,
'LONGITUDE'=> $location->LONGITUDE,
'LATITUDE' => $location->LATITUDE,
'ADDRESS' => $location->ADDRESS,
'TYPE' => $location->TYPE,
'ID' => $location->ID
])
];
}
Now, all of the parameters are showing in the URL, which I would like to get around.
I realize that this is not a great way of accomplishing what I need, so any additional suggestions would be much obliged.