ErrorException尝试在视图中显示数据时出现未定义的变量

时间:2019-06-29 10:22:42

标签: php laravel

所以我试图用laravel显示数据库中的记录,并且我已经在@foreach语句中定义了变量,但是当我运行它时,它显示了ErrorException Undefined variable,尽管所有变量都已经在foreach内语句,我是否在控制器中缺少方法函数?

这是视图 welcome.blade.php

<div class="blog-item">       
                        <div class="blog-text text-box text-white">
                            @foreach ($guestbooks as $guestbook)    
                            <div class="top-meta">{{ Carbon\Carbon::parse($guestbook->created_at)->format('d-m-Y')  }} /  di <a href="">Rakitan</a></div>
                            <h3>{{ $guestbooks->name }}</h3>
                            <p>{!! \Illuminate\Support\Str::words($guestbook->message, 50, '...') !!}</p>
                            <a href="{{ route('post.read', ['post_id' => $post->id]) }}" class="read-more">Lanjutkan Baca  <img src="asset/img/icons/double-arrow.png" alt="#"/></a>
                        </div>
                    </div>
                    <!-- Blog item -->
                    <div class="blog-item">       
                        <div class="blog-text text-box text-white">   
                            <div class="top-meta">{{ Carbon\Carbon::parse($guestbook->created_at)->format('d-m-Y')  }} /  di <a href="">Rakitan</a></div>
                            <h3>{{ $guestbook->name }}</h3>
                            <p>{!! \Illuminate\Support\Str::words($guestbook->message, 50, '...') !!}</p>
                            <a href="{{ route('post.read', ['post_id' => $post->id]) }}" class="read-more">Lanjutkan Baca  <img src="asset/img/icons/double-arrow.png" alt="#"/></a>
                        </div>
                    </div>
                    <!-- Blog item -->
                    <div class="blog-item">       
                        <div class="blog-text text-box text-white">   
                            <div class="top-meta">{{ Carbon\Carbon::parse($guestbook->created_at)->format('d-m-Y')  }} /  di <a href="">Rakitan</a></div>
                            <h3>{{ $guestbook->name }}</h3>
                            <p>{!! \Illuminate\Support\Str::words($guestbook->message, 50, '...') !!}</p>
                            <a href="{{ route('post.read', ['post_id' => $post->id]) }}" class="read-more">Lanjutkan Baca  <img src="asset/img/icons/double-arrow.png" alt="#"/></a>
                            @endforeach
                        </div>
                    </div>

这是控制器 GuestbookController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Guestbook;

class GuestbookController extends Controller
{
    public function index()
    {
        $guestbooks = Guestbook::get();
        return view('post.post_textarea',[
            'guestbooks' => $guestbooks
        ]);
    }

    public function store(Request $request)
    {
        Guestbook::create([
            'name' => $request->name,
            'message' => $request->message
        ]);
        return redirect()->back();
    }
}

这是路线

Route::get('/posting','GuestbookController@index')->name('guestbook');
Route::post('/posting','GuestbookController@store')->name('guestbook.store');

2 个答案:

答案 0 :(得分:0)

您的GuestbookController函数index()返回一个名为post.post_textarea的视图,并将$ guestbook变量传递给该视图,而您尝试在welcome.blade.php中获取该变量

更改索引函数以返回欢迎视图,如下所示:

public function index()
    {
        $guestbooks = Guestbook::get();
        return view('welcome',[
            'guestbooks' => $guestbooks
        ]);
    }

答案 1 :(得分:0)

我发现问题在于,有两条路由路由到Route::get('/posting','GuestbookController@index')->name('guestbook');所在的url中,因此我删除了另一条路由,这要归功于错别字的@DenisĆerić