vahidds
4 سال پیش توسط vahidds مطرح شد
1 پاسخ

ارور در لاراول 8

سلام تو سبد خرید برای کوپن
این ارور دریافت میکنم

ErrorException
Trying to access array offset on value of type null
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
C:\wamp\www\digikala\app\Http\Controllers\CartController.php:22

کد های مربوطه به این صورته

cartcontroller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use Cart;
use Validator;
use App\Models\Coupon;
use App\Models\Category;

class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
      $categories = Category::all();
      $discount = session()->get('coupon')['discount'];
      $newTotal = Cart::total()->$discount;
        return View('cart')->with([
          'categories'=>$categories,
          'discount'=> $discount,
          'newTotal'=> $newTotal,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //store product information to Cart
        $duplicated = Cart::instance('default')->search(function($cartItem,$rowId) use($request){
          return $cartItem->id === $request->id;
        });
            if ($duplicated->isNotEmpty()){
            return redirect(route('cart.index'))->with('success_message','محصول از قبل در سبد خرید موجود می باشد.');
            }else {
            Cart::instance('default')->add($request->id, $request->name, 1, $request->price)
            ->associate('App\Models\Product'); //model
            return redirect(route('cart.index'))->with('success_message','محصول با موفقیت به سبد خرید اضافه شد.');
                  }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //update one product cart
        $validator = validator::make($request->all(),[
          'quantity'=>'required|numeric|between:1,48'
        ]);
        if($validator->fails()) {
          session()->flash('errors_message','تعداد محصول باید بین 1 تا 48 عدد باشد.');
          return response()->json(['success'=>false]);
        } else {
          Cart::instance('default')->update($id,$request->quantity);
          session()->flash('success_message','تعداد محصولات به روز شد.');
          return response()->json(['success'=>true]);
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Cart::remove($id);
        return back()->with('success_message','محصول با موفقیت از سبد خرید حذف شد.');
    }
    public function empty()
    {
        //delete cart all
        Cart::destroy();
        return redirect(route('cart.index'))->with('success_message','سبد خرید با موفقیت خالی شد');
    }

}

couponcontroller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\coupon;
use Cart;
class CouponController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $coupon = Coupon::where('code',$request->coupon_code)->first();
        if (! $coupon) {
          return redirect()->route('cart.index')->with('errors_message','.......');
        }
      session()->put('coupon',[
        'code'=>$coupon->code,
        'discount'=>$coupon->discount(Cart::total()),
      ]);
      return redirect()->route('cart.index')->with('success_message','کوپن با موفقیت اعمال شد.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        session()->forget('coupon');
        return redirect()->route('cart.index')->with('success_message','کوپن با موفقیت حذف شد');
    }
}

cart.blade.php

<!-- Page Title-->
<div class="page-title">
  <div class="container">
    <div class="column">
      <h1>سبد خرید فروشگاه</h1>
    </div>
    <div class="column">
      <ul class="breadcrumbs">
        <li><a href="{{url('/')}}">خانه</a>
        </li>
        <li class="separator">&nbsp;</li>
        <li>سبد خرید</li>
      </ul>
    </div>
  </div>
</div>
<!-- Page Content-->
<div class="container padding-bottom-3x mb-1">
  @if (session()->has('success_message'))
    <!-- Alert-->
    <div class="alert alert-info alert-dismissible fade show text-center" style="margin-bottom: 30px;">
      <span class="alert-close" data-dismiss="alert"></span>
      <img class="d-inline align-center" src="data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMTkuMC4wLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcgVmVyc2lvbjogNi4wMCBCdWlsZCAwKSAgLS0+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeD0iMHB4IiB5PSIwcHgiIHZpZXdCb3g9IjAgMCA1MTIuMDAzIDUxMi4wMDMiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDUxMi4wMDMgNTEyLjAwMzsiIHhtbDpzcGFjZT0icHJlc2VydmUiIHdpZHRoPSIxNnB4IiBoZWlnaHQ9IjE2cHgiPgo8Zz4KCTxnPgoJCTxnPgoJCQk8cGF0aCBkPSJNMjU2LjAwMSw2NGMtNzAuNTkyLDAtMTI4LDU3LjQwOC0xMjgsMTI4czU3LjQwOCwxMjgsMTI4LDEyOHMxMjgtNTcuNDA4LDEyOC0xMjhTMzI2LjU5Myw2NCwyNTYuMDAxLDY0eiAgICAgIE0yNTYuMDAxLDI5OC42NjdjLTU4LjgxNiwwLTEwNi42NjctNDcuODUxLTEwNi42NjctMTA2LjY2N1MxOTcuMTg1LDg1LjMzMywyNTYuMDAxLDg1LjMzM1MzNjIuNjY4LDEzMy4xODQsMzYyLjY2OCwxOTIgICAgIFMzMTQuODE3LDI5OC42NjcsMjU2LjAwMSwyOTguNjY3eiIgZmlsbD0iIzUwYzZlOSIvPgo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5My41NDcsMzUuNjA1TDI0Ni43LDQxMS4yQzI0Ni40ODcsNDExLjU2MywyNDYuMTY3LDQxMS44NCwyNDYuMDE3LDQxMi4yNjd6IE0yNTYuMDAxLDM2Mi42NjcgICAgIEMxNjEuOSwzNjIuNjY3LDg1LjMzNSwyODYuMTAxLDg1LjMzNSwxOTJTMTYxLjksMjEuMzMzLDI1Ni4wMDEsMjEuMzMzUzQyNi42NjgsOTcuODk5LDQyNi42NjgsMTkyICAgICBTMzUwLjEwMywzNjIuNjY3LDI1Ni4wMDEsMzYyLjY2N3ogTTM1Ni43NTksNDQ5LjEzMWMtMy40MTMtMS43MjgtNy41MDktMS40NzItMTAuNjg4LDAuNjYxbC01Mi4zNzMsMzQuOTIzbC0zMy42NDMtMTAwLjkyOCAgICAgYzQwLjM0MS0wLjg1Myw3Ny41ODktMTQuMTg3LDEwOC4xNi0zNi4zMzFsNDkuNTc5LDEzMi4yMDNMMzU2Ljc1OSw0NDkuMTMxeiIgZmlsbD0iIzUwYzZlOSIvPgoJCTwvZz4KCTwvZz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8L3N2Zz4K" width="18" height="18" alt="Medal icon"><strong>{{session()->get('success_message')}}</strong></div>
    @endif
    @if (session()->has('errors_message'))
      <!-- Alert-->
      <div class="alert alert-info alert-dismissible fade show text-center" style="margin-bottom: 30px;">
        <span class="alert-close" data-dismiss="alert"></span>
        <img class="d-inline align-center" src="data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0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i42NjgsOTcuODk5LDQyNi42NjgsMTkyICAgICBTMzUwLjEwMywzNjIuNjY3LDI1Ni4wMDEsMzYyLjY2N3ogTTM1Ni43NTksNDQ5LjEzMWMtMy40MTMtMS43MjgtNy41MDktMS40NzItMTAuNjg4LDAuNjYxbC01Mi4zNzMsMzQuOTIzbC0zMy42NDMtMTAwLjkyOCAgICAgYzQwLjM0MS0wLjg1Myw3Ny41ODktMTQuMTg3LDEwOC4xNi0zNi4zMzFsNDkuNTc5LDEzMi4yMDNMMzU2Ljc1OSw0NDkuMTMxeiIgZmlsbD0iIzUwYzZlOSIvPgoJCTwvZz4KCTwvZz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8L3N2Zz4K" width="18" height="18" alt="Medal icon"><strong>{{session()->get('errors_message')}}</strong></div>
      @endif
  @if (count($errors)>0)
    @foreach ($errors->all() as $error)
      <!-- Alert-->
      <div class="alert alert-danger alert-dismissible fade show text-center" style="margin-bottom: 30px;"><span class="alert-close" data-dismiss="alert"></span><i class="iziToast-icon icon-ban"></i><strong>  {{$error}}  </strong></div>
    @endforeach
  @endif
  <!-- Shopping Cart-->
      @if (Cart::count()>0)

        <div class="table-responsive shopping-cart">
        <table class="table">
          <thead>
            <tr>
              <th>نام محصول</th>
              <th class="text-center">تعداد / مقدار</th>
              <th class="text-center">قیمت واحد</th>
              <th class="text-center">قیمت کل</th>
              <th class="text-center">سبد خرید بعدی</th>
              <th class="text-center"><a class="btn btn-sm btn-outline-danger" href="{{route('cart.empty')}}">خالی کردن سبد</a></th>
            </tr>
          </thead>
      @foreach(Cart::content() as $item)
      <tbody>
        <tr>
          <td>
            <div class="product-item"><a href="{{route('product.show',$item->model->slug)}}" class="product-thumb"><img src="{{asset('img/shop/cart/10.jpg')}}" alt="Product"></a>
              <div class="product-info ">
                <h4 class="product-title"><a href="{{route('product.show',$item->model->slug)}}">{{$item->model->name}}</a></h4>
      <span><em>اندازه :</em> 10.5</span>
      <span><em>رنگ بندی :</em> سفید مشکی</span>
              </div>
            </div>
          </td>
          <td class="text-center">
            <div class="count-input">
              <input class="quantity form-control" type="number" data-id="{{ $item->rowId }}" data-productQuantity="{{ $item->model->quantity }}" value="{{ $item->qty }}">
            </div>
          </td>
          <td class="text-center text-lg text-medium font-small">{{$item->model->price}} تومان</td>
          <td class="text-center text-lg text-medium font-small">{{ number_format($item->subtotal) }} تومان</td>

         {{-- save one product to later--}}
          <td class="text-center">
             <form action="{{route('cart.saveForLater',$item->rowId)}}" method="post">
               @csrf
               @method('post')
               <button class="btn remove-from-cart" type="submit" name="button" data-toggle="tooltip" title="افزودن به سبد خرید بعدی"><i class="icon-heart"></i></button>
              </form>
          </td>
          {{-- Delet one product from cart--}}
            <td class="text-center">
               <form action="{{route('cart.destroy',$item->rowId)}}" method="post">
                 @csrf
                 @method('Delete')
                 <button class="btn remove-from-cart" type="submit" name="button" data-toggle="tooltip" title="حذف از سبد"><i class="icon-cross"></i></button>
                </form>
            </td>
        </tr>
      </tbody>
          @endforeach
    </table>
  </div>
  <div class="shopping-cart-footer">

    <div class="column text-lg text-left">جمع قیمت: <span class="text-medium font-small"> {{Cart::subtotal()}} </span>+<span class="text-medium font-small"> {{Cart::tax()}} (مالیات) </span> : <span class="text-medium font-small">{{Cart::total()}}</span></div>
    @if (session()->has('coupon'))
    <div class="column text-lg text-right">مبلغ {{$discount}} اعمال شد.
       <span class="text-medium font-small">{{session()->get('coupon')['code']}}</span>
      <form action="{{route('coupon.destroy')}}" method="post">
        @csrf
        @method('Delete')
        <button class="btn remove-from-cart" type="submit" name="button" data-toggle="tooltip" title="حذف کوپن"><i class="icon-cross"></i></button>
       </form>
    </div>
  @else
    <div class="column text-left">
      <form action="{{route('coupon.store')}}" class="coupon-form" method="post">
        @csrf
        <input class="form-control form-control-sm" type="text" name="coupon_code" placeholder="کد کوپن خرید" required>
        <button class="btn btn-outline-primary btn-sm" type="submit" name="submit">اعمال کوپین خرید</button>
      </form>
    </div>
  @endif
  </div>
  <div class="shopping-cart-footer ">
    <div class="column text-right">
<a class="btn btn-outline-secondary" href="{{route('shop')}}"> برگشت به فروشگاه &nbsp;<i class="icon-arrow-left"></i></a>
</div>
    <div class="column text-left">
<a class="btn btn-primary" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-circle-check" data-toast-title="سبد خرید : " data-toast-message="بروز رسانی موفقیت آمیز">بروز رسانی سبد خرید</a>
<a class="btn btn-success" href="checkout-address.html">بررسی و پرداخت</a>
</div>
  </div>

      @else
        <div class="table-responsive shopping-cart">
        <table class="table">

            <div class="card text-center">
            <div class="card-body">
              <h3 class="card-title">اوه.چه بد سبد خرید شما خالیست.</h3>
              <p class="card-text">جهت انجام خرید روی لینک زیر کلیک کنید</p>
              <button class="btn btn-warning" onclick="location.href='{{route('shop')}}'" type="button">فروشگاه</button>
              <button class="btn btn-success" onclick="location.href='{{route('home')}}'" type="button"><i class="icon-unlock"></i>  ورود به سایت</button>
              <div class="card-footer text-muted">کاربر جدید هستید؟<button class="btn" href="#" type="button"><i class="icon-unlock"></i> ثبت نام کنید</button></div>
            </div>
        </div>

    </table>
  </div>
      @endif
      <!-- Shopping Save Cart-->
      <!-- Page Title-->
      <div class="page-title">
        <div class="container">
          <div class="column">
            <h1>لیست ذخیره</h1>
          </div>
        </div>
      </div>
      <!-- Shopping item Save Cart-->

      @if (Cart::instance('saveForLater')->count()>0)

        <div class="table-responsive shopping-cart">
        <table class="table">
          <thead>

            <tr>
              <th>نام محصول</th>
              <th class="text-center">قیمت</th>
              <th class="text-center">سبد خرید بعدی</th>
              <th class="text-center"><a class="btn btn-sm btn-outline-danger" href="{{route('cart.savedForLaterEmpty')}}">خالی کردن لیست ذخیره</a></th>
            </tr>
          </thead>
                @foreach (Cart::instance('saveForLater')->content() as $item)
      <tbody>
        <tr>
          <td>
            <div class="product-item"><a href="{{route('product.show',$item->model->slug)}}" class="product-thumb"><img src="{{asset('img/shop/cart/10.jpg')}}" alt="Product"></a>
              <div class="product-info ">
                <h4 class="product-title"><a href="{{route('product.show',$item->model->slug)}}">{{$item->name}}</a></h4>
      <span><em>اندازه :</em> 10.5</span>
      <span><em>رنگ بندی :</em> سفید مشکی</span>
              </div>
            </div>
          </td>
          <td class="text-center text-lg text-medium font-small">{{$item->price}} تومان</td>

         {{-- save one product to later--}}
          <td class="text-center">
             <form action="{{route('cart.switchFromSavedToCart',$item->rowId)}}" method="post">
               @csrf
               @method('post')
               <button class="btn remove-from-cart" type="submit" name="button" data-toggle="tooltip" title="افزودن به سبد خرید بعدی"><i class="icon-heart"></i></button>
              </form>
          </td>
          {{-- Delet one product from cart--}}
            <td class="text-center">
               <form action="{{route('cart.destroySavedForLater',$item->rowId)}}" method="post">
                 @csrf
                 @method('Delete')
                 <button class="btn remove-from-cart" type="submit" name="button" data-toggle="tooltip" title="حذف از سبد"><i class="icon-cross"></i></button>
                </form>
            </td>
        </tr>
      </tbody>
      @endforeach
    </table>
  </div>
@else
  <div class="table-responsive shopping-cart">
  <table class="table">

      <div class="card text-center">
      <div class="card-body">
        <h3 class="card-title">لیست ذخیره شما خالیست.!</h3>
      </div>
  </div>

</table>
</div>
@endif

</div>

web.php

//Copon
Route::post('/coupon', [CouponController::class, 'store'])->name('coupon.store');
Route::delete('/coupon', [CouponController::class, 'destroy'])->name('coupon.destroy');

Coupon.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Coupon extends Model
{
    use HasFactory;
    public function findByCode($code)
    {
      return self::where('code',$code)->first();
    }
    public function discount($total)
    {
      if ($this->type=='fixed') {

        return $this->value;

      }elseif ($this->type=='percent_off')
      {

      return  (integer)$total*($this->percent_off / 100);

      }else {
      return 0;
      }
    }
}

ثبت پرسش جدید
محمدحسن یگانه
تخصص : Full-Stack Web Developer Freel...
@mhyeganeh 4 سال پیش مطرح شد
0

توضیحاتتون خیلی ناقصه. ولی احتمالا مشکلش مربوط به کد زیر هست:

session()->get('coupon')['discount']

قبلش مطمئن بشید که محتوای مورد نظرتون در session وجود داره یا نه.


برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام