【问题标题】:How to print a table with data being displayed in column format in Laravel?如何在 Laravel 中打印数据以列格式显示的表格?
【发布时间】:2018-12-15 05:43:09
【问题描述】:

以下是我的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 代码中的格式。

【问题讨论】:

  • 此外,我认为这里传递的数据没有任何问题,因为我尝试传递列标题的数据,即 todo,并且能够在视图中打印它们。
  • 我不知道 laravel,但是$todo=DB::table('todos')-&gt;select('status') 有一个-&gt;select(),但$todo1=DB::table('todos') 没有,有什么原因吗?根据您的错误消息,我认为这是因为$todo1-&gt;summary
  • @Sean 因为我在这里使用了 group by 子句,所以我需要在 group by 中使用的列上使用 select 语句,这是我在这里使用它的唯一原因。

标签: php html laravel laravel-5


【解决方案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>

【讨论】:

  • 谢谢,我想用相同的名字检索它们是这里的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 2013-06-23
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
相关资源
最近更新 更多