Laravel没有得到正确和相关的结果

Laravel没有得到正确和相关的结果,laravel,laravel-5.4,laravel-eloquent,Laravel,Laravel 5.4,Laravel Eloquent,我正在尝试为我的应用程序创建一个图像库,这样我就可以从我的LaravelAPI请求图像 这些图像根据它们在应用程序中的位置分为不同的类型,如“侧菜单”、“topbanner”等 某些图像可以采用其他属性,如“url”、“文本”等。这些属性将根据图像类型而有所不同。所以,“侧菜单”类型的图像可能具有一个名为“字体颜色”或类似的属性 我的数据库结构如下所示: 因此,我们的想法是获得所有具有image\u类型的图像,并带有特定的类似于slug的“侧菜单”。然后我想加入image\u type上的所有

我正在尝试为我的应用程序创建一个图像库,这样我就可以从我的LaravelAPI请求图像

这些图像根据它们在应用程序中的位置分为不同的类型,如“侧菜单”、“topbanner”等

某些图像可以采用其他属性,如“url”、“文本”等。这些属性将根据
图像类型而有所不同。所以,“侧菜单”类型的图像可能具有一个名为“字体颜色”或类似的属性

我的数据库结构如下所示:

因此,我们的想法是获得所有具有
image\u类型的图像
,并带有特定的类似于slug的“侧菜单”。然后我想加入
image\u type
上的所有
image\u type\u属性,最后加入它们的值

我已经将值和
image\u type\u属性
拆分到它们自己的表中,因此我可以使用这些属性作为模板,用户可以在上载图像时填写这些属性

这几乎是可行的,但我的代码/关系有问题,因为忽略了图像和图像\u值之间的关系,因此我最终得到的值只是属于数据库中给定的
image\u类型的第一个实例-这应该是与图像相关的值。
(现在,我在所有图像上为我的第一张图像输入了值)

这些是我的模型:

图像

class Image extends Model
{
    protected $table = 'images';


    public function attributes(){
        return $this->type->attributes();       
    }

    public function type(){
        return $this->belongsTo(ImageType::class, 'image_type_id');
    }

}
class ImageType extends Model
{
    protected $table = 'image_type'; 


    public function images(){
        return $this->hasMany(Image::class);
    }

    public function account(){
        return $this->belongsTo(Account::class);
    }


    public function attributes(){
        return $this->hasMany(ImageTypeAttribute::class);
    }

}
class ImageValue extends Model
{
    protected $table = 'image_value'; 

}
图像类型

class Image extends Model
{
    protected $table = 'images';


    public function attributes(){
        return $this->type->attributes();       
    }

    public function type(){
        return $this->belongsTo(ImageType::class, 'image_type_id');
    }

}
class ImageType extends Model
{
    protected $table = 'image_type'; 


    public function images(){
        return $this->hasMany(Image::class);
    }

    public function account(){
        return $this->belongsTo(Account::class);
    }


    public function attributes(){
        return $this->hasMany(ImageTypeAttribute::class);
    }

}
class ImageValue extends Model
{
    protected $table = 'image_value'; 

}
ImageTypeAttribute

class ImageTypeAttribute extends Model
{
    protected $table = 'image_type_attribute'; 


    public function value(){
        return $this->hasOne(ImageValue::class, 'image_type_attribute_id');
    }

}
ImageValue

class Image extends Model
{
    protected $table = 'images';


    public function attributes(){
        return $this->type->attributes();       
    }

    public function type(){
        return $this->belongsTo(ImageType::class, 'image_type_id');
    }

}
class ImageType extends Model
{
    protected $table = 'image_type'; 


    public function images(){
        return $this->hasMany(Image::class);
    }

    public function account(){
        return $this->belongsTo(Account::class);
    }


    public function attributes(){
        return $this->hasMany(ImageTypeAttribute::class);
    }

}
class ImageValue extends Model
{
    protected $table = 'image_value'; 

}
我的控制器:

// 1. Get all active images with some slug ex. "sidemenu" 
// and get the image-attributes and values as well

$images = Image::whereHas('type', function($query) use ($image_slug, $slug){
    $query->where('slug', $image_slug);
})
->with('attributes')
->where('active', 1)        
->get();

// 2. Loop through all images, and get the image attribute-names and values
$image_array = [];
foreach($images as $image){
    $image_array['image'] = $image->image;

    foreach($image->type->attributes as $attribute){
        $image_array[$attribute->name] = $attribute->value->value;
    }
}
return $image_array;

你知道我做错了什么吗?

你能给我们看看foreach循环上的
dd($images)
dd($attribute->value)
?另外,我认为您应该使用('attributes.value'
)急切地加载
值(
->with('attributes.value'
)我最终得到的是:(图像是正确的,但图像2上的值是错误的)
{image:“storage/images/599eb9b09b09c6f0.jpg”,标题:“header1”,链接:“第一个图像的url”},{image:“storage/images/599eb889a3d7c.jpg”,标题:“header1”,link:“第一个图像的url”}
我尝试添加with('attributes.value'),但与以前一样。我得到了一个值,但不是从哪个表中获得链接的正确值,以及所有这些-
dd($attribute->value)