我已经安装了Laravel脚本,现在当用户填写新的订单表格并下订单时,将显示: “试图获取非对象的属性'价格'” 错误是针对usercontroller的,并且此行:
“ $ price =($ request-> quantity * $ servicePrice-> price)/ 1000;”
此零件代码用于用户控制器中的新订单:
public function newOrder()
{
$categories = Category::where('status', 1)->orderBy('name')->get();
return view('user.newOrder', compact('categories'));
}
public function getPack(Request $request)
{
$items = Service::where('category_id', $request->id)->where('status', 1)->orderBy('name')->get();
return $items;
}
public function getPackDetails(Request $request)
{
$items = Service::findOrFail($request->id);
return $items;
}
public function storeNewOrder(Request $request)
{
$this->validate($request, [
'category' => 'required',
'service' => 'required',
'link' => 'required',
'quantity' => 'required',
]);
$service = Service::where('category_id', $request->category)->where('id', $request->service)->first();
$servicePrice = ServicePrice::where('category_id', $request->category)->where('service_id', $request->service)->where('user_id', Auth::id())->first();
$item = new Order();
$user = User::findOrFail(Auth::id());
$transaction = new Transaction();
if ($request->quantity >= $service->min && $request->quantity <= $service->max) {
$price = ($request->quantity * $servicePrice->price) / 1000;
if ($user->balance >= $price) {
$item->category_id = $request->category;
$item->service_id = $request->service;
$item->user_id = Auth::id();
$item->service_no = $service->service_id;
$item->order_no = 0;
$item->link = $request->link;
$item->quantity = $request->quantity;
$item->price = $price;
$item->status = 'Pending';
$item->start_counter = 0;
$item->remains = $request->quantity;
$item->order_through = 'Web';
$item->save();
$user->balance = $user->balance - $price;
$user->save();
$transaction->user_id = Auth::id();
$transaction->amount = $price;
$transaction->user_balance = $user->balance;
$transaction->type = 1;
$transaction->trx = str_random(12);
$transaction->save();
send_email($user->email, $user->name, 'Order Placed Successfully', 'Your ' . $request->quantity . ' ' . $service->name . ' has been placed successfully.');
session()->flash('success', 'Order request send successfully');
return back();
} else {
session()->flash('alert', 'Insufficient Balance');
return back();
}
} else {
session()->flash('alert', 'Quantity should be within ' .
$service->min . ' to ' . $service->max);
return back();
}
}
答案 0 :(得分:0)
问题很可能出在这一行:
$servicePrice = ServicePrice::where('category_id', $request->category)->where('service_id', $request->service)->where('user_id', Auth::id())->first();
如果查询未从数据库返回任何结果,则代码将返回null。 一个好的做法是检查结果是否为空,应该处理它。
这意味着,在使用$servicePrice
之前,您需要检查其中是否包含任何内容:
if(!$servicePrice){ //same result: if($servicePrice == null) {
//`$servicePrice==null`, so you should handle the error here - throw an exception or return
}
//your code here
$servicePrice = ServicePrice::where('category_id', $request->category)->where('service_id', $request->service)->where('user_id', Auth::id())->first();
此外,可以直接从以下位置检索经过身份验证的用户:
$user = Auth::user();
如果访问者未通过身份验证,则$user
将是null
,原因与前一个相同。
if(!$user){
//the user is NOT authenticated
} else {
//the user is authenticated
}