why i m null getting value in laravel - php

here is my controller i don't know why i have null value i check using dd($user)
public function success1(Request $request)
{
$phonetoken = $request->input('phonetoken');
$user =User::where('phonetoken', $phonetoken)->first();
dd($user);
if(!is_null($user)){
$user->phoneconfirmed = 1;
$user->phonetoken = " ";
$user->save();
return redirect('sellerdashboard');
}
return redirect()->back();
}
why i m not getting value in $user always getting null

Thats probably because your eloquent cannot find the result.
Use dd($phonetoken) just to be sure the variable is being passed properly.
Also check phonetoken is in your user model.
Othet than this your code seems to be correct.

Related

Why model/variable keeps its value?

I have problem with my code in Laravel Framework. What I am trying to do is sell method inside model. The problem is that $variable keeps its value untill next code execution. How I should make it so it's gonna work like I want to?
/// Model method.
public function Sell()
{
$this->UserData->increment('gold', ($this->ItemData->price / 100) * 80);
$this->delete();
return null;
}
///in controller
$user = \Auth::user();
$user_item = UserItems::find(10);
$user_item->Sell();
return $user_item->ItemData->name; /// Returns full data about model even if I deleted it/sold. After next refresh, returns null/error.I want it to return null since beginning.
Even if you have deleted a record from a database by $this->delete(), $user_item variable still holds a reference to the filled model until the script ends or you destroy the variable.
It seems that you want to return a result of the sell() function. In your case it would be
///in controller
$user = \Auth::user();
$user_item = UserItems::find(10);
$user_item->Sell();
return $user_item->Sell();
I don't know what you are trying to do but below code will solve your issue -
/// Model method.
public function Sell()
{
$this->UserData->increment('gold', ($this->ItemData->price / 100) * 80);
$this->delete();
$this->ItemData = $this->ItemData->fresh()
return null;
}
///in controller
$user = \Auth::user();
$user_item = UserItems::find(10);
$user_item->Sell();
return $user_item->ItemData->name; /// Returns full data about model even if I deleted it/sold. After next refresh, returns null/error.I want it to return null since beginning.

CakePHP $this->Auth->user("id") always return null?

I have a function that I use to get the user id of Auth component. It works fine without use return json_encode. The problem is that I need this works with json_encode because I get values from ajax request.
Using json_encode it always return null to id and I can't understand why does it occur. The problem is with the function indexAjax() below.
How could I use $this->Auth->user("id") with json_encode and it not return null ?
Trying.
//using $this->set it works fine
public function index() {
$id = $this->Auth->user("id");
$empresas = $this->Empresa->find('all', array(
'fields'=>array("id", "nomeFantasia", "cnpj",
"telefone1", "telefone2", "celular", "aberto"),
'conditions'=>array("users_id = "=> $id)
));
debug($id) or die;
//$this->set(compact('empresas'));
}
//with json_encode always return null
public function indexAjax() {
$this->autoRender = false;
$id = $this->Auth->user("id");
$empresas = $this->Empresa->find('all', array(
'fields'=>array("id", "nomeFantasia", "cnpj",
"telefone1", "telefone2", "celular", "aberto"),
'conditions'=>array("users_id = "=> $id)
));
return json_encode($id);
}
solved the problem. My solution was when user make login I get the user id and write in session so when I need this id I get from session directly and not from AuthComponent.
It works.

Laravel Eloquent Can't Get Attribute When I Use Find

I need to return email of the user like in example:
public function getUserEmailAttribute()
{
$user = User::find($this->used_by);
return $user->email;
}
When I use $this->used_by it's throw me this error:
Trying to get property of non-object (View: /home/mertindervish/Documents/school/app/views/admin/invitation/list.blade.php)
But when I use a string like '2', '3' it's working. Is there any problem with my code? I tried to var_dump $this->used_by and it's return string(1).
It turns out that one invitation didn't have a valid used_by value.
To prevent such an error in the future, add a null check:
public function getUserEmailAttribute()
{
$user = User::find($this->used_by);
if(is_null($user)) return null;
return $user->email;
}

Call to a member function fill() on a non-object

Trying to add information to an empty profile and redirect back.
$user = User::whereUsername($username)->firstOrFail(); // Selects correct user
$input = Input::all(); // a dd($input) at this point confirms input present
$this->profileForm->validate($input); // Passes
$user->profile->fill($input)->save();
return Redirect::route('profile.edit', $user->username);
If $user->profile is null then this gives the error: Call to a member function fill() on a non-object. I tried to remedy this with:
$user = User::whereUsername($username)->firstOrFail(); // Selects correct user
$input = Input::all(); // a dd($input) at this point confirms input present
$this->profileForm->validate($input); // Passes
if ($user->profile == null)
{
$user->profile = new Profile;
}
$user->profile->fill($input)->save();
return Redirect::route('profile.edit', $user->username);
But in this case it is redirected without adding the profile details ($user->profile is still null at this point).
If $user->profile already has information then this problem does not occur and the code works fine.
You can do that like this:
if (count($user->profile))
{
$user->profile->fill($input)->save();
}
else
{
$profile = Profile::create($input);
$user->profile()->save($profile);
}

why does deleted record still get returned using eloquent model?

I'm new to laravel, and this is the first framework I'm learning in any language, anyway,
I deleted some records using :
public function getForceLogOut()
{
$input = Input::all();
$e = $input['email'];
echo $e;
DB::select(DB::raw("DELETE FROM active_users WHERE email = '$e'"));
}
But the query executed through the eloquent model returns the object anyway, even though it has been actually been deleted (checked the table in phpMyAdmin)
public static function isLoggedIn()
{
$email = Auth::user()->email;
//$user = ActiveUser::where("email",$email); <== RETURNS THE OBJECT
$user = DB::select(DB::raw("SELECT * fROM active_users where email = '$email'")); // <== WORKS FINE
if ($user) {
return true;
} else {
Auth::logout();
return false;
}
}
Why is this happening? Doesn't eloquent model query the database and works on cached records or something similar?
EDIT: yes, as pointed out, it returns the QueryBuilder object! My mistake.
You are doing this:
$user = ActiveUser::where("email",$email);
which returns you a QueryBuilder, QueryBuilders are used by Laravel to prepare your queries while you're constructing them through your Eloquent model.
If you want to get the result from your database, you should do:
$user = ActiveUser::where("email",$email)->first();
which should return you the query result, or null if the record doesn't exist.

Categories