Laravel 4.2 use Sentinel with Jenssegers MongoDB - laravel-4

Is it possible to use Cartalyst/Sentinel with Jenssegers/MongoDB on Laravel 4.2?
I'm currently using Sentry, but I want to try Sentinel with new features.
After installation, I tried this:
$user = Sentinel::register([
'email' => $email,
'password' => $password,
]);
But I've got the following error:
Argument 2 passed to Illuminate\Database\Query\Builder::__construct()
must be an instance of Illuminate\Database\Query\Grammars\Grammar,
null given, called in
/home/vagrant/shared/muzza/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
on line 1853 and defined

Sentinel doesn't have integration with Jenssegers MongoDB lib.
Dan Syme commented on 2 Jul:
You would have to write the implementations for the different models
used throughout Sentinel.
Then switch out the models for the different entities on the config
file with your own implementations, Sentinel should take care of
everything else.
Maxime-BHA commented on 3 Jul:
As #jenssegers already did for Sentry package, you have to create your
own Models extending the originals models from Sentinel and bind the
attributes defined in those models for relationships/persistances to
the new models etc.. like static $usersModel =
'Cartalyst\Sentinel\Users\EloquentUser'; and also change the config
file.
See exemple for Sentry there :
https://github.com/jenssegers/laravel-mongodb-sentry
I think it's the exact same procedure.
More information you can find here.

Related

laravel5 Route::resource Generate wrong route

I am using Laravel 5.7 Route::resource.
I has saw
https://laravel.com/docs/5.7/controllers
when I use
Route::resource('koumokus', 'KoumokuController');
should be Generate as bleow
GET admin/koumokus/{koumoku} show
GET admin/koumokus/{koumoku}/edit edit
However, It is Generate
GET admin/koumokus/{koumokus} show
GET admin/koumokus/{koumokus}/edit edit
on the other hand,
Route::resource('funruis', 'FunruiController');
is normally to gent
GET admin/funruis/{funrui} show
GET admin/funruis/{funrui}/edit edit
How can I fixed it?
Sometimes laravel fails to find the conventional name for the parameter on the resource routes. It has happened to me sometimes when I used words that are not in english. But it allows you to customize that name. You can take a look at the docs here ->
Naming Resource Route Parameters
Since you are working with the 'koumokus' resource here, you can specify that the parameter for the 'koumokus' resource must be 'koumoku'.
Route::resource('koumokus', 'KoumokuController', [
'parameters' => [
'koumokus' => 'koumoku'
]
]);

Laravel 5.2: Inverse (reverse) of seeJson in testing

I've been using PHPUnit 4.8 on Laravel 5.2, wondering if it's possible to see if an API call does NOT have a JSON object in its response.
You can see if the response has a particular object, but how about doing the opposite?
$this->json('GET', 'api/items')
->seeJson(['id' => "100"])
->notSeeJson(['id' => "222"])//Is there anything like it?
->assertResponseOk();
I've been reading the documentation of PHPUnit and Laravel 5.2, but haven't found how to achieve it.
Any advice will be appreciated.
PS
In order to make sure that a particular object isn't included in the response, it'll suffice to count the total number of objects the response has.
With newer versions of PHPUnit, it's possible to do so with assertJsonCount(2, 'data').
But how about PHPUnit4/Laravel5.2?
It's important to note these are Laravel 5.2 methods, not PHPUnit methods, defined in Illuminate\Foundation\Testing\TestCase.
The inverse of seeJson is dontSeeJson.
$this->json('GET', 'api/items')
->seeJson(['id' => "100"])
->dontSeeJson(['id' => "222"])
->assertResponseOk();

Laravel Routes creating clashes with artisan migrate

Scenario
I have setup Laravel application routes in such a way that, where
Route::group(["domain" => getSubDomain()->sub_domain,"middleware"=>
["guest"]], function () {
//member routes
});
have one helper function getSubDomain()->sub_domain, which is checking sub_domain allowed in companies table. here is the helper function code
$domain = \App\Model\Company::where('sub_domain', request()->getHost())->whereIn("company_type_id", (array)$company_type_id)->first();
return !empty($domain) ? $domain : new \App\Model\Company(
[
"sub_domain" => !in_array(4, $company_type_id) ? env("sub_domain") : "",
"welcome_message" => env("welcome_message"),
"domain_flag" => "assets/images/dev-logo.png"
]);
In short this helper function will check in database about the sub domain is what? and based on valid sub domains routes are created.
Issue
This implementation is having requirement of sub_domain column from table and which is yet run using migration,
So when I am running migration using php artisan migrate it's showing me invalid column sub_domain.
This is creating conflict, that To run the migration routes are creating problem, and to prepare proper routes migration needs to be run.
Earlier the same implementation was using .env variables but that was bit tedious as the support of new domains is required.
Can anybody have solution to run migration without interfering Routes ?

Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically

Hi I am using laravel Sentinel as my Auth, also I am trying to use laravel auditing I am getting "Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically".
In my user model I have added a static function resolveId() for adding user_id in Laravel Auditing 'audits' table
public static function resolveId(){
return Sentinel::getUser()->getUserId();
//return auth()->check() ? auth()->user()->getAuthIdentifier() : null;
}
When I try to use \Sentinel::getUser() I am getting the error below.
Non-static method Cartalyst\Sentinel\Sentinel::getUser() should not be called statically
From the docs:
After installing the package, open your Laravel config file located at config/app.php and add the following lines.
In the $aliases array add the following facades for this package.
'Sentinel' => Cartalyst\Sentinel\Laravel\Facades\Sentinel::class,
Then just add this to the top of the class:
use Sentinel;
Put this use on top of the file in question:
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
I'm aware that the package version #manikandan k was asking help for is 4.x or 5.x, and while the documentation does mention the use case for Sentinel, it doesn't provide an actual example.
Since version 6.x, the Audit Resolvers documentation has this very use case, where Sentinel is used instead.
I suggest updating the resolver logic to the following:
return Sentinel::check() ? Sentinel::getUser()->getUserId() : null;
This will prevent calling getUserId() on null, when a user isn't logged.

Laravel 5.3 - how do I set fetchmode in query builder?

I'm working with legacy code. The old report engine uses associative arrays, Laravel's query builder returns an array of objects.
I need to turn objects into arrays. I've tried using:
\DB::connection('tars-test') //->setFetchMode(PDO::FETCH_ASSOC)
but that gets me Class 'App\Http\Controllers\PDO' not found
It's been suggested to put ->all() at the end of the query but that throws error Call to a member function all() on array
The most efficient way would be to set the fetchmode at runtime, for the legacy function and just for the legacy function. How do I do it?
You can use 'toArray' method:
https://laravel.com/docs/5.3/collections#method-toarray
Laravel 5.3 and lower
You went the right way but as you can see laravel is trying to find PDO in App\Http\Controllers\PDO which probably means you forgot to add use Illuminate\Database\PDO;
Laravel 5.4 and up
Since laravel 5.4 this is not an option. But you still can set fetch mode globally: Laravel 5.4 - How to set PDO Fetch Mode?
Or if you still wish to change it just locally:
Return values only (no keys/associative array) in Laravel

Resources