Jefsama
Jefsama

Reputation: 553

Php Laravel Trying to get property of non object

I am new in laravel. I got this error in my code.

capture


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

Answers (4)

Majbah Habib
Majbah Habib

Reputation: 8558

just use the ->first() or ->get()

$lastactivity = Activity::where('text','LIKE','%Affiliate ['.$data->affiliate_name.']%')->get(); 

Upvotes: 0

rad11
rad11

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

Thomas Van der Veen
Thomas Van der Veen

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 Activitys 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

Vinod VT
Vinod VT

Reputation: 7149

Use get() or first() or lists() in your query like,

$lastactivity = Activity::where('text','LIKE','%Affiliate ['.$data->affiliate_name.']%')->get();

Upvotes: 2

Related Questions