【发布时间】:2020-05-05 09:56:46
【问题描述】:
我正在尝试通过在用户登录后编写统计信息来在 Laravel 中实现用户跟踪。即位置,时区等。我有点幸运地实现了它。
我通过在登录表单上附加事件侦听器来使用 ajax 提交按钮。表单提交后,调用ajax方法。此方法通过使用 api 调用获取用户的统计信息。然后,该 ajax 请求将该数据发送到StatsController@store,后者随后将该条目记录到 stats 表中。
这只能通过使用e.preventDefault(); 来实现,如果我不使用它,记录不会插入到数据库中,它会抛出一个错误,该错误在重定向到仪表板后迅速消失。
Ajax 方法驻留在布局文件中被调用的 js 文件中。
这是一个ajax方法:
function getCoords(){
return fetch('https://ipinfo.io/geo')
.then((response) => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
else{
return response.json();
}
})
.then((response) => {
let url = "/writeStats";
let token = document.querySelector("meta[name='csrf-token']").getAttribute("content");
let forma = document.getElementById("loginForm");
let formElements = {};
formElements.email = forma.elements[1].value;
formElements.password = forma.elements[2].value;
$.ajax({
url: url,
type: 'POST',
data: {_token: token , message: "bravo", stats: response, formElements: formElements},
dataType: 'JSON',
success: (response) => {
console.log("success");
console.log(response);
},
error: (response) => {
console.log("error");
console.log(response);
}
});
})
.catch((error) => {
console.error('There has been a problem with your fetch operation:', error);
});
}
window.addEventListener("click", (e) => {
if(e.target.id==="loginBtn"){
//e.preventDefault();
getCoords();
}
});
web.php:
Route::post('/writeStats','StatsController@store');
统计控制器:
public function store(Request $request)
{
if($request->ajax()){
$email = $request->formElements["email"];
$password = $request->formElements["password"];
$user = DB::table('users')->where("email", "=", $email)->first();
if(Hash::check($password, $user->password)) {
$stat = new Stats;
$stat->ip = $request->stats["ip"];
$stat->city = $request->stats["city"];
$stat->region = $request->stats["region"];
$stat->country = $request->stats["country"];
$stat->coords = $request->stats["loc"];
$stat->timezone = $request->stats["timezone"];
$stat->user_id = $user->id;
$stat->save();
$response = array(
"message" => "bravo",
"request" => $request->all(),
"stats" => $request->stats,
"user" => $user,
"stat" => $stat,
);
return response()->json($response);
}
}
}
php artisan --version 显示Laravel Framework 6.9.0
我的问题是:如何在用户登录后插入统计表?
编辑1:
我现在正在尝试一些不同的方法。我现在在 LoginController 中使用 authenticated 方法,而不是来自 StatsController 的一些自定义方法。
public function authenticated(Request $request)
{
$credentials = $request->only('email', 'password');
$email = $request->input("email");
$password = $request->input("password");
$user = DB::table('users')->where("email", "=", $email)->first();
if (Auth::attempt($credentials)) {
$stat = new Stats;
$stat->ip = $request->stats["ip"];
$stat->city = $request->stats["city"];
$stat->region = $request->stats["region"];
$stat->country = $request->stats["country"];
$stat->coords = $request->stats["loc"];
$stat->timezone = $request->stats["timezone"];
$stat->user_id = auth()->user()->id;
$stat->save();
$response = array(
"stat" => $request->all(),
);
return redirect()->intended('dashboard');
}
}
在 web.php 中:
Route::post('/login','Auth\LoginController@authenticated');
仍然没有运气。我只是不知道使用什么方法。感觉如此接近,就像我错过了一些小东西。
编辑2:
让我改一下问题:
如何将api调用响应发送到登录控制器,以便用户通过身份验证后,我可以将api响应数据插入另一个表(不是用户表)? 我可以:
- 以某种方式将 api 响应插入到身份验证请求中?
- 或者,如何在将记录写入表
stats的同时进行身份验证?
我尝试了这两种方法(参见:原始和编辑),但不确定我应该写什么......
【问题讨论】: