【问题标题】:undefined variable inside view laravel视图laravel内部未定义的变量
【发布时间】:2019-12-17 10:25:58
【问题描述】:

所以我试图从我的数据库中获取数据到我从 getbootstrap 获得的数据表中

但我得到了错误:

未定义变量:问题(查看:C:\Users...\resources\views\dashboard.blade.php)

下面我列出了我使用变量的代码:问题

dashboard.blade.php

<table id="datatable" class="table table-bordered table-striped table-dark">
<thead>
<tr>
    <th scope="col">#</th>
    <th scope="col">Issue</th>
    <th scope="col">begrootte tijd</th>
    <th scope="col">beschrijving</th>
    <th scope="col">Action</th>
</tr>
</thead>
<tfoot>
<tr>
    <th scope="col">#</th>
    <th scope="col">Issue</th>
    <th scope="col">begrootte tijd</th>
    <th scope="col">beschrijving</th>
    <th scope="col">Action</th>
</tr>
</tfoot>
<tbody>
@foreach($issue as $issues)
    <tr>
        <th> {{ $issues->id }} </th>
        <th> {{ $issues->iname }} </th>
        <th> {{ $issues->begroting }} </th>
        <th> {{ $issues->description }} </th>
        <th>
            <a href="" class="btn btn-success"> START</a>
            <a href="" class="btn btn-danger"> STOP</a>

        </th>

    </tr>
@endforeach
</tbody>

DashboardController.php

    <?php

namespace App\Http\Controllers;

use App\Dashboard;
use Illuminate\Http\Request;


class DashboardController extends Controller
{
    public function store(Request $request)
    {
        $this->validate($request,[
            'iname' => 'required',
            'begroting' => 'required',
            'description' => 'required',

        ]);

        $issue = new Issue;

        $issue->iname = $request->input('iname');
        $issue->begroting = $request->input('begroting');
        $issue->description = $request->input('description');

        $issue->save();

        return redirect('/issue')->with('success', 'Issue opgeslagen');



    }

}

型号 dashboard.php

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Dashboard extends Model
{
    protected $table = 'issue';
}

【问题讨论】:

  • 您要将$issue 发送到哪里查看?
  • /issue 执行哪个操作。
  • 上面的代码不会将问题发送到视图。这是正确的代码,还是您没有阅读文档?此外,issue 是单数 (enkelvoudig),issues 是复数 (meervoud)。 foreach 循环应该是@foreach ($issues as $issue)

标签: php laravel variables view


【解决方案1】:

您应该将变量传递给视图。

假设您有一个 HomeController,并希望将一些变量从控制器传递给视图。

HomeController.php

public function show()
{
    $someVariable = "An Awesome Example";
    return view('example', [
        'someVariable' => $someVariable,
    ]);
}

example.blade.php

<b>{{ $someVariable }}</b>

您必须将数据传递给视图。然后在您的视图中,您可以向用户显示该数据。在我的示例中,我创建了一个带有键 someVariable 的数组,并将 $someVariable 传递给该键的值。

在我的视图中,我可以使用键来显示值。

【讨论】:

    【解决方案2】:

    您没有将 $issues 发送到刀片页面,请像这样更改您的 DashboardController:

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Dashboard;
    use Illuminate\Http\Request;
    
    
    class DashboardController extends Controller
    {
          public function store(Request $request)
          {
               $this->validate($request,[
                  'iname' => 'required',
                  'begroting' => 'required',
                  'description' => 'required',
    
               ]);
    
              $issue = new Issue;
    
              $issue->iname = $request->input('iname');
              $issue->begroting = $request->input('begroting');
              $issue->description = $request->input('description');
    
              $issue->save();
    
              $issues=Dashboard::all();
              return redirect('/issue')->with('success', 'Issue opgeslagen')
                                       ->with('issues',$issues);
    
    
    
         }
    
     }
    

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多