Laravel-mongodb: find($id) Returning Null on Laravel 7

Created on 10 Sep 2020  Â·  13Comments  Â·  Source: jenssegers/laravel-mongodb

  • Laravel Version: 7.28
  • jenssegers/mongodb Version: dev-develop
  • PHP Version: 7.3.18

I'm trying to use mongodb but find function returning null.

My Model:

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
{
    protected $primaryKey = 'id';
}

My Controller:

public function show($id)
    {
            // Not working
        $find = Post::find($id);

            // Not working
        $find = Post::where('id', $id)->get();

            //  Working
        $find = Post::find(1);

            // Working
        $find = Post::where('id', 1)->get();

        dd($find);
}

dd returns null. But if I use $find = Post::find(1) I can see result (With exact id). Is there a solution for this or am I doing something wrong?

All 13 comments

Does this fix your issue https://github.com/jenssegers/laravel-mongodb/issues/2087#issuecomment-690020966 ?

Thanks!

Added protected $keyType = 'int'; to my model but still not working.

I will try the following:

Post::raw()->find(array('id' => $id));

That will use MongoDB driver, passing over this package, can you find something using this?

Also, dd($id); returns an int 1?

Hello,

Could you please try version 3.7?

Thanks!

Hello,

Could you please try version 3.7?

Thanks!

Sorry no changes...

Sorry, I can't reproduce.

@reasecret

use this:

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
{
    protected $primaryKey = 'id';
        protected $keyType = 'int';
}


@reasecret

use this:

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
{
  protected $primaryKey = 'id';
        protected $keyType = 'int';
}

I've tried that but not worked.

@reasecret did u find any solution for this issue? I'm having the same error.

@reasecret

use this:

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
{
  protected $primaryKey = 'id';
        protected $keyType = 'int';
}

$_id = '60a73dd8ce649c40f37e5374';
ok this fix my problem , but find($_id) return null ...

  • Laravel Version: 7.28
  • jenssegers/mongodb Version: dev-develop
  • PHP Version: 7.3.18

I'm trying to use mongodb but find function returning null.

My Model:

use Jenssegers\Mongodb\Eloquent\Model;

class Post extends Model
{
  protected $primaryKey = 'id';
}

My Controller:

public function show($id)
    {
            // Not working
      $find = Post::find($id);

            // Not working
      $find = Post::where('id', $id)->get();

            //  Working
      $find = Post::find(1);

            // Working
      $find = Post::where('id', 1)->get();

      dd($find);
}

dd returns null. But if I use $find = Post::find(1) I can see result (With exact id). Is there a solution for this or am I doing something wrong?

$find = Post::find($id);

        // Not working
  $find = Post::where('id', $id)->get();

        //  Working
  $find = Post::find(1);

        // Working
  $find = Post::where('id', 1)->get();

How to solve it in the end???

@enamor looks like there is no solution. So I'm not using package. Maybe I'll try another time.

I have a similar problem.

Database table has a primary key for id set by migration $table->id().

Tx::find(1); // returns all results from the table;
Tx::find(1)->toSql(); // show SELECT * FROM table;

Tx::where('id', 1); // works as expected
Tx::where('id', 1)->toSql(); // show SELECT * FROM table WHERE id = 1;

Never had issues with find() before, google lead me here, I'm on Laravel ^8.40

Was this page helpful?
0 / 5 - 0 ratings

Related issues

naveedyasin picture naveedyasin  Â·  3Comments

pirmax picture pirmax  Â·  3Comments

Vasiliy-Bondarenko picture Vasiliy-Bondarenko  Â·  3Comments

HassanIbrahim picture HassanIbrahim  Â·  3Comments

yupangestu picture yupangestu  Â·  3Comments