在Laravel 5.2中通过id显示图像文件

时间:2021-12-25 08:15:21

I have a table named files, this saves the name of the images related to the properties table.

我有一个名为files的表,它保存了与属性表相关的图像的名称。

I am trying to make these images appear as in the following relation.

我试图使这些图像呈现如下关系。

This is part of the properties table.

这是properties表的一部分。

在Laravel 5.2中通过id显示图像文件

This is the table files and their relationship to the properties table.

这是表文件及其与属性表的关系。

在Laravel 5.2中通过id显示图像文件

What parameter can I pass in the show method of my controller (PropertyController)?

我在控制器(PropertyController)的show方法中可以传递什么参数?

Currently I have the following:

目前我有以下几点:

public function show($id)
{
 $properties = Property::find($id);

 $files = File::all();

 return View::make('properties.show', ['properties' => $properties, 'files' => $files]);
}

But it returns to the view all the images stored in the files table.

但它返回到视图中存储在文件表中的所有图像。

@foreach($files as $file) 

    <div class="col-md-6 thumb">
        <a class="thumbnail">
            <img id="myImg" src="{{ URL::asset('uploads/products/' . $file->name) }}" alt="{{ $file->name }}" width="300" height="200">
        </a>
    </div>

@endforeach

Which method would be correct so that the images related to the records can be displayed by id in the properties table?

哪种方法是正确的,以便与记录相关的图像可以通过属性表中的id显示?

2 个解决方案

#1


2  

I'm assuming you're using hasMany() relationship between Property and File models. If not, create the relation in the Property model:

我假设您正在使用属性和文件模型之间的hasMany()关系。如果不是,在属性模型中创建关系:

public function files()
{
    return $this->hasMany('App\File');
}

To load the property with all it's images use eager loading:

要将所有的图像加载到属性中,请使用即时加载:

public function show($id)
{
    $property = Property::with('files')->find($id);
    return view('properties.show', compact('property'));
}

To display images:

显示图片:

@foreach($property->files as $file) 
    // Here use the same code you used before.
@endforeach

Alternatively, you can load data separately:

或者,您可以分别加载数据:

public function show($id)
{
    $property = Property::find($id);
    $files = File::where('property_id', $property->id)->get();
    return view('properties.show', compact('property', 'files'));
}

#2


0  

In your Property model, define your hasMany method like this:

在您的属性模型中,定义您的hasMany方法如下:

public function files()
{
    return $this->hasMany('App\File');
}

Your show method in your controller will be like this:

您的控制器中的show方法将如下所示:

public function show($id)
{
 $property = Property::find($id);

 $files = $property->files;

 return View::make('properties.show', ['property' => $property, 'files' => $files]);
}

#1


2  

I'm assuming you're using hasMany() relationship between Property and File models. If not, create the relation in the Property model:

我假设您正在使用属性和文件模型之间的hasMany()关系。如果不是,在属性模型中创建关系:

public function files()
{
    return $this->hasMany('App\File');
}

To load the property with all it's images use eager loading:

要将所有的图像加载到属性中,请使用即时加载:

public function show($id)
{
    $property = Property::with('files')->find($id);
    return view('properties.show', compact('property'));
}

To display images:

显示图片:

@foreach($property->files as $file) 
    // Here use the same code you used before.
@endforeach

Alternatively, you can load data separately:

或者,您可以分别加载数据:

public function show($id)
{
    $property = Property::find($id);
    $files = File::where('property_id', $property->id)->get();
    return view('properties.show', compact('property', 'files'));
}

#2


0  

In your Property model, define your hasMany method like this:

在您的属性模型中,定义您的hasMany方法如下:

public function files()
{
    return $this->hasMany('App\File');
}

Your show method in your controller will be like this:

您的控制器中的show方法将如下所示:

public function show($id)
{
 $property = Property::find($id);

 $files = $property->files;

 return View::make('properties.show', ['property' => $property, 'files' => $files]);
}