“未定义变量:书籍
"Undefined variable: books
我收到此错误:
"Undefined variable: books (View:
C:\xampp\htdocs\Shop\resources\views\welcome.blade.php)"
我无法在 Welcome.blade.php
中显示我的项目,因为它说我有一个未定义的变量。而且我找不到让它工作的方法。
这是我的模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
'name',
'description',
'condition',
'category_id',
'image'
];
public function category()
{
$this->belongsTo(Category::class);
}
}
这是我的 ProductsController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
use App\Product;
class ProductsController extends Controller
{
public function index()
{
$products = Product::all();
return view('admin.product.index',compact('products'));
}
public function create()
{
$categories = Category::pluck('name','id');
return view ('admin.product.create', compact('categories'));
}
public function store(Request $request)
{
$product = $request->except('image');
//validation
$this->validate($request,[
'name' => 'required',
'condition' => 'required',
'image' => 'image|mimes:png,jpg,jpeg|max:10000'
]);
//image upload
$image = $request->image;
if( $image ){
$imageName=$image->getClientOriginalName();
$image->move('images',$imageName);
$product['image']=$imageName;
}
Product::create($product);
return redirect()->route('admin.index');
}
}
这是我的Welcome.blade.php
<div class="row">
@forelse( $books as $book)
<div class="column-prod">
<a href="{{route('book')}}">
<div class="card-prod">
<center>
<img src="/css/aa.jpg" style="width: 300px; height: 300px;">
</center>
</a>
<div class="container-prod">
<h2></h2>
<p class="title">{{$book->name}}</p>
<p class="price">Php 450.00</p>
<p>
<button class="button">
<i class="fas fa-cart-arrow-down"></i>Add to Cart
</button>
</p>
<p>
<button class="button">
<i class="fas fa-heart"></i> Add to Wishlist
</button>
</p>
</div>
</div>
</div>
@empty
<h3> No Books </h3>
@endforelse
</div> <!--Div all end-->
@endsection
这是我的 web.php
,很抱歉我是新手...希望您能帮助我。
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/cart','ShopController@cart');
Route::get('/signin','ShopController@signin');
Route::get('/book','ShopController@book');
Route::get('/wishlist','ShopController@wish');
Auth::routes();
Route::get('/logout', 'Auth\LoginController@logout');
Route::get('/home', 'HomeController@index');
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function(){
Route::get('/',function(){
return view('admin.index');
})->name('admin.index');
Route::resource('product','ProductsController');
Route::resource('category','CategoriesController');
});
替换
Route::get('/', function () {
return view('welcome');
});
有
Route::get('/', function () {
return view('welcome')->with(['books' => \App\Books::all()]);
});
(如果可行,那么您应该像处理其他代码一样将代码移至控制器中)
替换
Route::get('/', function () {
return view('welcome');
});
和
Route::get('/', function () {
return view('welcome')->with(['books' => \App\Product::all()]);
});
我收到此错误:
"Undefined variable: books (View: C:\xampp\htdocs\Shop\resources\views\welcome.blade.php)"
我无法在 Welcome.blade.php
中显示我的项目,因为它说我有一个未定义的变量。而且我找不到让它工作的方法。
这是我的模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
'name',
'description',
'condition',
'category_id',
'image'
];
public function category()
{
$this->belongsTo(Category::class);
}
}
这是我的 ProductsController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
use App\Product;
class ProductsController extends Controller
{
public function index()
{
$products = Product::all();
return view('admin.product.index',compact('products'));
}
public function create()
{
$categories = Category::pluck('name','id');
return view ('admin.product.create', compact('categories'));
}
public function store(Request $request)
{
$product = $request->except('image');
//validation
$this->validate($request,[
'name' => 'required',
'condition' => 'required',
'image' => 'image|mimes:png,jpg,jpeg|max:10000'
]);
//image upload
$image = $request->image;
if( $image ){
$imageName=$image->getClientOriginalName();
$image->move('images',$imageName);
$product['image']=$imageName;
}
Product::create($product);
return redirect()->route('admin.index');
}
}
这是我的Welcome.blade.php
<div class="row">
@forelse( $books as $book)
<div class="column-prod">
<a href="{{route('book')}}">
<div class="card-prod">
<center>
<img src="/css/aa.jpg" style="width: 300px; height: 300px;">
</center>
</a>
<div class="container-prod">
<h2></h2>
<p class="title">{{$book->name}}</p>
<p class="price">Php 450.00</p>
<p>
<button class="button">
<i class="fas fa-cart-arrow-down"></i>Add to Cart
</button>
</p>
<p>
<button class="button">
<i class="fas fa-heart"></i> Add to Wishlist
</button>
</p>
</div>
</div>
</div>
@empty
<h3> No Books </h3>
@endforelse
</div> <!--Div all end-->
@endsection
这是我的 web.php
,很抱歉我是新手...希望您能帮助我。
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/cart','ShopController@cart');
Route::get('/signin','ShopController@signin');
Route::get('/book','ShopController@book');
Route::get('/wishlist','ShopController@wish');
Auth::routes();
Route::get('/logout', 'Auth\LoginController@logout');
Route::get('/home', 'HomeController@index');
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function(){
Route::get('/',function(){
return view('admin.index');
})->name('admin.index');
Route::resource('product','ProductsController');
Route::resource('category','CategoriesController');
});
替换
Route::get('/', function () {
return view('welcome');
});
有
Route::get('/', function () {
return view('welcome')->with(['books' => \App\Books::all()]);
});
(如果可行,那么您应该像处理其他代码一样将代码移至控制器中)
替换
Route::get('/', function () {
return view('welcome');
});
和
Route::get('/', function () {
return view('welcome')->with(['books' => \App\Product::all()]);
});