0

i wanted to add a subtotal to my cart page and i had this problem from laravel-9x, and actually i was watching a video on youtube that they used the same methode and that worked nicely with laravel 5.6 and i'm using this laravelshopping cart from github: https://github.com/darryldecode/laravelshoppingcart

so this is my function :

use Cart;
use App\Models\Produit;
use Darryldecode\Cart\CartCondition;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CartController extends Controller
{
    //add product to card
    public function add(Request $request)
    {
        $produit = Produit::find($request->id);
        Cart::add(array(
            'id' => $produit->id, 
            'name' => $produit->nom,
            'price' => $produit->prix_ht,
            'quantity' => $request->qty,
            'attributes' => array('size'=>$request->size,
                                  'photo'=>$produit->photo_principale,
                                  'prix_ttc'=>$produit->prixTTC()
            )
        ));
        return redirect(route(name:'cart_index'));
        
    }

and this is the function prixTTC():

   public function prixTTC(){
        $prix_ttc = $this->prix_ht * self::$facteur_tva;
        return number_format($prix_ttc,2);
    }

and this is my HTML:

                   <td>
                    {{ $produit->attributes['prix_ttc'] }} €
                    </td>

                    <td>
                        {{ number_format($produit->attributes['prix_ttc'] * $produit->quantity,2)}} €
                    </td>

and laravel gives me this message: Undefined array key "prix_ttc"

kr.Nada
  • 1
  • 2
  • please provide source code of the function "prixTTC()" – Stormtrooper CWR Jul 28 '22 at 11:45
  • 1
    can you do `dd($produit)` and add output to question? – geertjanknapen Jul 28 '22 at 11:48
  • @RobBiermann because he adds the $produit to his $Cart with 'attributes':[...'prix_ttc'...]. For further analysis the source code of your classes Cart and Produit are relevant too. – Stormtrooper CWR Jul 28 '22 at 11:48
  • `$produit` is a collection of your `Produit` model result. It don't even have attributes key. How you are trying to access it. If you have a relation between them first you have to call relation then access the attribute. – Vüsal Hüseynli Jul 28 '22 at 11:49
  • @geertjanknapen it's undefined variable! – kr.Nada Jul 28 '22 at 11:56
  • @JohannesGriebenow i did it – kr.Nada Jul 28 '22 at 12:01
  • @kr.Nada function looks fine. Please consider the comment from _Vüsal Hüseynli_, since this is likely to solve your problem. – Stormtrooper CWR Jul 28 '22 at 12:09
  • @JohannesGriebenow i don't kniw how to do it, im beginner in php – kr.Nada Jul 28 '22 at 12:16
  • @VüsalHüseynli how can i do it – kr.Nada Jul 28 '22 at 12:17
  • Please briefly explain what you are trying to do so we can help you. Is there a relation between them? Why are you holding array in attributes column. You can add another table for it and then relate them each other. If you don't know what I am talking about. Please read about database relations in laravel: https://laravel.com/docs/9.x/eloquent-relationships – Vüsal Hüseynli Jul 28 '22 at 12:22
  • i'm following this laravelshoppingcart from github:https://github.com/darryldecode/laravelshoppingcart#usage – kr.Nada Jul 28 '22 at 12:27
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – Peppermintology Jul 28 '22 at 12:59

1 Answers1

0

in "add" function

return redirect(route(name:'cart_index'))->with(['produit'=>$produit]);

I'm from morocco too

i hope it was useful .

Joukhar
  • 724
  • 1
  • 3
  • 18