ErrorException未定义的变量:发布

时间:2019-06-15 11:50:04

标签: php laravel eloquent

好,所以我想使用控制器从数据库中获取帖子,然后在welcome.blade.php的视图中显示该帖子,但消息始终显示ErrorException

Undefined variable: post

我想问题出在我如何编写代码以在路由中获得getIndex函数,所以我一直在尝试更改它,但我一直得到相同的结果。

所以这是welcome.blade.php中的行:

<!-- Blog item -->
                    <div class="blog-item">
                        <div class="blog-thumb">
                            <img src="asset/img/blog/1.jpg" alt="">
                        </div>
                        <div class="blog-text text-box text-white">

                            <div class="top-meta"><small><i>{{ Carbon\Carbon::parse($post->created_at)->format('d-m-Y')  }} by <a href="#">{{ $post->name }}</a></i></small>/  di <a href="">Rakitan</a></div>

                            @foreach($posts as $post)
                            <h3 class="blog-post-title">{{ $post->title }}</h3>
                            <p>{!! \Illuminate\Support\Str::words($post->description, 30, '...') !!}</p>
                             <blockquote>
                            <p>
                            <a href="{{ route('post.read', ['post_id' => $post->id]) }}" class="btn btn-primary btn-sm">Learn more</a> </p>
                            </blockquote>
                            </div><!-- /.blog-post -->
                            @endforeach

                            <a href="{{ url('/2019') }}" class="read-more">Lanjutkan Baca  <img src="asset/img/icons/double-arrow.png" alt="#"/></a>
                        </div>
                    </div>
                    <!-- Blog item -->

这是控制器PostController

<?php
namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;

    class PostController extends Controller
    {
        public function getIndex() {
            $posts = DB::table('users')->leftjoin('posts', 'users.id', '=', 'posts.author')->paginate(2); 
            return view('welcome', ['posts' => $posts]);
        }
    }

这是路由文件web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', 'PostController@getIndex',function () {
    return view('welcome')->name('index');
});
Route::get('/', 'PostController@getIndex')->name('index');
Route::get('/2019', 'BlogController@blog');
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/author/post','HomeController@getPostForm')->name('post.form');
Route::post('/author/post', 'HomeController@createPost')->name('post.form');
Route::get('/author/post/detail/{id}', 'HomeController@getPost')->name('post.detail');
Route::get('/author/post/edit/{id}', 'HomeController@editPost')->name('post.edit');
Route::post('/author/post/edit/{id}', 'HomeController@updatePost')->name('post.update');
Route::get('/author/post/delete/{id}', 'HomeController@deletePost')->name('post.delete');

1 个答案:

答案 0 :(得分:2)

在for循环中定义HTML之前,您需要先在post中设置元。

@foreach($posts as $post)
    <!--moved meta in here-->
    <div class="top-meta"><small><i>{{ Carbon\Carbon::parse($post->created_at)->format('d-m-Y')  }} by <a href="#">{{ $post->name }}</a></i></small>/  di <a href="">Rakitan</a></div>
...
@endforeach