Sentinal and helper file issue

Published 2 years ago by madsynn

Hope someone can see what i am missing.

I keep getting an error when trying to use helper functions on my index and view pages. It is the mechanism that shows my cart and contents in the head menu on all the pages. I am using Sentinel and i think that might be part of my problem I keep getting this error.

ErrorException in helperFunctions.php line 34:  Invalid argument supplied for foreach()

my helper file:

use App\Models\Section;
use App\Models\Cart;
use App\Models\Option;
use App\Models\Price;

use Sentinel;
use Session;
use \Illuminate\Database\Eloquent\Collection;

class helperFunctions
{
    public static function getPageInfo(&$sections,&$cart,&$total)
    {
        $sections = Section::all();
        if ( Sentinel::getUser()) {
            $cart =  Sentinel::getUser()->cart;
        } else {
            $cart = new Collection;
            if (Session::has('cart')) {
                foreach (Session::get('cart') as $item) {
                    $elem = new Cart;
                    $elem->product_id = $item['product_id'];
                    $elem->amount = $item['qty'];
                    if (isset($item['options'])) {
                        $elem->options = $item['options'];
                    }
                    $cart->add($elem);
                }
            }
        }
        $total = 0;
        foreach ($cart as $item) {
            $total += $item->product->price*$item->amount;
        }
    }
}

index function in my productscontroller.php

namespace app\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
 
use App\Models\Category;
use App\Models\Product;
use App\Models\CategoryProduct;
use App\Models\AlbumPhoto;
use App\Models\Section;
use App\Models\Cart;
use App\Models\Option;
use App\Models\OptionValue;
use App\Models\ProductFeature;
use App\Models\ProductVariant;
use App\Models\OrderProduct;
use File;
use Sentinel;
use App;
use Session;
use \Illuminate\Database\Eloquent\Collection;
use App \Ecommerce\helperFunctions;

class ProductController extends Controller
{
    public function index()
    {
       $new_products = Product::orderBy('created_at', 'desc')->take(12)->get();
        helperFunctions::getPageInfo($sections,$cart,$total);
        return view('frontend.shop.index', compact('new_products','sections', 'cart', 'total'));

    }

the models:


// CART 
class Cart extends Model
{
    protected $table = "cart";
    protected $fillable = ['product_id','amount'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function product()
    {
        return $this->belongsTo(Product::class);
    }
}

// PRODUCT
lass Product extends Model
{
    protected $table = "products";

    protected $fillable = ['name' , 'manufacturer' , 'price' , 'details' , 'quantity' , 'category_id' , 'thumbnail'];

    public function categories()
    {
        return $this->belongsToMany('App\Category', 'category_product');
    }

    public function orders()
    {
        return $this->belongsToMany('App\Order');
    }

    public function carts()
    {
        return $this->belongsToMany('App\Cart');
    }

    public function photos()
    {
        return $this->hasMany('App\AlbumPhoto');
    }

    public function options(){
        return $this->hasMany('App\Option');
    }
}
// SECTION
class Section extends Model
{
    protected $table = "sections";
    protected $fillable = ['name'];

    public function categories()
    {
        return $this->hasMany(App\Models\Category::class);
    }
}

If anyone can see what im missing it would be very helpful.

madsynn

@Snapey

Can you see what my problem is i keep getting this error on the helperFunctions.php

ErrorExceptionapp/Ecommerce/helperFunctions.php in handleError

Thanks

madsynn

The file;

<?php namespace Ecommerce;

use App\Models\Section;
use App\Models\Cart;

use Sentinel;
use Session;
use Illuminate\Database\Eloquent\Collection;


class helperFunctions
{
    public static function getPageInfo(&$sections,&$cart,&$total)
    {
        $sections = Section::all();
        if (Sentinel::getUser()) {
            $cart =  Sentinel::getUser()->cart;
        } else {
            $cart = new Collection;
            if (Session::has('cart')) {
                foreach (Session::get('cart') as $item) {
                    $elem = new Cart;
                    $elem->product_id = $item['product_id'];
                    $elem->amount = $item['quantity'];
                    if (isset($item['options'])) {
                        $elem->options = $item['options'];
                    }
                    $cart->add($elem);
                }
            }
        }
        $total = 0;
        foreach ($cart as $item) {
            $total += $item->product->price*$item->amount;
        }
    }
}

Please sign in or create an account to participate in this conversation.