在控制器中循环图像

时间:2018-05-18 02:12:53

标签: laravel laravel-5.6

我在控制器中的代码:

select t.*
from work.test t
where not exists (select 1
                  from work.test t2
                  where t2.id = t.id and
                        t2.event_datetime > t.event_datetime - interval '30 minute' nd
                        t2.event_datetime < t.event_datetime
                 );

问题: 我无法从数据库循环图像,请帮助我。

2 个答案:

答案 0 :(得分:2)

您的代码中存在一些问题

  1. 您的查询是正确的,但您不需要访问$ a [0]您已检索到每个对象都有'file_name'作为属性的对象列表,因此可以通过$ a-&gt; file_name直接访问它。 / LI>
  2. {{}}是为刀片模板引擎设计的语法(即.blade.php文件),您不能在普通的.php文件中使用它们。
  3. 当laravel以laravel方式编码时,当你拥有'request()'帮助时,不要使用本机全局php变量$ _GET和$ _POST。
  4. 您的查询暴露于SQL注入,因此不要使用原始SQL查询,除非您通过预准备语句包围它们。

    public function image_item_name($inc) 
    {
    if(isset(request('inc'))) {
      $inc = request('inc');
      $i = DB::select("SELECT file_name FROM tbl_image_item_name WHERE inc = ?;",[$inc]);
    
    foreach ($i as $a){ 
    
        echo '<img src="../../'.$a->file_name.'">';
    
     }
    }  
    else {
           echo "Access Denied";
     }
    

答案 1 :(得分:-2)

尝试

$images = DB::table('tbl_image_item_name')->where('inc', $inc)->get();

foreach ($images as $image){ 

          echo '<img src="../{{ $image->file_name }}">';

        }

或雄辩的方式

$images = YourModelName::where('inc', $inc)->get();