dimanche 23 décembre 2018

"Trying to get property 'price' of non-object"

I had a Laravel script installed, Now When the user fills in the new order form and makes an order for ordering, will be displayed: "Trying to get property 'price' of non-object" The error is for usercontroller and this line:

" $price = ($request->quantity * $servicePrice->price) / 1000;"

This part code is for new order in usercontroller:

 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();
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire