0
votes

I faced strange problem , i want to get data from my model but i cannot do this , my other models work fine but one of them return empty array In the event that table has data , here is my model :

 <?php

 namespace App\Hotels;

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

class Hotel extends Model
{
 use SoftDeletes;

 protected $table = 'hotels';

 protected $fillable = [
    'city_id',
    'title',
    'star',
    'lat',
    'lng',
    'phone',
    'location',
    'address',
    'description',
 ];

  public $timestamps = true;
}

i use Hotel::all() to get data but the result is :

   Collection {#434
   #items: []
   }

and my table records is : enter image description here

please help me to find out my problem , other models work perfect but this one not work.

3

3 Answers

3
votes

When a model uses the SoftDeletes trait it will add a scope to any queries for that model to not include any soft deleted rows (where the deleted_at is not null).

Both the rows you've shown in the image of your database have been soft deleted so to be able to retrieve these rows you can use withTrashed():

Hotel::withTrashed()->get();

When you use this method it will remove the scope mentioned above.

Alternatively, if you only want to only get the rows that have been trashed you can use onlyTrashed():

Hotel::onlyTrashed()->get();
2
votes

Because all records are softdelete in table. Try withTrashed() method.

Model::withTrashed()->get();

When records are softdeleted those records can not be fetched directly.

1
votes

In your controller withTrashed because you use softDeletr in your model.

Use App/Hotel;


public function functionname(){
$data= Hotel::withTrashed()->get();
dd($data);
}