Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ajvanho's avatar
Level 14

Attempt to read property "..." on null

Attempt to read property "meta_value" on null

if ( $post_meta->where('meta_key', 'faq_content') ) {
            $faq_number = $post_meta->where('meta_key', 'faq_content')->first()->meta_value;
        } else {
            $faq_number = 0;
        }
0 likes
27 replies
bugsysha's avatar
bugsysha
Best Answer
Level 61

Change to:

if ( $post_meta->where('meta_key', 'faq_content')->exists() ) {
  $faq_number = $post_meta->where('meta_key', 'faq_content')->first()->meta_value;
} else {
  $faq_number = 0;
}

Or even better

if ( $something = $post_meta->where('meta_key', 'faq_content')->first() ) {
  $faq_number = $something->meta_value;
} else {
  $faq_number = 0;
}

Or simplify:

$faqNumber = $post_meta->where('meta_key', 'faq_content')->first()->meta_value ?? 0;
3 likes
rajan001's avatar

@bugsysha error is gone but my function didn't work in first attempt, after first attempt it works fine. what is the reason?

//Reducing Biniyojan balance from Budgets table
                    if($getBudgetValRev = bajet::where(['kriyakalap' => $biniyojan_details->kriyakalap])->first()){
                        $newBudgetRev = $getBudgetValRev->bujet + $biniyojan_details->cash;
                        bajet::where('kriyakalap',$biniyojan_details->kriyakalap)->update(['bujet'=>$newBudgetRev]);  
                    }else{
                        $newBudgetRev = 0;
                    }
 //Reducing Biniyojan balance from Budgets table

bugsysha's avatar

@rajan001 you would have to explain more, and please rewrite your code to English so it is easier to understand everything fully. I think I understand it even now, but I'm guessing what some of those words mean. So what is the first try and what exactly didn't work? Try to improve your communication so it is clear and concise, which will get you far in the future.

MichalOravec's avatar
$faq_number = PostMeta::where('meta_key', 'faq_content')->first()->meta_value ?? 0;
2 likes
kitesurf's avatar

@MichalOravec Perfect. Works great!

					$AmazonVendorPoStateData = AmazonVendorPoState::where('po_number', 																		 
                    $payloadPurchaseOrderNumberResult)->first()->po_state ?? 0;
                    echo $AmazonVendorPoStateData;
                    exit;
bugsysha's avatar

Kidding aside, I edit my posts few times before I refresh. I had no idea what you replied.

1 like
bugsysha's avatar

You are saying I do? I don't know how to use ?? operator?

1 like
MichalOravec's avatar

Of course you know ?? operator, it should be your first answer in my opinion :)

It doesn't matter :) have a nice day.

1 like
bugsysha's avatar

It will be 1:1

I can provide more links.

and you need it more :)

@michaloravec nice one. Touché!!!

Are we cool?

1 like
MichalOravec's avatar

I can provide more links.

Really? :D

Sorry for that.

Yeah we are cool.

2 likes
MichalOravec's avatar

@bugsysha Spend more times on Laracasts :), I had 0 best replies and only 40,000 experience 11 months ago.

1 like
bugsysha's avatar

@michaloravec I wanted to ask you how you did it, but I guess it boils down to time you've spent here.

It is hard with full time job and two kids. I'm also having issues focusing so I've blocked every site unrelated to work including Laracasts.

I haven't watched a movie in years now. If it wasn't for COVID19 and my family moving to weekend house so kids are not closed in the apartment during lockdown while I've stayed home cause of reliable internet I would've never watched Breaking Bad. That is how late to the party I am at this point.

automica's avatar

@michaloravec you have done well. My new job is keeping me too busy to spend time during the day on laracasts but I do have the opportunity to be working in laravel. My previous role was in financial services so legacy & silex. Glad to see you are still scooping up the best answers,

@bugsysha i have also sniped the odd best answer from Michal - it’s a rare treat to enjoy.

MichalOravec's avatar

@bugsysha Three monitors, I have always opened Laracasts on one of them with 30s refreshing the page :) And if there is something interesting I always reply to get points :)

I have also a full time job but I can still find a few seconds or minutes to help other people to find a solution.

And now during a corona virus what we can do, just stay at home.

bugsysha's avatar

@bugsysha i have also sniped the odd best answer from Michal - it’s a rare treat to enjoy.

@automica I once did snatch best answer from those two top guys who's names are forbidden to mention 🤣 But doing it to @michaloravec is also a nice reward 🥳

@michaloravec so that is the secret. I have 3 monitors also, but I'm only using one to help me focus and not to wonder off lately. Problem is that when I had time I didn't have knowledge. But now when I have knowledge I don't have time. I have to entertain those kids before and after work which is like another full time job. But I'm not complaining.

jlrdw's avatar

@automica you need to be here at times to help ox... you know who I mean. I think forum members has written his complete apps, I know you have contributed quite a bit.

automica's avatar

@jlrdw i have a problem child to deal with my in new role. We are fixing a laravel7 application which came to us (worked on by him) with no test coverage whilst trying to add more features and ship it to the public to use with only 12 weeks. At least he is keen to learn.

Please or to participate in this conversation.