Skip to content
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

morphOne Relationship Not Loading #1247

Open
dipaksarkar opened this issue Aug 26, 2024 · 5 comments
Open

morphOne Relationship Not Loading #1247

dipaksarkar opened this issue Aug 26, 2024 · 5 comments
Assignees

Comments

@dipaksarkar
Copy link

Bug description

I am experiencing an issue with the stancl/tenancy package where the morphOne relationship does not load as expected, while the morphMany relationship works correctly. This issue occurs when the morphOne relationship is used in conjunction with the stancl/tenancy package.

Code Example

Here is a simplified version of the Admin model that illustrates the problem:

<?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')
                    ->latestOfMany(); 
    }
}

In the example above:

  • The logs relationship, which is a morphMany relationship, works correctly and loads the associated Log records.
  • The lastLogin relationship, which is a morphOne relationship, does not load when using the stancl/tenancy package.

Steps to reproduce

  1. Set up a Laravel project with the stancl/tenancy package.
  2. Define the Admin model with the morphMany and morphOne relationships as shown in the code example above.
  3. Attempt to load the lastLogin relationship in a tenant context.

Expected behavior

The lastLogin relationship should load the latest Log record of type login for the Admin model, similar to how the logs relationship loads the associated Log records.

Laravel version

11.x

stancl/tenancy version

3.8

@dipaksarkar dipaksarkar added the bug Something isn't working label Aug 26, 2024
@dipaksarkar dipaksarkar changed the title morphOne Relationship Not Loading with stancl/tenancy Package morphOne Relationship Not Loading Aug 26, 2024
@stancl
Copy link
Member

stancl commented Aug 26, 2024

If you're closing a bug report, you should mention why you're closing it.

@dipaksarkar
Copy link
Author

@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(); 
    }
}

@dipaksarkar dipaksarkar reopened this Aug 26, 2024
@dipaksarkar
Copy link
Author

dipaksarkar commented Aug 26, 2024

@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');
                },
            ]);
        });
    }
}

@dipaksarkar
Copy link
Author

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();
}

@stancl
Copy link
Member

stancl commented Sep 10, 2024

Would need more information to know how exactly this is related to the package.

@stancl stancl removed the bug Something isn't working label Sep 10, 2024
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

4 participants
@stancl @dipaksarkar and others