doupuchen6378 2018-12-13 11:02
浏览 146
已采纳

Laravel 5.7我的模型从我的参数返回为空时我转储它

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 = [];

  • 写回答

1条回答 默认 最新

  • dongmie3987067 2018-12-13 12:52
    关注
    $productcat->Productserie
    

    What is the type of Productserie ? Is this the primary key or a category name (text) ?

    Also can you confirm if your spellings are correct? To me it should be Productseries. But possibly you have a typo mistake in your database otherwise laravel must have thrown an exception of unknown column.

    function shopCat(Cat $cat) 
    

    I am assuming that you are posting category name in URL. If so then Laravel may be unable to convert the category name into the related Cat model as parameter to your method. Instead it might be passing null in $cat to that method

    Try to debug dd($cat) and see what do you receive in $cat .

    if $cat is null, then your following where clause will become false. so it will return null.

    $query->where('Productserie', '=', $cat);
    

    Solution

    if dd shows that $cat is a Cat model . then change your where clause to the following:

    $query->where('Productserie', '=', $cat->Productserie);
    

    but if dd shows $cat as string, then change your method signature something similar to this:

    public function shopCat(String $cat)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?