2017-08-09 26 views
0

我有26个教室,我的想法是,在学校的平面图上,使用时填充SVG图像中的颜色的教室。动态SVG图像与Laravel 5.4 PHP - SQL

已经做到了,但我想我必须做26个查询sql,每个教室一个。 它是正确的还是可以有另一种方式?

控制器:

public function index() 
{ 
    $dataClassr = Classroom::all()->where('name_classroom', 'sistemas 1')->first(); 

    return view('welcome')->with('dataClassr', $dataClassr); 
} 

查看:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 545 532.5"> 
    <title>Recurso 3</title> 
    <g> 
     <polyline points="315 484 264 483.5 264 268.5 213 268.5 213 84.5 200 84.5 200 0.5 265 0.5 265 172.5 315 172.5" style="fill: #cecece;stroke: #000;stroke-miterlimit: 10"/> 
     <g> 
      // Conditional 
      @if($dataClassr->availability == 'not available') 
       <rect x="1" y="83.5" width="221" height="177" style="fill: #fc7;stroke: #111;stroke-miterlimit: 10;stroke-width: 2px"/> 
      @endif 

      <rect x="265" y="4.5" width="219" height="168" style="fill: #fff;stroke: #111;stroke-miterlimit: 10;stroke-width: 2px"/> 
      <rect x="315" y="172.5" width="229" height="359" style="fill: #fff;stroke: #111;stroke-miterlimit: 10;stroke-width: 2px"/> 
      <text transform="translate(52.96 176.84)" style="font-size: 23.11184310913086px;font-family: MyriadPro-Regular, Myriad Pro">Class<tspan x="48.3" y="0" style="letter-spacing: -0.009993016574206285em">r</tspan><tspan x="55.63" y="0">oom 1</tspan></text> 
      <text transform="translate(314.96 95.84)" style="font-size: 23.11184310913086px;font-family: MyriadPro-Regular, Myriad Pro">Class<tspan x="48.3" y="0" style="letter-spacing: -0.009993016574206285em">r</tspan><tspan x="55.63" y="0">oom 2</tspan></text> 
      <text transform="translate(374.96 365.84)" style="font-size: 23.11184310913086px;font-family: MyriadPro-Regular, Myriad Pro">Class<tspan x="48.3" y="0" style="letter-spacing: -0.009993016574206285em">r</tspan><tspan x="55.63" y="0">oom 3</tspan></text> 
     </g> 
    </g> 
</svg> 

在foreach,打印许多元素RECT

结果是 enter image description here

+0

不知道你的数据库设置等。所以你的classrom名称列是'name_classroom'? coudl你不是简单地称所有的价值是真实的。 Classroom :: all() - > where('FIELD_NAME','sistemas 1'true) - > get();然后这应该得到所有那些只是迭代他们 –

回答

-1

尝试和运行foreach语句你的观点来回应每一个s vg在$ dataClassr数组中。

0

不知道,但尝试为我的评论尝试改变first()get()

Classroom::all()->where('name_classroom', 'sistemas 1')->get();

由于第一,因为它规定得到的第一个结果,如果youwant他们都使用get()选项。