Vrajesh Doshi
Vrajesh Doshi

Reputation: 764

Laravel returning empty collection even if the records exist

The following is the Model class

namespace App\Models\BaseModels;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class BallByBall extends Model
{
    use SoftDeletes;
    protected $table = 'ball_data';
    protected $primaryKey = 'trans_id';
    protected $guarded = [];
    protected $dates = ['deleted_at'];
}

The schema of ball_data is:

CREATE TABLE `ball_data` (
  `trans_id` int(10) NOT NULL AUTO_INCREMENT,
  `tour_id` int(11) DEFAULT NULL,
  `m_trans_id` int(11) DEFAULT NULL,
  `match_id` int(11) DEFAULT NULL,
  `team_id1` int(11) DEFAULT NULL,
  `team_id2` int(11) DEFAULT NULL,
  `innings` int(11) DEFAULT NULL,
  `batsman_id` int(11) DEFAULT NULL,
  `batsman_id2` int(11) DEFAULT NULL,
  `bowler_id` int(11) DEFAULT NULL,
  `fielder_id` int(11) DEFAULT NULL,
  `batsman_score` int(11) DEFAULT NULL,
  `bowler_given` int(11) DEFAULT NULL,
  `extra_runs` int(11) DEFAULT NULL,
  `total_runs` int(11) DEFAULT NULL,
  `team_runs` int(11) DEFAULT NULL,
  `for_wicket` int(11) DEFAULT NULL,
  `ball_no` int(11) DEFAULT NULL,
  `ball_type_id` int(11) DEFAULT NULL,
  `ball_type` varchar(573) DEFAULT NULL,
  `over_no` double DEFAULT NULL,
  `maiden` int(11) DEFAULT NULL,
  `wicket_id` int(11) DEFAULT NULL,
  `wicket_type` varchar(573) DEFAULT NULL,
  `wicket_desc` varchar(573) DEFAULT NULL,
  `ball_length_id` int(11) DEFAULT NULL,
  `ball_area_id` int(11) DEFAULT NULL,
  `field_type_id` int(11) DEFAULT NULL,
  `power_play` int(11) DEFAULT NULL,
  `remark` varchar(573) DEFAULT NULL,
  `commentry` varchar(573) DEFAULT NULL,
  `deleted_by` int(11) DEFAULT NULL,
  `updated_by` int(11) DEFAULT NULL,
  `deleted_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`trans_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5025 DEFAULT CHARSET=latin1

When I execute App\Models\BaseModels\BallByBall::get(); I receive an empty collection.

Upvotes: 1

Views: 1589

Answers (2)

SallyJesseRafael
SallyJesseRafael

Reputation: 31

I think you must use something to get soft deleted records like ->withTrashed() function. Then is it possible to get() or all() the collection.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You're using SoftDeletes trait and it looks like you have a timestamp in deleted_at column instead of null for this record. This means this record is soft deleted and Laravel won't show it by default.

So, use the withTrashed() method to include all soft deleted rows as well:

BallByBall::withTrashed()->get()

Or change deleted_at to null for this record.

Upvotes: 3

Related Questions