Support the ongoing development of Laravel.io →
Blade Artisan Database
Last updated by @keziantero 2 years ago.
0

Hi @keziantero

You could dd the $imovel variable before return a view to see if the owner_id is filled, something like this.

  /**
     * 
     *
     * @param  \App\Imovel  $Imovel
     * @return \Illuminate\View\View
     */
    public function show(Imovel $imovel)
    {
        // dumps the $imovel property
        dd($imovel);
        return view('imoveis.show', compact('imovel'));
    }

If the owner_id is filled maybe the onwer was deleted, and you're trying to access a property of an empty relation, you could use the null coalescing operator (??) while printing the owner cpf to check if it's null, and if is, print something like 'Not Found', like bellow:

<tr><td>{{ __('imovel.cpf') }}</td><td>{{ $imovel->owner->cpf ?? 'Not Found' }}</td></tr>

Or you could relate to all owners, including the deleted ones, in your Imovel class, like this.

/**
    * Get all of the Owner for the Imovel
    *
    * @return \Illuminate\Database\Eloquent\Relations\HasMany
    */
   public function owner() {
       
       return $this->belongsTo(Owner::class)->withTrashed();
   }

That would always retrieve a owner even if it was deleted, you could also show the owner status while printing his informations, like this:

<tr><td>{{ __('imovel.name_owner') }}</td><td>{{ $imovel->owner->name_owner}}{{$imovel->owner->trashed() ? ' - Trashed' : ''}}</td></tr>

I hope you have a great day!

keziantero liked this reply

1

Hello @alec ! How are you?

  • Check you suggested, it returns the owner's data, I can see the owner's name in the list of entries, but when I ask to detail, only this show.blade page presents an error.
  • I listed the owners in the Property class, but it returns another error message. [Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::withTrashed()]. :( anyway, thanks for the help.

keziantero liked this reply

1

Hi @keziantero,

Can you send the dd of the $imovel property and also the definition of your owner class?

The withTrashed() will only work if the related model has the SoftDeletes trait.

You could also do something like this to eager load the owner relation and dd that record.

 /**
     * 
     *
     * @param  \App\Imovel  $Imovel
     * @return \Illuminate\View\View
     */
    public function show(Imovel $imovel)
    {
        // dumps the $imovel property with owner relation 
        dd(Imovel::with('owner')->find($imovel->id);
        return view('imoveis.show', compact('imovel'));
    }

With that the dd would show with the relations property filled, if it isnt filled then its a relation issue.

Last updated 2 years ago.

keziantero liked this reply

1

Good Morning @alec! Returns me the following message "null // app\Http\Controllers\ImovelController.php:88"

Last updated 2 years ago.

keziantero liked this reply

1

Hi @keziantero,

Try to change your show method to the following:

  /**
     * 
     *
     * @param  in  $id
     * @return \Illuminate\View\View
     */
    public function show($id)
    {
        // dumps the $imovel property with owner relation 
        dd(Imovel::with('owner')->find($id);
        return view('imoveis.show', compact('imovel'));
    }

And your getSeqLinkAttribute() in your Imovel class to the following:

 /**
    * 
    *
    * @return string
    */
   public function getSeqLinkAttribute()
   {
       $title = __('app.show_detail_title', [
           'seq' => $this->seq, 'type' => __('imovel.imovel'),
       ]);
       $link = '<a href="'.route('imoveis.show', $this->id).'"';
       $link .= ' title="'.$title.'">';
       $link .= $this->seq;
       $link .= '</a>';

       return $link;
   }

And check that dd again, you should be able to see the imovel record.

keziantero liked this reply

1

Hello friend Alex! I wanted to thank you for your help, but unfortunately I don't know where the error is in this code, I've tried everything :,(. If you can, and if you want, take a look at the code. Here's the github link where the project is: https://github.com/KeziAntero/sigi-TCC

keziantero, alec liked this reply

2

Hi @keziantero,

Sure, i'll take a look.

keziantero liked this reply

1
Solution

Hi @keziantero,

I created a pull request in that project fixing the bug.

The bug occurred because you were injecting a Imovel Class Instance via Larave IoC container, i fixed it by passing the $imovel id to the show method and in there i retrieved that row.

I tested it and it worked in my local test bench.

keziantero liked this reply

1
Solution selected by @keziantero

Thank you very much Alec for the help. I wish you all the best. Good Morning! I'll take a look in a little while.

alec liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.