You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
publicfunctionshow($port)
{ //@param int $portreturn ShadowsocksUser::find($port);
}
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
useIlluminate\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
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 AThe implemention of
ShadowsocksUserCtl
Controller —— Code Edition AThe implemention of
ShadowsocksUser
Model —— Code Edition AThe document structure in MongoDB:
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()
ofShadowsocksUserCtl
Controller as following: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
ModelCode of
ShadowsocksUserCtl
Controller and API route are the same as Code Edition AThen 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()
inapp/Providers/RouteServiceProvider.php
2.Change
Route::resource
toRoute::get
to
The response of URL "http://localhost/api/user/10000" is:
Laravel Version:
Laravel Framework 5.4.35
Laravel-MongoDB Version:
3.2.2
###System Info:
The text was updated successfully, but these errors were encountered: