我的foreach不会显示任何内容,我不确定我是否正确传递了变量。从我的控制器中选择的作品,如果我在修补程序中使用它的作品。但在我看来,它不会显示。没有错误,只是一无所有。
我的网页:
//shop
Route::get('/shop', ['middleware' => 'auth', 'uses' => 'ProductsController@shopindex']);
//shop categorie
Route::get('/shop/products/{pcategorie}', ['middleware' => 'auth', 'uses' => 'ProductsController@shopCat']);
//shop product detail
Route::get('/shop/productdetail/{product}', 'ProductsController@productdetail');
我的控制器:
<?php
namespace App\Http\Controllers;
use DB;
use Illuminate\Http\Request;
use App\Product;
use App\pCategorie;
class ProductsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function shopindex()
{
$productsOTs = DB::select(DB::raw("SELECT * FROM wiz.productimages WHERE Afkorting = 'PPI' LIMIT 83, 3"));
$productcats = DB::select(DB::raw("SELECT DISTINCT Productserie FROM wiz.products"));
return view('shop', compact('productsOTs', 'productcats'));
}
public function productdetail(Product $Product)
{
return view('Products.productdetail', compact('productsOT'));
}
public function shopCat(pCategorie $pCategorie)
{
// Combobox items Cats
$productcats = DB::select(DB::raw("SELECT DISTINCT Productserie FROM wiz.products"));
// Products from category
$categorieProds = DB::select(DB::raw("SELECT * FROM products WHERE Productserie = '$pCategorie'"));
return view('Products.allproducts', compact('productcats', 'categorieProds'));
}
}
从我的角度来看,foreach:
@if (isset($categorieProds))
@foreach ($categorieProds as $categorieProd)
<li>test</li>
@endforeach
@else
<h1>Not found</h1>
@endif
我希望有人可以帮助我:)
答案 0 :(得分:3)
您不会在选择查询的末尾致电 get()
:
原始SQL是不必要的,最好使用适当的查询生成器方法。
让我们清理那些查询:
// instead of writing this
$productcats = DB::select(DB::raw("SELECT DISTINCT Productserie FROM wiz.products"));
// write this
$productcats = DB::table('products')->distinct()->select('Productserie')->get();
// bad
$categorieProds = DB::select(DB::raw("SELECT * FROM products WHERE Productserie = '$pCategorie'"));
// good
$categorieProds = DB::table('products')->where('Productserie', $pCategorie)->get();
答案 1 :(得分:-1)
使用->get()
方法重写查询。并且看起来$ pCategorie是对象,可能您应该使用其属性,例如$ pCategorie-> id。
public function shopCat(pCategorie $pCategorie)
{
// Combobox items Cats
$productcats = DB::table('wiz.products')
->select('Productserie')
->distinct()
->get();
// Products from category
DB::table('products')
->where('Productserie', $pCategorie)
->get();
return view('Products.allproducts', compact('productcats', 'categorieProds'));
}