Skip to content

Route resource model inject is not working #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
YanzheL opened this issue Aug 30, 2017 · 4 comments
Open

Route resource model inject is not working #1278

YanzheL opened this issue Aug 30, 2017 · 4 comments

Comments

@YanzheL
Copy link

YanzheL commented Aug 30, 2017

Related System Infomation is located at footer.

I want to build a RESTful API using Laravel's automatic model inject, here is my code.

The API route in routes/api.php —— Code Edition A

Route::resource(
    'user',
    'ShadowsocksUserCtl'
);

The implemention of ShadowsocksUserCtl Controller —— Code Edition A

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\ShadowsocksUser;

class ShadowsocksUserCtl extends Controller
{
    public function index()
    {
        return ShadowsocksUser::all();
    }

    public function show(ShadowsocksUser $ssuser){
        return $ssuser;
    }
}

The implemention of ShadowsocksUser Model —— Code Edition A

<?php

namespace App;
use Jenssegers\Mongodb\Eloquent\Model;

class ShadowsocksUser extends Model
{
    protected $fillable = ['port'];
    protected $collection = 'users';
    protected $primaryKey = 'port';
}

The document structure in MongoDB:

{
  "_id":ObjectId('xxxxxxxxxxxxxxxx'),
  "port":NumberInt(10000),
  "other_fields":[]
}

When I visit the URL "http://localhost/api/user" using GET method in Browser, it will return all the users in MongoDB collection "users" as expected; whereas when I visit "http://localhost/api/user/10000", it show nothing but a empty page.

So if I change the function show() of ShadowsocksUserCtl Controller as following:

public function show($port)
{	//@param int $port
    return ShadowsocksUser::find($port);
}

the response of URL "http://localhost/api/user/10000" is correct, showing a user's document whose 'port' is 10000.

I suspect that the laravel-mongodb cannot support automatic model injection during route stage.

To confirm that, I changed my backend database as MySQL and made a fresh start

Changed the model as the Laravel default model class Illuminate\Database\Eloquent\Model

Changed the default db_connection as 'mysql'

Here is MySQL edition of the ShadowsocksUser Model

use Illuminate\Database\Eloquent\Model;
class ShadowsocksUser extends Model
{
    protected $fillable = ['port'];
    protected $table = 'users';
    protected $primaryKey = 'port';
}

Code of ShadowsocksUserCtl Controller and API route are the same as Code Edition A

Then I visit "http://localhost/api/user/10000" in browser, and the response is correct, showing a user's document whose 'port' is 10000.

So the auto model injection feature works with Original Laravel MySQL Model.

I want to know if there is any difference between laravel-mongodb and original Laravel mysql model when implementing auto model injection during routing, how can I achieve this feature?

Some approaches that have tried:

None of them worked with auto model injection.

1. Try to use explict model binding

modified the function boot() in app/Providers/RouteServiceProvider.php

public function boot()
{
    parent::boot();
    Route::model('port', ShadowsocksUser::class);
}

2.Change Route::resource to Route::get

Route::resource(
    'user',
    'ShadowsocksUserCtl'
);

to

Route::get(
    'user/{port}',
    'ShadowsocksUserCtl@show'
);

The response of URL "http://localhost/api/user/10000" is:

NotFoundHttpException
No query results for model [App\ShadowsocksUser].

Laravel Version:

Laravel Framework 5.4.35

Laravel-MongoDB Version:

3.2.2

###System Info:

System Linux homestead 4.4.0-81-generic #104-Ubuntu SMP Wed Jun 14 08:17:06 UTC 2017 x86_64
Build Date Aug 4 2017 13:04:12
Server API FPM/FastCGI
Virtual Directory Support disabled
Configuration File (php.ini) Path /etc/php/7.1/fpm
Loaded Configuration File /etc/php/7.1/fpm/php.ini
Scan this dir for additional .ini files /etc/php/7.1/fpm/conf.d
Additional .ini files parsed /etc/php/7.1/fpm/conf.d/10-mysqlnd.ini, /etc/php/7.1/fpm/conf.d/10-opcache.ini, /etc/php/7.1/fpm/conf.d/10-pdo.ini, /etc/php/7.1/fpm/conf.d/15-xml.ini, /etc/php/7.1/fpm/conf.d/20-bcmath.ini, /etc/php/7.1/fpm/conf.d/20-calendar.ini, /etc/php/7.1/fpm/conf.d/20-ctype.ini, /etc/php/7.1/fpm/conf.d/20-curl.ini, /etc/php/7.1/fpm/conf.d/20-dom.ini, /etc/php/7.1/fpm/conf.d/20-exif.ini, /etc/php/7.1/fpm/conf.d/20-fileinfo.ini, /etc/php/7.1/fpm/conf.d/20-ftp.ini, /etc/php/7.1/fpm/conf.d/20-gd.ini, /etc/php/7.1/fpm/conf.d/20-gettext.ini, /etc/php/7.1/fpm/conf.d/20-iconv.ini, /etc/php/7.1/fpm/conf.d/20-igbinary.ini, /etc/php/7.1/fpm/conf.d/20-imap.ini, /etc/php/7.1/fpm/conf.d/20-intl.ini, /etc/php/7.1/fpm/conf.d/20-json.ini, /etc/php/7.1/fpm/conf.d/20-mbstring.ini, /etc/php/7.1/fpm/conf.d/20-mongodb.ini, /etc/php/7.1/fpm/conf.d/20-msgpack.ini, /etc/php/7.1/fpm/conf.d/20-mysqli.ini, /etc/php/7.1/fpm/conf.d/20-pdo_mysql.ini, /etc/php/7.1/fpm/conf.d/20-pdo_pgsql.ini, /etc/php/7.1/fpm/conf.d/20-pdo_sqlite.ini, /etc/php/7.1/fpm/conf.d/20-pgsql.ini, /etc/php/7.1/fpm/conf.d/20-phar.ini, /etc/php/7.1/fpm/conf.d/20-posix.ini, /etc/php/7.1/fpm/conf.d/20-readline.ini, /etc/php/7.1/fpm/conf.d/20-shmop.ini, /etc/php/7.1/fpm/conf.d/20-simplexml.ini, /etc/php/7.1/fpm/conf.d/20-soap.ini, /etc/php/7.1/fpm/conf.d/20-sockets.ini, /etc/php/7.1/fpm/conf.d/20-sqlite3.ini, /etc/php/7.1/fpm/conf.d/20-sysvmsg.ini, /etc/php/7.1/fpm/conf.d/20-sysvsem.ini, /etc/php/7.1/fpm/conf.d/20-sysvshm.ini, /etc/php/7.1/fpm/conf.d/20-tokenizer.ini, /etc/php/7.1/fpm/conf.d/20-wddx.ini, /etc/php/7.1/fpm/conf.d/20-xdebug.ini, /etc/php/7.1/fpm/conf.d/20-xmlreader.ini, /etc/php/7.1/fpm/conf.d/20-xmlwriter.ini, /etc/php/7.1/fpm/conf.d/20-xsl.ini, /etc/php/7.1/fpm/conf.d/20-zip.ini, /etc/php/7.1/fpm/conf.d/25-memcached.ini, /etc/php/7.1/fpm/conf.d/90-blackfire.ini
PHP API 20160303
PHP Extension 20160303
Zend Extension 320160303
Zend Extension Build API320160303,NTS
PHP Extension Build API20160303,NTS
Debug Build no
Thread Safety disabled
Zend Signal Handling enabled
Zend Memory Manager enabled
Zend Multibyte Support provided by mbstring
IPv6 Support enabled
DTrace Support available, disabled
Registered PHP Streams https, ftps, compress.zlib, php, file, glob, data, http, ftp, sqlsrv, phar, zip
Registered Stream Socket Transports tcp, udp, unix, udg, ssl, tls, tlsv1.0, tlsv1.1, tlsv1.2
Registered Stream Filters zlib., string.rot13, string.toupper, string.tolower, string.strip_tags, convert., consumed, dechunk, convert.iconv.*
MongoDB support enabled
MongoDB extension version 1.2.9
MongoDB extension stability stable
libbson bundled version 1.5.5
libmongoc bundled version 1.5.5
libmongoc SSL enabled
libmongoc SSL library OpenSSL
libmongoc crypto enabled
libmongoc crypto library libcrypto
libmongoc crypto system profile disabled
libmongoc SASL disabled
@penkoad
Copy link

penkoad commented Sep 27, 2017

Havin the same issue here, I was expecting to fetch a catalog offer by reference rather than by id. But I'll stick to offer::where('ref', $offer).

(Please consider my gratitude for your awesome work with this library)

@Christophvh
Copy link

I'm Facing the same issue

@milosilic
Copy link

milosilic commented Nov 6, 2017

me also

using laravel 5.5 and 3.3.0-alpha version of laravel-mongodb

@AliN11
Copy link

AliN11 commented Jun 1, 2018

Is this issue solved?
I have faced this issue too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants