Bonish Koirala
Bonish Koirala

Reputation: 651

In laravel, how to exclude rows while retrieving data from one table which have link to a deleted row on another table

I have a model like this

public function getProduct() {

        return $this->belongsTo('App\Product','product_id','id');

    }

This function is from a model called Support. The data in product_id matches to the id column in Product table. Some of the entries in supports table fetch product details using this relationship function. Now, when a row in the products table, for example:having id = 4, gets deleted or softdeleted, the rows in the supports table having product_id = 4 gets affected i.e. for a code like this

{{$support->getProduct->name}}

gives an error saying "Trying to get the property of a non object".

How do i get around this error such that if the rows in product table gets deleted or soft deleted, the rows in supports table having product_id equal to the id in products table do not get retrieved during query?

Upvotes: 1

Views: 314

Answers (1)

FULL STACK DEV
FULL STACK DEV

Reputation: 15971

if you can do this using blade.

{{$support->getProduct->name or ''}}

or

{{ isset($support->getProduct->name) ?  $support->getProduct->name:'' }}

Upvotes: 0

Related Questions