希望不要重复。
我在第一个表(用户)中有两个表,列(uid和tokenn)是注册用户的ID。 当来自外部网站的访客向(uid)发送消息时,函数将消息发布到(posts table) strong> 和列 (uid) 在帖子表中变为 (to_id)。
我想获取(to_id)的(token),即users表中的(uid) strong> 以便让 Firebase 将通知推送到该令牌。
用户表:
id | uid | 姓名 | 电子邮件 | 密码 | 令牌 |
---|---|---|---|---|---|
5 | 6 | o | o | o | 12313 |
帖子表:
id | pid | from_id | to_id | 反馈 |
---|---|---|---|---|
5 | 6 | 12313 |
我的postsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\post;
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('API_ACCESS_KEY','xxx');
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$token = User::where('uid', $to_id)->first()->tokenn;
$notification = [
'title' =>'xxx',
'body' => 'xxx',
'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);
//
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; //but it returns the logged in and the sender user's token which is wrong.
$token = User::find($to_id)->tokenn;
$token = User::where('uid', $to_id)->first()->tokenn;
//these both are not working too
当 GUEST 向任何人发送消息时,我需要检索收件人的令牌。
这是我的网站:https://www.secreta.me/omar
希望我说得很清楚。
答案 0 :(得分:0)
您必须在模型目录中创建 User.php
这个类默认在你安装 laravel 的时候创建,但是如果你无意中删除了它,你可以通过以下方式创建它:
php artisan make:model User
并且在您的控制器中,您必须使用此模型而不是 App\Http\Controllers\User
。
所以换句话说,你必须使用
App\User
在 Laravel 7 及以下版本中,或在 Laravel 8 中使用 App\Models\User
在您的控制器中,如下所示。
use App\User; // if your laravel version is 7 or below
use App\Models\User; // if your laravel version is 8 or higher