Laravel Javascript-使用Javascript获取用户角色
我试图用Javascript而不是php来获取用户角色。使用PHP,您可以执行以下操作:Laravel Javascript-使用Javascript获取用户角色,javascript,laravel,axios,Javascript,Laravel,Axios,我试图用Javascript而不是php来获取用户角色。使用PHP,您可以执行以下操作: Auth::User()->role()->role_name 但如何做到这一点: axios.get('./api/users') .then(response => { this.users = response.data.users, th
Auth::User()->role()->role_name
但如何做到这一点:
axios.get('./api/users')
.then(response => {
this.users = response.data.users,
this.user = this.users[0].role.role_name
});
但那没用。我必须怎么做?Javascript
axios.get('/api/user')
.then(response => {
if (response.status == 200) {
this.user = response.data;
// this.user.role.role_name
}
})
.catch(error => {
//
});
控制器
public function index()
{
$user = auth()->user()->with('role');
return response()->json($user, Response::HTTP_OK); // 200 use Symfony\Component\HttpFoundation\Response;
}
您的web.php
Route::get('api/user', 'YourController@index');