Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 插入图像的最佳方式是什么_Laravel_Laravel Blade_Laravel 5.6 - Fatal编程技术网

Laravel 插入图像的最佳方式是什么

Laravel 插入图像的最佳方式是什么,laravel,laravel-blade,laravel-5.6,Laravel,Laravel Blade,Laravel 5.6,我用的是拉威尔。说到网站的加载时间,我想知道哪种方法更适合添加图像。假设我在每个帖子上传大约10张图片。这也许很愚蠢,但这里有两个想法 在名为“image”“image1”的表中创建一个字段,依此类推,直到“image10” 为图像存储创建一个单独的表,其中“post_id”是与图像表的关系 所以我想知道在运行查询方面哪个会慢一些。例如,我有这个查询 $id = Post::first(); Image::where(“post_id”, “=“, $id); 在exmaple中,我从与帖子相

我用的是拉威尔。说到网站的加载时间,我想知道哪种方法更适合添加图像。假设我在每个帖子上传大约10张图片。这也许很愚蠢,但这里有两个想法

  • 在名为“image”“image1”的表中创建一个字段,依此类推,直到“image10”
  • 为图像存储创建一个单独的表,其中“post_id”是与图像表的关系 所以我想知道在运行查询方面哪个会慢一些。例如,我有这个查询

    $id = Post::first();
    Image::where(“post_id”, “=“, $id);
    
    在exmaple中,我从与帖子相关的图像表中获取所有图像

    现在是第二个查询

    $second = Post::findOrFail($id);
    
    foreach
    在我们的刀片视图中循环它,并拍摄图像1到图像10。 在查询方面哪一个更好

    现在,我在最近的构建中注意到,处理应用程序中的一些查询需要一些时间,处理请求大约需要250毫秒,下载内容需要15毫秒。我缩短了一些查询并优化了代码/查询,使其处理时间达到150毫秒

    哪种查询需要更短的处理时间,哪种查询有助于缩短加载时间

    注意:这完全是一种想法,上面使用的示例是实时编写的,并不反映任何实际的项目编码。

    您正在寻找的

    在post模型中,您可以定义图像关系来告诉Laravel如何查找图像。使用单独的图像表是关联每篇文章中任意数量图像的最佳方法

    在Post.php类中:

    public function images()
    {
        return $this->hasMany(Image::class, 'post_id');
    }
    
    public function images()
    {
        return $this->hasMany(Image::class, 'post_id');
    }
    
    然后替换第一个示例,您可以执行以下操作

    $post = Post::first();
    $images = $post->images; // this will be an array of all the images associated with that post.
    
    既然你是在问性能问题,你应该向上看。基本上,一旦定义了关系,您就可以告诉laravel加载所有相关记录

    $posts = Post::with('images')->get();
    
    这行代码将获得所有帖子以及与每篇帖子相关联的所有图像。它将运行两个查询;一个获取所有帖子,一个获取所有图片

    SELECT * FROM POSTS
    SELECT * FROM IMAGES where post_id IN (results from above query)
    
    然后,您可以访问foreach循环中的所有图像,而无需担心其他查询

    @foreach($posts as $post) 
        {{ $post->images }}
    @endforeach
    

    最好使用第二个选项: 为图像存储创建一个单独的表,其中“post_id”是与图像表的关系

    和使用 在Post.php类中:

    public function images()
    {
        return $this->hasMany(Image::class, 'post_id');
    }
    
    public function images()
    {
        return $this->hasMany(Image::class, 'post_id');
    }
    
    把他们带回来
    因为通过这种方式,您可以在数据库中无限地更新、删除或插入新图像,如果您有多个图像,则可以制作不同的模型

    Post.php

    public function images()
    {
        return $this->hasMany(Image::class, 'post_id');
    }
    
    Image.php

    public function post()
    {
        return $this->belongsTo(Post::class, 'post_id');
    }
    
    这是最好的办法
    这允许您保存多张图像,无论您有多少张图像

    这正是我想要的答案。基本上,我的问题是查询是否会提供加载时间。但答案解释得太好了。非常感谢:)