zedee
zedee

Reputation: 429

Laravel Eloquent::Find() returning NULL with an existing ID

It's pretty straightforward as it's the most basic thing but I don't know what I'm missing:

Having a model called Site

I'm using Eloquent ORM, so when I call (in a controller)

$oSite = Site::find(1)

and then

var_dump($oSite);

It returns a value of NULL.

But when I check the database, the table 'sites' actually contains the following item:

id: 1
user_id: 1
name: test

In my Site model I have the following code:

use Illuminate\Database\Eloquent\ModelNotFoundException;

 Class Site extends Eloquent {

        protected $table = 'sites';

        protected $fillable = ['user_id', 'name'];
 }

Instead, if I gather the item with the following:

$oSite = DB::table('sites')
                ->where('id', 1)
                ->first();

It works and I get the correct register.

What I'm doing wrong? Which part of the documentation I didn't get?

EDIT:

Model code can be checked above.

Controller:

use Illuminate\Support\Facades\Redirect;
class SiteManagementController extends BaseController {

...

    public function deleteSite()
    {
        if (Request::ajax())
        {
            $iSiteToDelete = Input::get('siteId');

            $oSite = Site::find($iSiteToDelete);

            return var_dump($oSite);
        }
        else
        {
            return false;
        }
    }
}

EDIT 2: (SOLVED)

Real reason why wasn't working:

I had originally in my model code the following:

use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\ModelNotFoundException;

Class Site extends Eloquent {

    protected $table = 'sites';

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    protected $fillable = ['user_id', 'name'];
}

Problem was I added a 'deleted_at' column after I started the project and when I applied migrations, I didn't have softdeleting enabled. Obviously, I did a second error, forgetting to enable 'deleted_at' to be nullable, hence all inserts went had a wrong timestamp (0000-00-00 ...).

Fix:

  1. Made nullable 'deleted_at' column.

  2. Set all wrong 'deleted_at' timestamps to NULL.

Upvotes: 17

Views: 28900

Answers (4)

Makashov Nurbol
Makashov Nurbol

Reputation: 594

For the future if you encounter a similar problem you can check what SQL queries laravel is sending to the database.

Todo so just enable query logging by using DB facade:

\DB::enableQueryLog();

Before sending request to database. Then after using find() or get() you can get all requests by:

\DB::getQueryLog();

You can getQueryLog into dd() function and see what database queries were made.

Upvotes: 1

Soy César Mora
Soy César Mora

Reputation: 277

In my case I was using a custom query with the DB facade. I neglected to skip records that have a deleted_at in my DB query. When showing all the records, it worked with IDs that had already been deleted, so methods like find that if they were considering the deleted_at, did not find the record.

Layer eight.

Upvotes: 1

Anand Patel
Anand Patel

Reputation: 3943

Check you are getting Input::get('siteId') correctly. if you are getting it then try to convert it into integer i.e

$iSiteToDelete = intval(Input::get('siteId'));

Upvotes: 1

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81167

You're not returning your model.

var_dump prints output and returns nothing.

do this instead:

dd($oSite); // stands for var_dump and die - a helper method

and even better, simply return the model:

return $oSite; // will be cast to JSON string

Upvotes: 0

Related Questions