未定义的变量:帖子(查看:/home/.../annonces/resources/views/welcome.blade.php)
Undefined variable: posts (View: /home/.../annonces/resources/views/welcome.blade.php)
你好我有问题我尝试在 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');
但我有这个错误:
Undefined variable: posts (View:
/home/savinov/annonces/resources/views/welcome.blade.php)
不明白为什么我在控制器中定义他时它是未定义的变量,
如果可能,有人可以帮助我吗?
你试试下面的选项
选项 1
public function index()
{
$posts = DB::table('posts')->orderBy('created_at', 'DESC')->paginate(10);
return view('welcome', ['posts' => $posts]);
}
选项2
public function index()
{
$posts = Post::orderBy('created_at', 'DESC')->paginate(10);
return view('welcome',compact('posts'));
or
return view('welcome', ['posts' => $posts]);
}
你好我有问题我尝试在 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');
但我有这个错误:
Undefined variable: posts (View: /home/savinov/annonces/resources/views/welcome.blade.php)
不明白为什么我在控制器中定义他时它是未定义的变量, 如果可能,有人可以帮助我吗?
你试试下面的选项
选项 1
public function index()
{
$posts = DB::table('posts')->orderBy('created_at', 'DESC')->paginate(10);
return view('welcome', ['posts' => $posts]);
}
选项2
public function index()
{
$posts = Post::orderBy('created_at', 'DESC')->paginate(10);
return view('welcome',compact('posts'));
or
return view('welcome', ['posts' => $posts]);
}