正在从数据库(Laravel)中检索Auth用户数据



我是laravel的新手,我想了解更多关于从数据库检索用户信息的信息。

我有一个网站,你可以在那里发送一条消息from_id,to_id

当用户注册时,数据会插入表users中。

用户表包含:uid(from_id(、电子邮件、tokenn

帖子表包含:消息,from_id,to_id

我创建了一个php文件,该文件将为用户tokenn推送通知,并将以下代码添加到:\sup\app\Http\ControllerspostsController.php

这是我的发布功能,它通过推送通知功能将消息从用户发布到另一个用户。

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Apppost;
use Auth;
use Lang;
class postsController extends Controller
{
public function send_feedback(Request $request){
$this->validate($request,[
'feedback_image' => 'nullable|image|mimes:jpeg,png,jpg|max:3072',
'feedback_content' => 'required|max:500'
]);
$pid = rand(9,999999999)+time();
if (Auth::user()) {
$from_id = Auth::user()->uid;
}elseif (Auth::guest()) {
$from_id = 0;
}
$to_id = $request['hidden2'];
$feedback = $request['feedback_content'];
$image = $request->file('feedback_image');
$time = $request['hidden'];
if ($request->hasFile('feedback_image')) {
$img_ext = $image->getClientOriginalExtension();
$img_name = rand(9,9999999)+time()+rand(0,55555).".".$img_ext;
$img_new = $image->storeAs("fbImgs",$img_name);
}else{
$img_name = "";
}
$post = new post();
$post->pid = $pid;
$post->from_id = $from_id;
$post->to_id = $to_id;
$post->feedback = $feedback;
$post->image = $img_name;
$post->time = $time;
$post->save();

//
**define('xxxxx');
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$token='{{ Auth::user()->tokenn }}';
$notification = [
'title' =>'XXX',
'body' => 'XXXX',
'icon' =>'myIcon', 
'sound' => 'mySound'
];
$extraNotificationData = ["message" => $notification,"moredata" =>'dd'];
$fcmNotification = [
//'registration_ids' => $tokenList, //multple token array
'to'        => $token, //single token
'notification' => $notification,
'data' => $extraNotificationData
];
$headers = [
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
$result = curl_exec($ch);
curl_close($ch);
echo $result;**

//
return redirect()->back()->with('feedback_sent',Lang::get('trans.fb_sent'));
}
public function postPrivacy(Request $request){
$pid_var = $request['pid'];
$pid_ex = explode("_", $pid_var);
$pid = @$pid_ex[1];
if ($request['status'] == "true") {
$updatePrivacy = post::where('pid',$pid)->update(['privacy' => 1]);
}else{
$updatePrivacy = post::where('pid',$pid)->update(['privacy' => 0]);
}
return $pid;
}
public function deletePost(Request $request){
$checkID = post::where('pid',$request['pid'])->get()->count();
if ($checkID > 0) {
$allowed = post::where('pid',$request['pid'])->get();
foreach ($allowed as $getAllowed) {
$to_id = $getAllowed->to_id;
$from_id = $getAllowed->from_id;
}
if ($to_id == Auth::user()->uid || $from_id == Auth::user()->uid) {
$deleteFB = post::where('pid',$request['pid'])->delete();
return "done";
}else{
return Lang::get('trans.delPost_notAllowed');
}
}else{
return Lang::get('trans.err_somethingWrong');
}
}
}
?>

如您所见,$token="{{Auth::user((->tokenn}}">,Token应该获取当前用户的tokenn,即来自users表的接收方Token,但我不知道如何获取?我尝试如下声明新变量:$tokenn=Auth::user((->托肯,但它不起作用。我搞砸了。我搜索了整个项目中声明和检索标记列名的位置,但不知道在哪里。

编辑:

已更改$token="{{Auth::user((->tokenn}}";到$token=Auth::user((->托肯;如我所知。

它正在工作,但在本例中$token正在接收登录的用户令牌,并向发件人发送通知。我想为消息的接收者推送通知,比如$token=Auth::user((->to_id->托肯;

如果您想获得其他用户的令牌(to_id用户(,请尝试以下代码:

$token = User::find($to_id)->tokenn;

如果to_id是表中的列uid,则应该使用以下内容:

$token = User::where('uid', $to_id)->first()->tokenn;

此外,您还需要通过请求验证或使用findOrFail而不是findfirstOrFail而不是first来确保其他用户存在。

更新:

您可以使用Laravel原生IlluminateSupportFacadesHttp包装器来代替curl,这使得调试更加容易。我翻译了你的代码:

<?php
class postsController extends Controller
{
public function send_feedback(Request $request)
{
// codes before ....
$apiAccessKey = "some key";
$token = "something";
$url = "https://fcm.googleapis.com/fcm/send";
$notification = [
'title' => 'XXX',
'body' => 'XXXX',
'icon' => 'myIcon',
'sound' => 'mySound'
];
$extraNotificationData = [
"message" => $notification,
"moredata" => 'dd'
];
$fcmNotification = [
'to' => $token,
'notification' => $notification,
'data' => $extraNotificationData
];
$response = Http::acceptJson()
->withHeaders([
'Authorization' => 'key=' . $apiAccessKey
])->post(
$url,
$fcmNotification
);
dd($response->json());
}
}

最新更新