I'm using laravel 5.4 to develop a small tool that shall keep track of various objects along their lives. A single object is identified by it's uuid. Uuid is not unique as an object can have multiple versions.
Performance is not a must, as the number of object is small.
The database is more or less as follow : Database diagram
I want to be able to select all obj2 linked to a specific obj1 for the current version of a project like so:
$obj1->obj2s()->allForProjectLastRevision($project_id);
I also want to be able to select all obj1 for the current version of a project like that:
Obj1::allForProjectLastRevision($project_id);
And I have the following code for now.
<?php
abstract class VersionableModel extends Model
{
public function versions()
{
return $this->belongsToMany('App\Version');
}
public function scopeAllForProjectLastRevision($query, $project_id)
{
$query->whereHas('versions', function($query) use($project_id) {
$query->whereHas('project', function($query) use($project_id) {
$query->where('projects.id', $project_id)
->whereColumn ('projects.current_version_id', 'versions.id');
});
});
return $query->get();
}
}
class Obj1 extends VersionableModel
{
public function obj2s()
{
return $this->belongsToMany('App\Obj2');
}
}
class Version extends Model
{
public function project()
{
return $this->belongsTo('App\Project');
}
}
class Project extends Model
{
public function current_version()
{
return $this->belongsTo('App\Version', 'current_version_id');
}
}
Unfortunately the scopeAllForProjectLastRevision doesn't return the expected result.
The result is a list containing all the obj2s linked to a specific obj1 not limited to the current version of the project.
Am'I missing something?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire