Home > Mobile >  One relation not loading just after creation, but another one loads
One relation not loading just after creation, but another one loads

Time:10-31

I'm encountering a weird problem in my Laravel 9 project.

I have this model Team, with a logo relation to another model ImageVersion, and a city relation to a City model, they're defined like this:

public function city(): BelongsTo
{
    return $this->belongsTo(City::class);
}

public function logo(): BelongsTo
{
    return $this->belongsTo(ImageVersion::class, 'logo_image_version_id');
}

The problem is, when i'm creating a new Team, and setting both city_id and logo_image_version_id, only the city becomes available with $team->city, $team->logo stays null.

This is the code:

public function update(Team $team, CreateOrUpdateTeamDto $dto): Team
{
    if ($team->logo && $team->logo->image_id !== $dto->logo_image_id) {
        $this->imageService->deleteVersion($team->logo);
    }

    $team->name = $dto->name;
    $team->manager = $dto->manager;
    $team->location = $dto->location;
    $team->city_id = $dto->city_id;

    if ($dto->logo_image_id) {
        $version = $this->imageService->createVersionIfDoesntExistFromId($dto->logo_image_id, 512, 512, keepWhenUnused: false);
        $team->logo_image_version_id = $version->id;
    }

    $team->save();

    return $team;
}

public function create(CreateOrUpdateTeamDto $dto): Team
{
    $team = new Team();
    return $this->update($team, $dto);
}

If I do a GET request on the Team I just created, I can access logo just fine, so there's probably no problems with the relation definition itself, the problem just happens in the request that creates it.

I know that I could probably just do a $team->refresh() before returning it, but I find it weird that it works just fine for city.

These are the queries logged by the request that creates the Team:

select * from `images_versions` where `image_id` = ? and `width` = ? and `height` = ? and `mime_type` = ? limit 1;  
select * from `images` where `images`.`id` in (5);  
insert into `teams` (`name`, `manager`, `location`, `city_id`, `logo_image_version_id`, `created_by_id`, `updated_by_id`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?);  
select * from `cities` where `cities`.`id` = ? limit 1;  
select * from `states` where `states`.`id` in (6);  

CodePudding user response:

One way to fix this is after $team->logo_image_version_id = $version->id; try adding unset($team->logo); to remove the null $team->logo

I believe when you are calling

$team->logo

in

if ($team->logo && $team->logo->image_id !== $dto->logo_image_id) {
   $this->imageService->deleteVersion($team->logo);
}

if the $team object does not yet have a logo_image_version_id, just calling $team->logo will set the logo property to null. Then after when you add the logo_image_version_id $team->logo is still null. And does not re query the DB

CodePudding user response:

Is the field logo_image_version_id fillable ?

Are you sure $version->id is not null ?

Can you tell me what does $team->logo()->toSql() returns ?

  • Related