abraham foto abraham foto - 11 days ago 6
PHP Question

Method returns an array, and the select query doesn't work

I have a method in my model that returns an array:

public function status($s_type)
{
if (!in_array($s_type, ['speed', 'health'])){
return false;
}

$o_status = Status::where('name', strval($s_type))->first();
$o_response = $this->statuses()
->where('status_id', $o_status->id)
->select('values', 'created_at')
->orderBy('created_at','DESC')
->first();

if($o_response === null){
return false;
}

return [
'value' => $o_response->values,
'timestamp' => $o_response->created_at
];
}

I want to get the timestamp. So, I called this method in a controller. it uses an

public function status(){

$client = new Client();
$notification=Notification::where('active',1)->get();
$status = Status::where('name', 'health')->first();

foreach ($notification as $note) {

$status_health = $note->status('health');
$status_health['timestamp'];


print_r($status_health);
echo '<br>';
/*
;
}*/
}

}


I want to print only the time stamp? now it is printing the whole object.
the Print_r is printing the whole object instead of timestamp only. I am really bad with associative array.

Answer

Are you sure that this is all the code we need to comprehend the problem? I'm asking here, because the error message says that you are calling get() on an array, yet the only two uses of get() in the code you provided are

$notification = Notification::where('active',1)->get();

(Called on query builder -> valid)

and

$response = $client->get($note->website_url);

I couldn't find a get() method in Illuminate\Database\Eloquent\Model, but maybe you wrote one or I'm missing something. In either case, $client is not an array, so that line can not be responsible for the error. So, I think the error is in some part of your code that you didn't show us yet.

There's one other thing that caught my eye: In your status($s_type) method, right at the beginning, you're saying

if (!in_array($s_type, ['speed', 'health'])){
    return false;
}

then you call that method later with

$check_frequency = $note->status('timestamp');

so this will always be false.

Comments