I am trying to show every product with the same category. If I dump my Model (cat) it shows me this:
cat {#400 ▼
#guarded: []
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
I think thats why it returns nothing when I select every product with that category. Here is my productscontroller:
<?php
namespace App\Http\Controllers;
use DB;
use Illuminate\Http\Request;
use App\Product;
use App\cat;
class ProductsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function shopindex()
{
//Producten onlangs toegevoegd
$productsOTs = DB::table('productimages')->where('Afkorting', 'PPI')->orWhere('Productcode', '[0-9]+')->limit(3)->offset(83)->get();
//$productsOTs = DB::select(DB::raw("SELECT * FROM wiz.productimages WHERE Afkorting = 'PPI' LIMIT 83, 3"));
//Producten categorieën combobox
$productcats = DB::table('products')->distinct()->select('Productserie', 'Productserie')->get();
//$productcats = DB::select(DB::raw("SELECT DISTINCT Productserie FROM wiz.products"));
return view('shop', compact('productsOTs', 'productcats'));
}
public function productdetail(Product $product)
{
dd($product);
//return view('Products.productdetail', compact('productsOT'));
}
public function shopCat(Cat $cat)
{
// Combobox items Cats
$productcats = DB::table('products')->distinct()->select('Productserie')->get();
// Products from category
$categorieProds = DB::table('products')->where(function ($query) use ($cat) {
$query->where('Productserie', '=', $cat);
})->get();
dd($cat);
//return view('Products.allproducts', compact('productcats', 'categorieProds'));
}
}
My web.php :
Route::get('/shop', ['middleware' => 'auth', 'uses' => 'ProductsController@shopindex']);
//shop categorie
Route::get('/shop/products/{cat}', ['middleware' => 'auth', 'uses' =>'ProductsController@shopCat']);
My foreach from /shop:
<select class="form-control category" onchange="window.location=this.options[this.selectedIndex].value">
<option value="" disabled selected hidden>Categorieën</option>
@foreach ($productcats as $productcat)
<option value="/shop/products/{{ $productcat->Productserie }}">{{ $productcat->Productserie }}</option>
@endforeach
</select>
My model (cat.php) is almost empty, I thought maybe thats the problem. The only code that is in my model is: protected $guarded = [];