This is a strange one (for me at least), I'm trying to pass an object to a function like so:
272. $runs[$key]['position'] = getTimePosition($run->times,$user['id']);
273. dd($run->times);
When I do it I get the error
message: "Trying to get property 'times' of non-object", exception: "ErrorException",…}
exception: "ErrorException"
file: "C:\Users\Test\PhpstormProjects\test\app\Helpers\Helper.php"
line: 272
message: "Trying to get property 'times' of non-object"
However, if I dump $run->times I get what seems to be correct, an Eloquent object with "times" as shown below...
What am I missing here? It's been bugging me for an hour
As I guess your result set return multiple rows so that you need to iterate them by using the loop or you can get the result by using.
$run[0]->time
If you want to get a single row from DB you should use it.
$run = DB::table('tableName')->where('condition')->first();
$run->time
It will work.
Your attribute name is $run->time, not $run->times
$runs[$key]['position'] = getTimePosition($run->time,$user['id']);
Try this one
if($run) {
$runs[$key]['position'] = getTimePosition($run->times,$user['id']);
}
Related
I'm very fresher in CodeIgniter and practicing. I'm now developing a simple Codeigniter application just for practicing. I've Banks and its branches in the database. I just want to show branches with its bank name. Branches are showing but in the controller while getting banks, this error is showing. I tried these in SO links, but nothing found works.
"A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object"
in Line Number: 85 and 91
This is my controller
function ShowBranchList() {
$branch_list = $this->FD_Model->get_all_branches();
$get_bank = $this->FD_Model->get_bank_by_branch($branch_list->bankid); //This is Line Number 85
if($branch_list ){
$data = array(
'pagetitle' => 'Branch List View',
'branch_list_data' => $branch_list,
'br_bank' => $get_bank['bank_name'],//This is Line Number 93
);
}
Model
/*function for getting all branches*/
function get_all_branches() {
$this->db->order_by( $this->brid, $this->order );
return $this->db->get( $this->brtable )->result();
}
/*function for getting banks by branches*/
function get_bank_by_branch( $id ) {
$this->db->where( $this->bid, $id);
return $this->db->get( $this->banktable )->row();
}
And finally, this is my View
foreach ($branch_list_data as $branch_list)
{
<?php echo $branch_list->brname ?>
<?php echo $br_bank; ?>
}
Anyone know the issue?
Update
When I'm tring to view the query
echo $this->db->last_query();
Result
SELECT * FROM tbl_bankmaster WHERE bid IS NULL
The value $branch_list->bankid is null.When I'm hardcoding some values the view page is showing without any error.
do like this
$get_bank = $this->FD_Model->get_bank_by_branch($branch_list[0]->bankid);
AND
'br_bank' => $get_bank[0]->bank_name # this will work maybe
or
'br_bank' => $get_bank[0]['bank_name']
Its 0 indexed array so you need to add the key to access the child's
Change your data in view like this
foreach ($branch_list_data as $branch_list)
{
echo $branch_list['brname']; //change your data like this
echo $br_bank;
}
I finally make it happen with the help of Abdulla Nilam's answer. I just passed the value in array like
'br_bank' => $get_bank->bank_name
Now the view is working.
get_bank_by_branch($branch_list->bankid); This method returning only object.
$get_bank this variable you're accessing by an array, but it's an object.
$somedata = Indicator::all();
$indicators = [];
// ... (re-structure the data; rows to columns)
$indicators[] = ['a'=>'2016', 'b'=>'2017', 'c'=>'2018'];
$indicators[] = ['a'=>'1232', 'b'=>'3242', 'c'=>'5467'];
$indicators[] = ['a'=>'1232', 'b'=>'3242', 'c'=>'5467'];
$indicators[] = ['a'=>'closed', 'b'=>'closed', 'c'=>'open'];>
// ??? How to form a valid object to send ???
return view('indicators.index')->with(['indicators'=> $indicators]);
I select data. I change the structure for displaying it; but cannot find the correct structure to then pass in my response.
The view throws the error "Trying to get property of non-object (View: "
(I looked at the dump of Indicator::all(); and wonder if I have the right/wrong approach)
// noob
You're returning an array, I image you are probably trying to access an index using object notation like:
$val->prop
when it should be:
$val['prop']
Indicator::all() returns a collection of objects which you're not using in your view.
As an aside, Laravel collections have some handy helper functions to work with result sets. You may be interested in:
https://laravel.com/docs/5.5/collections#method-map
As $indicators is an array so you have to use [''] instead of -> to access its data.
I run a query using Laravel's DB first() which returns an object, when I check using dd() or vardump(). But when I try to print the value using echo ($promotion->pp_name); it gives error, but same property shows while dd($promotion->pp_name);
<?php dd($promotion->pp_name); ?> prints "urgent"
<?php echo ($promotion->pp_name); ?> but it gives "Trying to get property of non-object"
Full object dump results: <?php dd($promotion); ?>
{#196 ▼
+"ppo_id": 23
+"ppo_prj_id": 68
+"ppo_pp_id": 4
+"ppo_updated_date": "2014-05-20"
+"ppo_status": 1
+"pp_id": 4
+"pp_name": "urgent"
+"pp_dispText": "I want my project to be marked as an urgent project"
+"pp_amount": "5.00"
+"pp_updated_date": "2013-08-09"
+"pp_status": 1
}
and the function that return this object.
function getProjectPromotion($value='')
{
$project_id = $value;
$promotion = DB::table('project_promotion_option')
->join('project_promotion', 'project_promotion_option.ppo_pp_id', '=', 'project_promotion.pp_id')
->where('ppo_prj_id', '=' , $project_id )
->first();
return $promotion;
}
Are you calling this method in a loop? When you do a dd(), the script stops with the right result after the first loop run and all is good. But when you do echo in a loop, it continues and my guess is that at some point you pass some corrupted data that breaks the method.
We would need to see the code excerpt where you are calling the mentioned method to verify this hunch.
As Tadas Paplauskas suggest because the function call the query run in foreach each loop and in some iteration DB::first() method return null so echo gets error. i solved this by checking first if the value is set.
<?php if (isset($promotion->pp_name)) {
echo $promotion->pp_name;
} ?>
I'm sorry, this is yet another Trying to get a property of a non-object-question...
Here's some code and I just can't seem to figure out how this works (or not works rather):
$b = Model_Artist::query()->where('id', 18)->get_one(); // Fuelphp ORM query, returns \Orm\Model object
var_dump($b); // output: object(Model_Artist)[46] ... etc.
// definitely an object
var_dump($b->id); // [Error: Trying to get property of non-object] output: '18'
// umm ok, so maybe no object?
var_dump(is_object($b)); // output: bool(true)
// no, no, it is an object!
var_dump($b->id); // [Error: Trying to get property of non-object] output: '18'
// make up your mind, it's no object after all?
if ( is_object($b) ) {
var_dump($b->id); // output: '18' [No error!!]
}
// WAT?? It is only an object inside the if statement?
So the question is: why does PHP say that I try to get a property of a non-object in the first two cases, while is_object is true. And why does that suddenly change inside the if statement?
Either PHP is funny, or I'm doing something really really wrong?
$b is an array of objects. and so $b->id is not valid but reset($b)->id is.
I'm trying to create associative array of objects from row result set with member id as the key, but getting some error.
addATravelog() is just a function of the class UserLogsAndSOS(), whose objects i want in array.
Here is what I tried:
class UserArraySet {
private $arrayOfUsers = array();
function createArrayForTravelogs($result) {
While($row = $result->next()) {
if(array_key_exists($row['id'], $this->arrayOfUsers)) {
$this->arrayOfUsers[$row['id']] = new UserLogsAndSOS();
}
$this->arrayOfUsers[$row['id']]->addATravelog($row['title'], $row['blog']); //line 72
}
}
}
On calling createArrayForTravelogs() from the object I got the following error
Here is the error I got:
Notice: Undefined index: 1 in C:\xampp\htdocs\site\classes\userprofile.php on line 72
Fatal error: Call to a member function addATravelog() on a non-object in C:\xampp\htdocs\site\classes\userprofile.php on line 72
Can someone please let me know how to achieve this, I want something like this:
Array (
[1] => objectUserLogsAndSOS1
[5] => objectUserLogsAndSOS2
....
)
where key is the member id from $row.
I also need to check if the key exists, then call a function of that particular object to add data to its member, if not then create an object and then call a function of that particular object to add data to its member.
Thanks
just read the error message: you only create UserLogsAndSOS if there is already an entry - otherwise you call addATravelog on null.
maybe you forgot the "!" in your if clause?
Because the array stays empty.
You only create a new UserLogsAndSOS when there already is an element with the provided ID in the arrayOfUsers. The exact opposite of what you probably wanted.
You're probably missing a ! to reverse the array_key_exist result.
if(!array_key_exists($row['id'], $this->arrayOfUsers)) {
$this->arrayOfUsers[$row['id']] = new UserLogsAndSOS();
}
You missed and '!' I think this is causing the error