Open dipaksarkar opened 2 weeks ago
If you're closing a bug report, you should mention why you're closing it.
@stancl, I’ve been investigating the issue further. It was working initially, but the problem has resurfaced. I found that the lastLogin
relationship works when I remove the ->where('type', 'login')
condition. However, with the condition in place, it still doesn't load, so the issue persists. I’ll need to reopen this. :(
<?php
namespace App\Models;
use Coderstm\Models\Log;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
// Working correctly
public function logs()
{
return $this->morphMany(Log::class, 'logable');
}
// Not Loading with tenancy package
public function lastLogin()
{
return $this->morphOne(Log::class, 'logable')
->where('type', 'login') // works without this condition
->latestOfMany();
}
}
@stancl I tried another solution, but it’s not working as expected. Both are working without tenancy package.
<?php
namespace App\Models\Tenant;
use Coderstm\Models\Log;
use App\Models\Central\User;
use Illuminate\Support\Facades\DB;
use App\Tenancy\Contracts\Syncable;
use Coderstm\Models\Admin as Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Stancl\Tenancy\Database\Concerns\ResourceSyncing;
class Admin extends Model implements Syncable
{
use ResourceSyncing;
protected $guarded = [];
protected $fillable = [
'first_name',
'last_name',
'email',
'status',
'password',
'phone_number',
'is_supper_admin',
'is_instructor',
'is_active',
'global_id',
'rfid',
];
protected $appends = [
'name',
'guard',
];
protected $casts = [
'email_verified_at' => 'datetime',
'is_active' => 'boolean',
'is_instructor' => 'boolean',
'is_supper_admin' => 'boolean',
];
public function logs(): MorphMany
{
return $this->morphMany(Log::class, 'logable');
}
public function getGlobalIdentifierKey()
{
return $this->getAttribute($this->getGlobalIdentifierKeyName());
}
public function getGlobalIdentifierKeyName(): string
{
return 'global_id';
}
public function getCentralModelName(): string
{
return User::class;
}
public function getCentralModelFillable(): array
{
return (new User)->getFillable();
}
public function getSyncedAttributeNames(): array
{
return [
'first_name',
'last_name',
'email',
'password',
];
}
public static function booted()
{
parent::booted();
static::addGlobalScope('last_login_at', function (Builder $builder) {
$builder->withCount([
'logs as last_login_at' => function (Builder $query) {
$query->select(DB::raw("MAX(created_at) as max_created_at"))->whereType('login');
},
]);
});
}
}
It's working when used orderBy('created_at', 'desc'
instead of latestOfMany()
public function lastLogin(): MorphOne
{
return $this->morphOne(Log::class, 'logable')
->where('type', 'login')
->orderBy('created_at', 'desc'); // change latestOfMany();
}
Would need more information to know how exactly this is related to the package.
Bug description
I am experiencing an issue with the
stancl/tenancy
package where themorphOne
relationship does not load as expected, while themorphMany
relationship works correctly. This issue occurs when themorphOne
relationship is used in conjunction with thestancl/tenancy
package.Code Example
Here is a simplified version of the
Admin
model that illustrates the problem:In the example above:
logs
relationship, which is amorphMany
relationship, works correctly and loads the associatedLog
records.lastLogin
relationship, which is amorphOne
relationship, does not load when using thestancl/tenancy
package.Steps to reproduce
stancl/tenancy
package.Admin
model with themorphMany
andmorphOne
relationships as shown in the code example above.lastLogin
relationship in a tenant context.Expected behavior
The
lastLogin
relationship should load the latestLog
record of typelogin
for theAdmin
model, similar to how thelogs
relationship loads the associatedLog
records.Laravel version
11.x
stancl/tenancy version
3.8