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
Hello @alec ! How are you?
keziantero liked this reply
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.
keziantero liked this reply
Good Morning @alec! Returns me the following message "null // app\Http\Controllers\ImovelController.php:88"
keziantero liked this reply
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
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
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
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community