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

clat23's avatar

Accessing soft deleted records through a model relationship

Listing model: App\Listing.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

use App\Upc;

class Listing extends Model
{
    use SoftDeletes;
    
    protected $dates = [
        'deleted_at',
    ];

    public function upc()
    {
        return $this->belongsTo(Upc::class);
    }
}

UPC model: App\Upc.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Auth;

class Upc extends Model
{
    public function listing()
    {
        return $this->belongsTo(Listing::class);
    }

    public function get()
    {
        $upcs = Upc::where('user_id', Auth::user()->id)
            ->orderBy('id', 'asc')
            ->paginate(25);

        return $upcs;
    }
}

For a UPC associated to an active listing: $upc->listing->title returns the associated listing's title just fine.

However, when a listing is soft deleted, I can no longer do $upc->listing->title to access the listing or any of its data.

Is there any way I can still access soft deleted listings through the UPC to Listing relationship?

0 likes
1 reply

Please or to participate in this conversation.