如何在Laravel中打印以列格式显示数据的表?

时间:2018-12-15 05:43:10

标签: php html laravel laravel-5

以下是我的php代码:

<table class="table table-striped">
<tr>
<?php

for ($i=1;$i<=8;$i++)
{
echo "<td>";
echo "<b>Col-".$i."</b><br>";
for ($j=1;$j<=15;$j++)
{

echo "Row-".$j."<br>";
}
echo "</td>";
}

?>
</tr>
</table>

因此,这基本上是在列下方按行打印每个行标题的数据值。我想在Laravel视图中实现相同的数据,其中数据将从控制器传递到视图。

我尝试使用以下代码实现相同的目标:

查看:

<form action='/display' method="post">
<table style="table-layout: fixed; width:100%;" border=1>
 {!! csrf_field() !!}
 <thead>
   <tr>
     @foreach($todo as $todo)
     <td>
       {{$todo->status}}
       @foreach($todo1 as $todo1)
       <table>
        <tr>
            <td>
            {{$todo1->summary}} 
            </td>
        </tr>

       </table>
       @endforeach
     </td>
     @endforeach
   </tr>
 </thead>
 </table>

控制器:

 <?php

namespace App\Http\Controllers;
use App\Todo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

//use Illuminate\Database\MySqlConnection;
class TodoController extends Controller
{
    public function index()
{

  $todo=DB::table('todos')->select('status')->groupBy('status')->get();
  //dd($todo);
  $todo1=DB::table('todos')->where('status','todo')->get();
  //dd($todo1);
  return view ('display',compact('todo1','todo'));
}
}

但是我一直收到此错误:

ErrorException

Trying to get property of non-object (View: lar4/resources/views/display.blade.php)

关于在这里需要做什么的任何建议,我只希望表的格式像我一开始所描述的php代码中的格式。

1 个答案:

答案 0 :(得分:1)

Trying changing your view

    <form action='/display' method="post">
     <table style="table-layout: fixed; width:100%;" border=1>
     {!! csrf_field() !!}
      <thead>
      <tr>
      @foreach($todo as $todo_new)
      <td>
        {{$todo_new->status}}
        @foreach($todo1 as $todo1_new)
        <table>
         <tr>
            <td>
            {{$todo1_new->summary}} 
            </td>
         </tr>

        </table>
        @endforeach
      </td>
      @endforeach
    </tr>
   </thead>
   </table>