Reputation: 314
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
:
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.
so this is the line in 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
<?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
<?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');
Upvotes: 1
Views: 1237
Reputation: 18946
You are setting the meta in the HTML
before you are defining post
in your for loop.
@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
Upvotes: 2