my controller look like
public function react(Request $request){
//echo "hi";
//return;
//return response()->json($request);
$this->validate($request, [
'postid' => 'required',
'react' => 'required'
]);
$reaction = Reaction::firstOrNew([
'post_id'=>$request->postid,
'user_id'=> Auth::id()
]);
$reaction->user_id = Auth::id();
$reaction->type = $request->react;
$reactType = "";
if ($request->react === "l"){$reactType = "liked";}
else if ($request->react === "d"){$reactType = "disliked";}
else if ($request->react === "h"){$reactType = "loved";}
else if ($request->react === "s"){$reactType = "Smiled";}
else{}
$post = Post::find($request->postid);
$postuser = $post->user->name;
if($post->reactions()->save($reaction)){
$data['message'] = Auth::user()->name.' '.$reactType. ' a Post from' . $postuser;
$data['type'] = 'reaction';
**$this->p->trigger('user-'.$post->user_id, 'new-post', $data);**
return response()->json([
'success' => true,
'message' => 'Reacted'
]);
}
else{
return response()->json([
'success' => false,
'message' => 'Error'
]);
}
AND I AM UNABLE TO LINK WITH PUSHER CHANNEL
But I keep getting an error
Undefined property: App\Http\Controllers\MyController::$p
What am I doing wrong? Would highly appreciate any possible help!
property $this->p (p) , You have not set the value
$this->p = Value ;
After setting, you can use the information.
$this->p->trigger('user-'.$post->user_id, 'new-post', $data);
In the code written above, $this->p has an null value
$this->null->trigger('user-'.$post->user_id, 'new-post', $data);
In fact, this is how you use it
Related
Client error: POST https://discordapp.com/api/oauth2/token resulted in a 429 Too Many Requests response: {"code": 0, "message": "You are being blocked from accessing our API temporarily due to exceeding our rate limits freque (truncated...)
I am getting this error when i am trying to login into my web apllication using discord,I know i am exceeding rate limits i want to increase my rate limit,after sometime it has started working again but i do not want this error again cause i have live customer on my site
<?php
namespace App\Http\Controllers\Auth;
include base_path('vendor/autoload.php');
use RestCord\DiscordClient;
use App\Http\Controllers\Controller;
use App\Models\User;
use Laravel\Socialite\Facades\Socialite;
use App\Notifications\SendCredentials;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class DiscordLoginController extends Controller
{
public function login(){
return Socialite::with('discord')->scopes(['guilds','guilds.join'])->redirect(); //redirect to generated 0auth link
}
public function callback(){
function getTotalMembers($last_id, $membrs_total)
{
$discord = new DiscordClient(['token' => getenv('DISCORD_BOT_TOKEN')]);
if ($last_id == 0) {
$members = $discord->guild->listGuildMembers(['guild.id' => 815031226699874334, 'limit' => 1000]);
} else {
$members = $discord->guild->listGuildMembers(['guild.id' => 815031226699874334, 'limit' => 1000, 'after' => $last_id]);
}
$end = end($members);
$last_id = $end->user->id;
$membrs_total = array_merge($membrs_total, $members);
if (count($members) <= 999) {
return $membrs_total;
} else {
return getTotalMembers($last_id, $membrs_total);
}
}
$membrs_total = array();
$membrs_totals = getTotalMembers(0, $membrs_total);
$current_guilds_ids=array();
foreach($membrs_totals as $member){
array_push($current_guilds_ids,$member->user->id);
}
$user = Socialite::driver('discord')->stateless()->user();
if(in_array($user->id,$current_guilds_ids))
{
$accessTokenResponseBody = $user->accessTokenResponseBody;
if (!Auth::user()) { //handle user login
$users = User::where(['discord_id' => $user->id])->first();
if ($users) {
if(in_array($users->discord_id,$current_guilds_ids)){
User::where(['id' => $users->id])->update([ 'is_binded' => true,'is_verified'=>true]);
}
Auth::login($users);
return redirect()->route('home');
} else {
$users = User::where('email', $user->email)->first();
if (!$users) {
$users = new User();
$users->name = $user->name;
$users->email = $user->email;
$password = uniqid();
$users->password = Hash::make($password);
$users->discord_id = $user->id;
$users->discord_email = $user->email;
$users->is_binded = 1;
if ($user->user['verified'] == 1) {
$users->is_verified = 1;
}
$users->save();
$notifyUsers = User::where('email', $users->email)->get();
foreach ($notifyUsers as $notifyUser) {
$notifyUser->notify(new SendCredentials($notifyUser, $password));
}
Auth::login($users);
return redirect()->route('home');
}
}
} else {
$user_id = auth()->user()->id;
$users = User::where(['discord_id' => $user->id])->count();
if ($users == 0) {
User::where(['id' => $user_id])->update(['discord_id' => $user->id, 'discord_email' => $user->email, 'is_binded' => true]);
return redirect()->route('home');
} else {
User::where(['id' => $user_id])->update(['discord_id' => $user->id, 'discord_email' => $user->email, 'is_verified' => false]);
$customer_status = User::where(['id' => $user_id])->value('is_customer');
$client = new \GuzzleHttp\Client();
$request = $client->get( //perform check
'https://discordapp.com/api/users/#me/guilds',
[
'headers' => [
'Authorization' => 'Bearer ' . $accessTokenResponseBody['access_token'],
],
]
);
$response = json_decode($request->getBody()->getContents(), true); //decode response
foreach ($response as $key => $value) { //traverse result
if ($value['id'] == 815031226699874334) {
User::where(['id' => $user_id])->update(['is_verified' => true]);
alert()->success('Nice One', 'You are in SMT Proxies Discord.');
if ($customer_status == true) {
$client = new Client([
'headers' => [
'Authorization' => 'Bot ' . getenv('DISCORD_BOT_TOKEN'),
"Content-Type" => "application/json"
]
]);
$client->put('https://discordapp.com/api/guilds/815031226699874334/members/' . $user->id . '/roles/815046198791503914', []);
alert()->success('Nice One', 'You are in SMT Proxies Discord & got customer role');
}
break;
}
}
return redirect()->route('home');
}
}
}
else{
alert()->warning('Sorry You have to join our server first');
return redirect()->route('login');
}
}
}
Please I have a laravel website which have been working fine then all of a sudden when a user uses a referral link to register, the page will reload and redirect back to the registration page. I tried it for few times and it works then I used the new referral link to register another new account and didn't work again ever since then. And its not showing any error message but rather bounce back to same page. I have checked the RegistrationController and all the code is still fine as same before. Please I really need this help.
Thanks in Advance
Below is my RegistrationControl
`public function getRegistrationPage(){
return view('auth.register');
}
public function storeUser(Request $request){
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|unique:users',
'phone' => 'required|min:10|unique:users',
'username' => 'required|min:5|unique:users|regex:/^\S*$/u',
'password' => 'required|string|min:6|confirmed',
'country' => 'required',
'state' => 'required',
]);
$email_code = strtoupper(Str::random(6));
$email_time = Carbon::parse()->addMinutes(5);
$phone_code = strtoupper(Str::random(6));
$phone_time = Carbon::parse()->addMinutes(5);
$upliner = Cookie::get('referral');
$uplinerUsername = Cookie::get('referral');
$email_verify = 1;
$phone_verify = 1;
$user = new User();
if($upliner == null){
$upliner = 0;
}
else{
$chkUserRef = User::where('reference', $upliner)->first();
if($chkUserRef == null){
return redirect('register');
}else{
$user_id = $chkUserRef->id;
$upliner = $user_id;
}
}
$data['name'] = $user->name = $request['name'];
$data['email'] =$user->email = $request['email'];
$user->phone = $request['phone'];
$data['username'] = $user->username = $request['username'];
$user->reference = $request['username'];
$user->country = $request['country'];
$user->state = $request['state'];
$user->under_reference = $upliner;
$user->email_verify = $email_verify;
$user->email_code = $email_code;
$user->email_time = $email_time;
$user->phone_verify = $phone_verify;
$user->phone_code = $phone_code;
$user->phone_time = $phone_time;
$data['password'] = $user->password = bcrypt($request['password']);
$saved = $user->save();
if($saved){
//send mail to the registered user
Mail::send('emails.welcome-email', ['data' => $data], function ($message) use ($data) {
$message->from('noreply#example.com', 'example.com')
->to($data['email'], $data['username'], $data['name'])
->subject('Welcome Email');
});
//send mail to Admin
$data['adminEmail'] = "example#gmail.com";
Mail::send('emails.registration-notification', ['data' => $data], function ($message) use ($data) {
$message->from('noreply#example.com', 'example.com')
->to($data['adminEmail'], $data['username'], $data['email'], $data['name'])
->subject('User Registeration Notification');
});
//send mail to referrer
if($uplinerUsername != null){
$chkReff = User::where('username',$uplinerUsername)->first();
if($chkReff == null){
return redirect('register');
}
else{
//get the person's details and send mail
$userReff = User::where('username',$uplinerUsername)->first();
$data['userEmail'] = $userReff->email;
$data['refUsername'] = $userReff->username;
Mail::send('emails.referral-notification', ['data' => $data], function ($message) use ($data) {
$message->from('noreply#example.com', 'example.com')
->to($data['userEmail'], $data['username'],
$data['refUsername'], $data['userEmail'], $data['email'], $data['name'])
->subject('Referral Notification');
});
}
}
return redirect('login');
}
`
I am trying to implement the save() method after the self::restrictionMinimunBid($ request, $ karateka, $ bid) function, but I get the following error: "Method App\\Http\\Controllers\\BidController::save does not exist."
Here is my code:
$bid = Bid::where('id_participants', '=', $request->id_participant)
->where('id_karatekas', '=',$market->id_karatekas)
->where('bid', '=',$request->bid);
if(!$bid->count()){
$bid->id_market = $market->id;
$bid->id_group = $market->id_group;
$bid->id_karatekas = $market->id_karatekas;
$bid->id_participants = $request->id_participant;
self::restrictionMinimunBid($request, $karateka, $bid);
$bid->save();
$response = array('code' => 200, 'Bid' => $bid, 'msg' => 'Bid created');
}else{
$response = array('code' => 400, 'error_msg' => "Bid already registered.");
}
public function restrictionMinimunBid(Request & $request, $karateka, & $bid)
{
$allKaratekas = Karateka::all()
->map(function ($allKaratekas) use ($karateka, $request, $bid){
if($karateka->id == $allKaratekas->id ){
if($request->bid > $allKaratekas->value){
$bid->bid = $request->bid;
$msg ="The bid is more than the value of karateka";
var_dump($msg);
}
else{
$error ="The bid is less than the value ofmkarateka";
var_dump($error);
}
}
});
}
You can replace this one:
$bid = Bid::where('id_participants', '=', $request->id_participant)
->where('id_karatekas', '=',$market->id_karatekas)
->where('bid', '=',$request->bid);
To this:
$bid = Bid::where('id_participants', '=', $request->id_participant)
->where('id_karatekas', '=',$market->id_karatekas)
->where('bid', '=',$request->bid)->first();
i want to write a API for updating data in sql. I am using CI for it.I am new to this field.while i have written it is not working in localhost itself .Can anyone help me? I am attaching my controller and model here.it is showing an error like this page is not working
function editprofile($id,$data) {
$this->db->where(array('user_id' => $id));
$this->db->update('registrationdetails', $data);
if(!empty($id))
{
$result = true;
} else {
$result = false;
}
return $result;
}
public function updateuser()
{
$adminId = 1;
$AUTHENTIC_KEY = "4u0IOxa1YTwNo38QjArD9ysW6PgVnbX7vtlJ";
$user_id=2;
$firstname ="aiswarya";
$lastname ="mathew";
$email ="aiswarya#gmail.com";
$password ="aiswarya";
$confirmpassword ="aiswarya";
$contactnumber ="999999999";
$gender ="female";
$address ="canada";
$department ="cse";
$designation ="swe";
$Admindetails = $this->common->CheckValidAdmin($adminId,$AUTHENTIC_KEY);
if($Admindetails != false)
{
$validAdmin = array('authentication_key' => $Admindetails->authentication_key,
'admin_id' => $Admindetails->id
);
$data = array();
$data = array('firstname'=>$firstname,'lastname'=>$lastname,'email'=>$email,'password'=>$password,'confirmpassword'=>$confirmpassword,'contactnumber'=>$contactnumber,'gender'=>$gender,'address'=>$address,'department'=>$department,'designation'=>$designation);
$status = $this->user->editprofile($user_id,$data);
if($status == true)
{
$response = array('status' => 200,
'message' => 'updated successfully',
'admin_details' => $validAdmin
);
} else {
$response = array('status' => 404,
'message' => 'unable to add, please try again',
'admin_details' => $validAdmin);
}
} else {
$response = array('status' => 404,
'message' => 'Authentication Failed');
}
echo json_encode($response);
}
function editprofile($data) {
if(!empty($data['user_id']))
{
$this->db->where(array('user_id' => $data['user_id']));
$this->db->update('registrationdetails', $data);
$result = true;
} else {
$result = false;
}
return $result;
}
public function updateuser()
{
$data['adminId'] = 1;
$data['AUTHENTIC_KEY'] = "4u0IOxa1YTwNo38QjArD9ysW6PgVnbX7vtlJ";
$data['user_id']=2;
$data['firstname'] ="aiswarya";
$data['lastname'] ="mathew";
$data['email'] ="aiswarya#gmail.com";
$data['password'] ="aiswarya";
$data['confirmpassword'] ="aiswarya";
$data['contactnumber'] ="999999999";
$data['gender'] ="female";
$data['address'] ="canada";
$data['department'] ="cse";
$data['designation'] ="swe";
$Admindetails = $this->common->CheckValidAdmin($data['adminId'],$data['AUTHENTIC_KEY']);
if($Admindetails != false)
{
$validAdmin = array('authentication_key' => $Admindetails->authentication_key,
'admin_id' => $Admindetails->id);
$status = $this->user->editprofile($data);
if($status == true)
{
$response = array('status' => 200,
'message' => 'updated successfully',
'admin_details' => $validAdmin
);
} else {
$response = array('status' => 404,
'message' => 'unable to add, please try again',
'admin_details' => $validAdmin);
}
} else {
$response = array('status' => 404,
'message' => 'Authentication Failed');
}
echo json_encode($response);
}
I have problem with Laravel validation when validation fails it also call block of code where it should be successful...
I am trying to check for some user id if his admin_id field equal with user which is currently logged.
Here is code:
$auth = Auth::user()->id;
$inputs = array(
'id' => Input::get('id')
);
$rules = array(
'id' => "required|exists:users,id,admin_id,$auth"
);
$validate = Validator::make($inputs, $rules);
if ($validate->fails()) {
return $validate->messages()->all();
} else {
return 'succes';
}
Try doing this:
$rules = array(
'id' => "required|exists:users,id,admin_id," . $auth
);
You can do this without validation.
$auth = Auth::user()->id;
$input = Input::get('id');
if($auth != $input){
return 'your custom error message';
}else{
return 'success';
}