您有问题,我尝试在laravel 5.8中创建搜索栏。
welcome.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
@foreach ($posts as $post)
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="..." alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
@endforeach
</div>
@endsection
PostController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests\Poststore;
use App\Post;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\DB;
class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$posts = DB::table('posts')->orderBy('created_at', 'DESC')->paginate(10);
return view('welcome', compact('posts'));
}
public function create()
{
return view('posts.create');
}
public function store()
{
$data = request()->validate([
'title' => ['required', 'string'],
'image' => ['required', 'image'],
'price' => ['required', 'integer'],
'descriptionpost' => ['required', 'string']
]);
$imagePath = request('image')->store('uploads', 'public');
$image = Image::make(public_path("/storage/{$imagePath}"))->fit(1200, 1200);
$image->save();
auth()->user()->posts()->create([
'title' => $data['title'],
'descriptionpost' => $data['descriptionpost'],
'price' => $data['price'],
'image' => $imagePath
]);
return redirect()->route('profile.show', ['user' => auth()->user() ]);
}
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
}
路线
Route::get('/welcome', 'PostController@index')->name('posts.index');
但是我有这个错误:
未定义变量:帖子(查看: /home/savinov/annonces/resources/views/welcome.blade.php)
不理解为什么如果我在控制器中定义了他的未定义变量, 有人可以帮助我吗?