我在数据库中有一些引号,并且正在尝试使用Laravel在页面上显示它。我将引号从控制器中拉出,然后传递给视图,但出现错误:
Undefined variable: quotes (View: C:\laragon\www\resources\views\inc\quote.blade.php) (View: C:\laragon\www\\resources\views\inc\quote.blade.php) (View: C:\laragon\www\\resources\views\inc\quote.blade.php
QuotesController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class QuotesController extends Controller
{
public function index()
{
$quotes = DB::table('quotes')->get();
return view('inc.quote', ['quotes' => $quotes]);
}
}
quote.blade.php
<div id="MarketingQuote" class="container-fluid padding bg-light">
<div class="row">
<div class="col-lg-6">
<h2>Marketing Quote of the day</h2>
@foreach($quotes->all() as $quote)
<li>{{$quote}}</li>
@endforeach
<br>
</div>
<div class="col-lg-6">
<img src="img/computer.jpg" class="image-fluid" alt="">
</div>
</div>
</div>
行情迁移表
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateQuotesTable extends Migration
{
public function up()
{
Schema::create('quotes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('quote');
$table->string('author');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('quotes');
}
}
为什么会出现此错误?而且如何一次只显示一个报价,并且每次刷新时都只更改一次?
谢谢。
答案 0 :(得分:1)
我建议您在控制器中输入变量$ quotes,并且我记得传递的变量数组键可以使用而不必使用所有功能。
laravel手册中的以下代码:
Route::get('greeting', function () {
return view('welcome', ['name' => 'Samantha']);
});
Hello, {{ $name }}.
更改
@foreach($quotes->all() as $quote)
<li>{{$quote}}</li>
@endforeach
进入
@foreach($quotes as $quote)
<li>{{$quote}}</li>
@endforeach