Reputation: 553
I am new in laravel. I got this error in my code.
DataController.php
// Get Affiliate Data
$data = Commission::where([
'uploads_id' => $period ,
'affiliate_code' => $id
])->first();
//Get Last Activity
$lastactivitytxt = null;
$lastactivity = Activity::where('text','LIKE','%Affiliate ['.$data->affiliate_name.']%'); //This is line 168
Can someone tell me what is wrong with my code?
Upvotes: 3
Views: 1459
Reputation: 8558
just use the ->first() or ->get()
$lastactivity = Activity::where('text','LIKE','%Affiliate ['.$data->affiliate_name.']%')->get();
Upvotes: 0
Reputation: 1561
It looks like you're trying to get to an object that does not exist in the variable $data. Make yourself instead of first(), count() to check what return you the result. If you get a score> 0, it means that you give not the object, if = 0, it means that nothing was found and trying to get to something that does not exist. You are going to stop in such a way to check if $data is not empty string when you use the first() or check the count() and if it is <= 0 watch out false, and only later try coupled to objects. As you have any additional questions to write greetings!
Upvotes: 0
Reputation: 3226
Like @chanafdo says: $data is empty AKA NULL. You should first check if $data is not NULL.
Also like @vinod-vt mentioned: you forgot to use get()
, first()
or list()
to retrieve your Activity
s from your database.
Example:
// Get Affiliate Data
$data = Commission::where([
'uploads_id' => $period ,
'affiliate_code' => $id
])->first();
// Check database resource exists
if (!$data)
{
// Throw error, 404 or whatever
return false;
}
//Get Last Activity
$lastactivitytxt = null;
$lastactivity = Activity::where('text','LIKE','%Affiliate ['.$data->affiliate_name.']%')->get(); // You forgot get()
Upvotes: 1
Reputation: 7149
Use get()
or first()
or lists()
in your query like,
$lastactivity = Activity::where('text','LIKE','%Affiliate ['.$data->affiliate_name.']%')->get();
Upvotes: 2