I have something strange going on with my Laravel/Vue application and cant figure it out. Within Laravels web.php, I have a simple route
Route::get('api/test', 'UploadController@test');
If I do route:list
I can see the correct route
| GET|HEAD | api/test | App\Http\Controllers\UploadController@test | web,auth |
I have simplified the controller for now so it is like so
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class UploadController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function test()
{
dd("IN");
}
}
And the Axios request is simply
<script>
export default {
data() {
return {
files: {}
}
},
methods: {
loadFiles() {
axios.get("api/test").then(({ data }) => (this.files = data));
}
},
created() {
this.loadFiles();
Fire.$on('AfterCreate',() => {
this.loadFiles();
});
}
}
</script>
This is where things get strange. Checking the console, I can see that the request URL is http://localhost:8000/api/test
which is correct. I am getting a 200 response, but it is not showing my output, just the whole HTML code for the page.
If I change the request to POST, then I see the output for this function.
Am I missing something here? Why would POST work but GET not?
Thanks
Update Managed to get it working, but not sure why. I have another page that displays users, and the GET request for this was working fine. I then wanted to check my api.php route file to make sure nothing in here was stopping the test. I noticed I still had my user one in here, so I deleted it, and then my user stopped working.
So in api.php, I have added
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::middleware('auth:api')->get('/upload', function (Request $request) {
return $request->user();
});
And now both work. Why is this?
Thanks
dd
and instead usereturn response()->json(['abc']);
– Patrick Schocke