【问题标题】:No query results for model [App\Product] 36模型 [App\Product] 没有查询结果 36
【发布时间】:2019-11-06 03:02:03
【问题描述】:

当我运行它时,它会提示并错误 [No query results for model [App\Product] 36]。

这是控制器代码。

public function  wishList(Request $request)
{        
    $products = DB::table('wishlist')->leftjoin('products', 'products.id' , '=', 'wishlist.pro_id')->get();
    return view('wishlist',compact('products'));
}

这是路线。

Route::get('addToWishList','UserProfileController@addWishList');
Route::get('/wishlist','UserProfileController@wishList');
Route::get('/removeWishList/{id}', 'UserProfileController@removeWishList');

这是页面界面代码。问题所在的错误鞋子来自这里。

<tbody>
@forelse($products as $product)
    <tr>
        <td><a href="{{url('/')}}/removeWishList/{{$product->id}}"><i class="fa fa-trash-o" aria-hidden="true"></i></a></td>
        <td>
            <img src="{{url('/assets/images/products')}}/{{\App\Product::findOrFail($product->pro_id)->feature_image}}" alt="">
        </td>
        <td>
            <a href="{{url('/product',$product->id)}}">
            {{$product->title}}
        </td>
        <td>
            <td>{{$settings[0]->currency_sign}}<span id="price{{$product->id}}">{{\App\Product::Cost($product->id)}}</span></td>
        </td>
        <td>
            <form class="addtocart-form">
                {{csrf_field()}}
                @if(Session::has('uniqueid'))
                    <input type="hidden" name="uniqueid" value="{{Session::get('uniqueid')}}">
                @else
                    <input type="hidden" name="uniqueid" value="{{str_random(7)}}">
                @endif
                <input type="hidden" name="title" value="{{$product->title}}">
                <input type="hidden" name="product" value="{{$product->id}}">
                <input type="hidden" id="cost" name="cost" value="{{\App\Product::Cost($product->id)}}">
                <input type="hidden" id="quantity" name="quantity" value="1">
                @if($product->stock != 0 || $product->stock === null )
                    <button type="button" class="addTo-cart to-cart"><i class="fa fa-cart-plus"></i><span>{{$language->add_to_cart}}</span></button>
                @else
                    <button type="button" class="addTo-cart  to-cart" disabled><i class="fa fa-cart-plus"></i>{{$language->out_of_stock}}</button>
                @endif
            </form>
        </td>
    </tr>
</tbody>

【问题讨论】:

  • 您想获得所有产品吗?
  • 你检查数据库查询是否真的返回任何结果?看起来您的数据库为空或查询返回空(可能是因为left join
  • 为什么会有leftjoin?为什么不使用模型关系?
  • 您的wishList 函数,如图所示,不会产生此错误。您确定向我们展示了正确的代码吗?

标签: php laravel


【解决方案1】:

该错误意味着模型用于查找记录或基本上失败。它无法通过提供的 id 找到记录。

由于您的控制器中没有引用模型,我认为视图中的这一行与您的错误有关:

{{ \App\Product::findOrFail($product->pro_id)->feature_image }}

如果它无法通过$product-&gt;pro_id 的 id 找到Product,它将失败并最终出现这样的错误消息,因为它会抛出异常。

对该模型的唯一其他引用是 App\Product::Cost(...) 不确定这是在做什么,但您似乎总是在传递产品 ID。

【讨论】:

    猜你喜欢
    • 2018-03-16
    • 2018-09-12
    • 1970-01-01
    • 2019-12-04
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 2019-02-20
    相关资源
    最近更新 更多