简体   繁体   English

Laravel 从具有相同列的两个表中检索数据

[英]Laravel Retrieving data from two tables with same columns

I hope this is not duplicated.我希望这不会重复。

I have two tables in first table (users) there is column (uid and tokenn) the ids of registered users.我在第一个表(用户)中有两个表,列(uid 和 tokenn)是注册用户的 id。 When a guest from outside website sends a message to (uid) , a function posts the message to (posts table) and column (uid) turns to (to_id) in posts table.当来自外部网站的访客(uid)发送消息时,函数将消息发布到(posts 表)并且(uid)列变为 posts 表中的(to_id)

I want to get the (tokeen) of the (to_id) which is (uid) in users table in order to let Firebase push notification to that token.我想获取用户表(uid)(to_id)(令牌) ,以便让 Firebase 将通知推送到该令牌。

users table:用户表:

id ID uid用户界面 name名称 email电子邮件 password密码 tokenn令牌
5 5 6 6 o o o 12313 12313

Posts table:帖子表:

id ID pid进程号 from_id from_id to_id to_id feedback回馈
5 5 6 6 12313 12313

My postsController.php我的帖子Controller.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'); } } }

I tried to retrieving the token of the recipient of the message as follows:我尝试检索消息收件人的令牌,如下所示:

 $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

I need to Retrieving the token of the recipient When A GUEST sends a message to any.当客人向任何人发送消息时,我需要检索收件人的令牌。

Here's my website : https://www.secreta.me/omar这是我的网站: https : //www.secreta.me/omar

Hope I'm clear.希望我很清楚。

You must Create User.php in your models directory您必须在模型目录中创建 User.php

this class by default created when you install laravel, but if you delete it unintentionally you can create it by:这个类默认在你安装 laravel 时创建,但如果你无意中删除它,你可以通过以下方式创建它:

php artisan make:model User

and in your Controller you must use this model instead of App\\Http\\Controllers\\User .并且在您的 Controller 中,您必须使用此模型而不是App\\Http\\Controllers\\User

so In other words you must use App\\User in laravel 7 and below or use App\\Models\\User in laravel 8 in your Controller like below.所以换句话说,你必须使用App\\User在laravel 7及以下或使用App\\Models\\User在你的控制器在laravel 8如下图所示。

use App\User; // if your laravel version is 7 or below

use App\Models\User; // if your laravel version is 8 or higher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM