Trying to get the 'id' property of non-imposition with Array

Asked

Viewed 273 times

1

I’m trying to get an ID from the database, but I get the following error:

Trying to get Property 'id' of non-object...

My code:

if($provider == null) {
                    $provider = array(
                        'provider' => $p->provider,
                        'items' => array($p),
                        'object' => User::find($p->provider),
                        'month' => array($order->id => $months)
                    );
                    $providers[] = $provider;
                }

The result will show me a filter for a report:

<?php $__currentLoopData = $providers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $provider): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php if($provider['object']->id != 3104): ?>
                    <option value="<?php echo e($provider['object']->id); ?>" provider="<?php echo e(json_encode($provider),true); ?>"><?php echo e($provider['object']->name); ?></option>
                <?php else: ?>
                    <option value="<?php echo e($provider['object']->id); ?>" provider="<?php echo e(json_encode($provider),true); ?>" class="d-none"><?php echo e($provider['object']->name); ?></option>
                <?php endif; ?>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

Error occurs on line <?php if($provider['object']->id != 3104): ?> I use PHP 7.2.

  • And what is $order?

  • It is a variable that receives requests.

  • 1

    Okay. And what kind is she? She owns the field id? What is the return of var_dump($order)?

  • On what line exactly are you making this mistake? Why might you also be giving in <?php if($provider['object']->id != 3104): ?> if $provider['object'] by chance is null you’ll never find a id there, and in your 'object' => User::find($p->provider) there is no guarantee q 'object' it won’t be null

  • It is of the null type, set when necessary. Yes it receives the id of the bank, which will filter the providers.

  • The mistake is making <?php if($provider['object']->id != 3104): ?>, but before it was working normally.

  • @Erloncharles And how could I make mine object never came back null of my search in the bank? I would attribute it directly to id?

Show 2 more comments

1 answer

1


The most recommended way to ensure that you will not have one $provider['object'] null is placing a check before the assembly of the provider

$user = User::find($p->provider);
if($provider == null && $user instanceOf User) {
    $provider = array(
        'provider' => $p->provider,
        'items' => array($p),
        'object' => $user,
        'month' => array($order->id => $months)
    );
    $providers[] = $provider;
}

The instanceOf will check your variable $user is in fact an instance of your model User thus ensuring that she will have the attribute id that today he claims that there is no

But you can also treat that in your Blade

Browser other questions tagged

You are not signed in. Login or sign up in order to post.