如何在laravel

时间:2018-04-30 12:47:55

标签: mysql laravel eloquent

我只会分享最重要的代码

$product = DB::table('products')->where('id','=',$pid)->get();
//the above code makes no errors
if ($product[0]::sale == '1') // this line of code makes error
        {
            //$cost+=($product[0]->price - ($product[0]->discount*$product[0]->price/100));
        }
        else
        {
            //$cost+=$product[0]->price;
        }

很清楚这一行if ($product[0]::sale == '1')犯了错误

然而我无法抓住它 - 当我发送请求时 - 我只收到500(内部服务器错误) - 我的事件无法捕获错误

- 更新

返回$ product //犯错误

返回var_dump($ product);

返回此

object(stdClass)#234 (23) {
  ["title"]=>
  string(16) "oihoh jewioh 234"
  ["id"]=>
  string(9) "hew7h23iu"
  ["desciption"]=>
  string(162) "ewhniuhewch2398h9h32
32dh2398dh23dh3
d2h3d8h238dh2389hd98
3d2hd8932hd8h23329dj892
j9hd2dh8723gd7g32d
23dh9832hd8h2389dh9h239dh239
23d8h3289dh3298d9h32dh329h"
  ["sizes"]=>
  string(4) "M,XL"
  ["colors"]=>
  string(14) "#12f,#caf,#afa"
  ["amount"]=>
  int(12)
  ["category"]=>
  string(5) "Women"
  ["scategory"]=>
  string(5) "Lambs"
  ["featured"]=>
  int(1)
  ["minfo"]=>
  string(23) "sia:deuw,on:iej,od:jdiw"
  ["images"]=>
  string(288) "snoicds,nconew,ncoiwenioew,http://www.ridgetopvirtualsolutions.com/wp-content/uploads/2014/03/product-that-sells-4-ridgetopvirtualsolutions.jpg*snoicdse,nconew,ncoiwenioew,http://www.ridgetopvirtualsolutions.com/wp-content/uploads/2014/03/product-that-sells-4-ridgetopvirtualsolutions.jpg"
  ["video"]=>
  NULL
  ["fimage"]=>
  string(143) "snoicds,nconew,ncoiwenioew,http://www.ridgetopvirtualsolutions.com/wp-content/uploads/2014/03/product-that-sells-4-ridgetopvirtualsolutions.jpg"
  ["sale"]=>
  int(0)
  ["price"]=>
  float(26.5)
  ["sold"]=>
  int(32)
  ["views"]=>
  int(14)
  ["likes"]=>
  int(24)
  ["discount"]=>
  NULL
  ["dexpire"]=>
  NULL
  ["created_at"]=>
  NULL
  ["updated_at"]=>
  NULL
  ["avgreview"]=>
  float(3.4)
}

1 个答案:

答案 0 :(得分:1)

使用查询构建器,您不应该收到该类型的错误。这是因为您将其视为一个数组。我们都做到了。

好消息是,它比那更好!使用:

$product = DB::table('products')->find($pid);

然后使用以下代码检查产品是否存在。

if ( $product ) { ... }