Laravel Sentinel setModel accessing the wrong table - laravel

I am attempting to use multiple tables for my authentication based on a subdomain. Therefore, I am attempting to use Sentinel's setModel function during runtime to set the model instance.
So in my routes file I have some simple logic:
if (Request::getHost() == $url) {
Sentinel::getUserRepository()->setModel('App\Client');
} else {
Sentinel::getUserRepository()->setModel('App\User');
}
The logic being one URL uses one model and the other uses the other model. Now, when I am attempting to load the App\Client by doing a dd(Sentinel::getUserRepository()); I get a return such as:
IlluminateUserRepository {#113 ▼
#hasher: NativeHasher {#114}
#model: "App\Client"
#dispatcher: Dispatcher {#23 ▶}
#dispatcherStatus: true
}
This is great, however, when I call Sentinel::getUser();, I am left with the following output:
EloquentUser {#230 ▼
#table: "users"
#fillable: array:5 [▶]
#hidden: array:1 [▶]
#persistableKey: "user_id"
#persistableRelationship: "persistences"
#loginNames: array:1 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▶]
#original: array:12 [▶]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#permissionsInstance: null
}
This is referring to the App\User model rather than my App\Client model.
I don't understand why, but I can't seem to get functions of Sentinel to look at the model I am trying to reference.
Here is my Client model as a point of reference.
namespace App;
class Client extends \Cartalyst\Sentinel\Users\EloquentUser
{
/**
* The fields for sentinel to authenticate (login)
*
* #var array
*/
protected $loginNames = ['email'];
/**
* The table associated with model.
*
* #var String
*/
protected $table = 'clients';
/**
* Used for mass assignment fields.
*
* #var Array
*/
protected $fillable = ['email', 'password', 'permissions', 'last_login', 'first_name', 'last_name'];
}
I have used this guide from the Github Wiki, however as you can see Im not achieving what I am trying to do.
How can I use this approach of setting the model during runtime and use the correct model with all of sentinels commands?

To anyone who encounters the same issue, i flagged this as an issue in the cartalyst/sentinel repository and some feedback was provided.
The solution is to not only set the user repository but also define the model on the persistence repository:
Sentinel::getUserRepository()->setUsersModel('App\Client');
Sentinel::getPersistenceRepository()->setUsersModel('App\Client');
Through this methods such as Sentinel::getUser() will access the model specified above.
Please see the full response here: https://github.com/cartalyst/sentinel/issues/367

Related

UpdateorInsert method in dynamic column cominig from array

I have some misunderstanding with the UpdateOrInsert helper with the DB
DB::table('table')
->updateOrInsert( );
}
I have a dynamic collection structured like this:
^ Illuminate\Support\Collection {#1534 ▼
#items: array:9 [▼
"it-IT" => array:1 [▶]
"cs-CZ" => array:1 [▶]
"de-DE" => array:1 [▶]
"en-GB" => array:1 [▶]
"en-US" => array:1 [▶]
"es-ES" => array:1 [▶]
"fr-FR" => array:1 [▶]
"ko-KR" => array:1 [▶]
"sk-SK" => array:1 [▶]
]
I would like to use the it-IT array as reference and Update the other languages or Insert a new line if the it-IT string don't exist.
This Collection comes from an array, I transform it in collectio just for use the chunk(); function.
The problem is, after a lot of tries, using in the correct way the UpdateOrInsert.
Cause, I undestand that I need to separe the reference with the rest, so I split the collection in two collection:
$coll = collect($array);
$chunks = $coll->chunk(500);
$refcol = collect($refArr);
$refchunks = $refcol->chunk(500);
foreach ($chunks as $chunk)
{
DB::table('dictionaries')
->updateOrInsert($refchunks->toArray(),$chunk->toArray() );
}
But this is not enought cause when it start to upgrade the data inside the database i get a strange query error:
select exists(select * from `dictionaries` where ((`1` = stringa di riferimento ))) as `exists`
so I think I have to split the collection in all languages collection, but the number and the languages are not always the same.
How can I do a foreach inside the UpdateOrInsert?
I have tried olso to using the Model way
Dictionary Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Dictionary extends Model
{
protected $primaryKey = null;
public $incrementing = false;
public $timestamps = false;
}
But when I perform the save() but if the it-IT string already exist it go in exception.
I tell to my self "Never give up" so I tried a thing like this:
try{
Dictionary::save();
} chatch (\Exception $e){
Dictionary::update();
}
But it save me the string like { 1: 'string in correct column'} and update method doesn't work.
Any way to help me?

Laravel resource always return null

I am having strange issue. The resource method always returns null.
Table name
operating_days
Model
class OperatingDay extends Model
{
/**
* #var string[]
*/
protected $fillable = ['day', 'date'];
}
Controller#edit
public function edit(OperatingDay $operatingDay)
{
return view('admin.day.form')->with('operatingDay', $operatingDay)->with('title', __('admin.day.edit_day'));
}
Route
Route::resource('days', 'OperatingDayController')->names([
'index' => 'admin.days.index',
'store' => 'admin.days.store',
'create' => 'admin.days.create',
'show' => 'admin.days.show',
'update' => 'admin.days.update',
'destroy' => 'admin.days.destroy',
'edit' => 'admin.days.edit',
]);
dd var in view
#dd($operatingDay)
App\OperatingDay {#1400 ▼
#fillable: array:2 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
#dd($operatingDay->id)
// output null
The problem is that you named your resource days therefore Laravel will try to route it to a model called Day, but your model is called OperatingDay, in order to route days to OperatingDay you can use explicit binding.
From the docs:
To register an explicit binding, use the router's model method to
specify the class for a given parameter. You should define your
explicit model bindings in the boot method of the RouteServiceProvider
class:
public function boot()
{
parent::boot();
Route::model('user', App\User::class);
}
So in your case it would look something like this:
use App\OperatingDay; // or whatever the namespace to your model is
use Illuminate\Support\Facades\Route;
public function boot()
{
parent::boot();
Route::model('day', OperatingDay::class);
}
If you only need this in one route you could also rename the route parameter:
From the docs:
By default, Route::resource will create the route parameters for your
resource routes based on the "singularized" version of the resource
name. You can easily override this on a per resource basis by using
the parameters method. The array passed into the parameters method
should be an associative array of resource names and parameter names:
Route::resource('users', 'AdminUserController')->parameters([
'users' => 'admin_user'
]);
So in your case it would be something like this:
Route::resource('days', 'OperatingDayController')
->names([
'index' => 'admin.days.index',
'store' => 'admin.days.store',
'create' => 'admin.days.create',
'show' => 'admin.days.show',
'update' => 'admin.days.update',
'destroy' => 'admin.days.destroy',
'edit' => 'admin.days.edit',
])
->parameters(['days' => 'operatingDay']);

Laravel Eager Loading is Null with Polymorphic Relations

In my model I have:
//UserAddress.php
protected $with = ['address'];
public function address()
{
return $this->morphOne('App\Address', 'addressable');
}
//User.php
public function userAddress()
{
return $this->hasOne(UserAddress::class);
}
//Address.php
public function addressable()
{
return $this->morphTo();
}
But when I call dd(auth()->user()->userAddress)) The address realtionship is not loaded. It is still null.
#attributes: array:4 [
"id" => "1"
"user_id" => "1"
"created_at" => "2018-11-13 10:11:54"
"updated_at" => "2018-11-13 10:11:54"
]
#original: array:4 [
"id" => "1"
"user_id" => "1"
"created_at" => "2018-11-13 10:11:54"
"updated_at" => "2018-11-13 10:11:54"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"address" => null
]
#touches: []
+timestamps: true
However, with dd(auth()->user()->userAddress()->get())) the relationship is eager loaded.
What is the problem?
Edit
It is super weird. If I write auth()->user()->userAddress->address I get null (it should not be that). And when I remove protected $with = ['address']; auth()->user()->userAddress->address gets me the correct model.
I have similar issue. It is loading the related models, but under different key.
I got a pivot model called Skillable it has a morphTo relationship to get the related morphed model.
public function morphedModel(): MorphTo
{
return $this->morphTo('skillable');
}
That works fine without eager loading when used like that
echo $skillable->morphedModel->id; // return some id
However when using eager loading, the morphedModel prop is null.
Skillable::with('morphedModel')->get()->first()->morhphedModel; // returns null
Turns out the eager loading is working almost as expected, however the related (morphed) models are loaded under skillable relationship instead.
dump(Skillable::with('morphedModel')->get()->first());
returns
...
#relations: array:2 [▶
"morphedModel" => null
"skillable" => App\Models\Lesson {#2376 ▶
Not sure why though.. something with my relation definition I guess.
I had the same issue with a one-to-one relationship where the id field was a string. Everything worked until I eager-loaded the connecting table (user_address in you case).
In my case I'd forgotten to define public $incrementing = false; in the connecting table, and once I had added this everything worked.

laravel eloquen relation not working on debian apache2 php5 server

i have develop a Laravel 5.2 application on my local windows10 PC using xampp with apache2 and php 7.0.3 as environment and everything works fine. i also have try it on xampp with php 5.3 installed, it run well. Now i would like to deploy the apps on debian 8 jessie server with apache2 and php 5.6 installed.
After i uploaded the apps, i got a problem every time i refer to a eloquent relation like in the code below
#if ($pegawai->list_pendidikan_pegawai->count() >0)
#foreach ($pegawai->list_pendidikan_pegawai as $pendidikan)
#if (($pendidikan->pendidikan_pendidikan != null) && ($pendidikan->status != 0))
<tr>
<td>{{ $i++ }}</td>
<td>{{$pendidikan->pendidikan_pendidikan->nama}}</td>
<td>{{$pendidikan->nama_tempat}}</td>
<td>{{$pendidikan->no_ijazah}}</td>
<td>{{$pendidikan->tgl_ijazah->format('d-m-Y')}}</td>
</tr>
#else
#endif
#endforeach
#endif
it returns
Trying to get property of non-object
based ont the example above, here is the riwayat_pendidikan model
class riwayat_pendidikan extends Model
{
protected $table = 'riwayat_pendidikan';
protected $fillable = ['pegawai', 'jenjang_pendidikan', 'pendidikan', 'nama_tempat', 'propinsi', 'kabkot', 'tempat', 'no_ijazah', 'tgl_ijazah', 'kepala', 'scan_ijazah', 'status'];
public function pendidikan_pendidikan()
{
return $this->belongsTo('App\ref_jenis_pendidikan', 'pendidikan', 'id');
}
}
and here is the master model
class master extends Model
{
protected $table = 'master';
protected $fillable = ['pegawai', 'sk_pengangkatan', 'tmt_pengangkatan', 'pendidikan', 'jabatan', 'jenis_kepegawaian', 'unor', 'unor_induk', 'scan_izin_prinsip', 'pejabat_pengangkat','status'];
public function list_pendidikan_pegawai()
{
return $this->hasMany('App\riwayat_pendidikan', 'pegawai', 'id');
}
}
Here is is the controller
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$pegawai = master::find($id);
return view('show.show_pegawai', compact('pegawai'));
}
here is the php --ini result on that debian server
/root$ php --ini
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File: /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed: /etc/php5/cli/conf.d/05-opcache.ini,
/etc/php5/cli/conf.d/10-pdo.ini,
/etc/php5/cli/conf.d/20-gd.ini,
/etc/php5/cli/conf.d/20-json.ini,
/etc/php5/cli/conf.d/20-mcrypt.ini,
/etc/php5/cli/conf.d/20-mysql.ini,
/etc/php5/cli/conf.d/20-mysqli.ini,
/etc/php5/cli/conf.d/20-pdo_mysql.ini,
/etc/php5/cli/conf.d/20-readline.ini
I have no idea why it got an error on different environment. Any help you can throw my way would be great. I'm very stuck with this.
Here is the dd($pegawai) result
master {#407 ▼
#table: "master"
#fillable: array:11 [▶]
#dates: array:1 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:15 [▶]
#original: array:15 [▼
"id" => 2
"pegawai" => 2
"pendidikan" => 0
"sk_pengangkatan" => "123644747hhhh"
"tmt_pengangkatan" => "2016-06-01"
"jabatan" => 27
"jenis_kepegawaian" => 3
"unor" => 992
"unor_induk" => 28
"pejabat_pengangkat" => ""
"scan_izin_prinsip" => ""
"status" => 1
"deleted_at" => "2016-06-12 20:54:53"
"created_at" => "2016-06-07 07:19:04"
"updated_at" => "2016-06-12 13:54:53"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}

Is Laravels' DD helper function working properly?

I like to use the dd function to debug. This time when I use it to display a list of appointments, I can't see (no clickable arrow) the data in the attributes and original. I get the brackets [ …19] displayed instead, not sure why.
Collection {#3061 ▼
#items: array:548 [▼
0 => Appointment {#821 ▼
#table: "appointments"
#fillable: array:16 [ …16]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:19 [ …19]
#original: array:19 [ …19]
#relations: array:2 [ …2]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [ …1]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => Appointment {#822 ▶}
2 => Appointment {#823 ▶}
3 => Appointment {#824 ▶}
4 => Appointment {#825 ▶}
5 => Appointment {#826 ▶}
6 => Appointment {#827 ▶}
7 => Appointment {#828 ▶}
And later in the list, I can't even see inside an appointment (no arrows):
81 => Appointment {#902 ▶}
82 => Appointment {#903 ▶}
83 => Appointment {#904 ▶}
84 => Appointment {#905 ▶}
85 => Appointment {#906 …23}
86 => Appointment {#907 …23}
87 => Appointment {#908 …23}
88 => Appointment {#909 …23}
89 => Appointment {#910 …23}
90 => Appointment {#911 …23}
But when I use var_dump, my data looks fine :
array(548) {
[0]=>
array(21) {
["id"]=>
int(149)
["appointmenttype_id"]=>
NULL
["appointmentlocationtype_id"]=>
NULL
["appointment_start"]=>
object(Carbon\Carbon)#812 (3) {
["date"]=>
string(26) "2015-12-21 07:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "America/New_York"
}
["appointment_end"]=>
object(Carbon\Carbon)#811 (3) {
["date"]=>
string(26) "2015-12-21 09:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "America/New_York"
}
Anyone else experienced that situation?
This is a not-so-known caveat of returning too large a list of results. Generally, dd() is meant to be a quick overview of the data you are returning, and handles "drill down" well on smaller groups of data. Once you reach a certain number (I forget the exact one, 500 maybe?), that functionality no longer works.
If you absolutely need to see this data before using it in your code somewhere, use a limit() clause before you get() the results, or use dd($example[0]) to see a single results' details. Hope that helps!
Not a bug in dd() just how the underlying Sympony VarDumper is configured.
I believe the line in question is this one which has sets $maxDepth to 20 but I haven't checked this.
Looking at the Laravel Dumper logic there doesn't seem to be anyway to override this from within Laravel.
I've found a way to work around this, although not recommended, if you really want the entire object dumped, add the following code snippet to /bootstrap/autoload.php
if (! function_exists('dd')) {
/**
* Dump the passed variables and end the script.
*
* #param mixed
* #return void
*/
function dd()
{
array_map(function ($x) {
$dumper = 'cli' === PHP_SAPI ? new \Symfony\Component\VarDumper\Dumper\CliDumper() : new \Illuminate\Support\Debug\HtmlDumper();
$cloner = new \Symfony\Component\VarDumper\Cloner\VarCloner();
$cloner->setMaxItems(-1);
$cloner->setMaxString(-1);
$dumper->dump($cloner->cloneVar($x));
}, func_get_args());
die(1);
}
}
This must be added above the line:
require DIR.'/../vendor/autoload.php';
It overrides the laravel dd function and sets 'setMaxItems' and 'setMaxString' on the Symfony VarCloner object before dumping.

Resources