简体   繁体   English

ErrorException未定义的变量:发布

[英]ErrorException Undefined variable: post

ok so I wanted to fetch a post from my database with my controller and then show it in my view in welcome.blade.php but the message keep showing ErrorException : 好的,所以我想使用控制器从数据库中获取帖子,然后在我的视图中的welcome.blade.php显示该帖子,但该消息ErrorException显示ErrorException

Undefined variable: post

I guess the problem is in how I write my code to get the getIndex function in my routes, so I've been trying to change that but I keep getting the same result. 我猜问题出在我如何编写代码以获取路由中的getIndex函数的问题,所以我一直在尝试更改它,但我一直得到相同的结果。

so this is the line in welcome.blade.php : 所以这是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 -->

this is the controller PostController 这是控制器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]);
        }
    }

and this is the routes file web.php 这是路由文件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');

You are setting the meta in the HTML before you are defining post in your for loop. 在for循环中定义post之前,您需要在HTML中设置meta。

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM