mercredi 11 mai 2016

Laravel 5.1 - Store data session on database

I created a session cart with product data, i would like create a temp_cart table on database, so admin can see all sessions carts with id products.

MY ERROR:

MassAssignmentException in Model.php line 424: product_id

CARTCONTROLLER.PHP

<?php

namespace dixard\Http\Controllers;

use Illuminate\Http\Request;

use dixard\Http\Requests;
use dixard\Http\Controllers\Controller;

use dixard\Product;
use dixard\TempCart;



class CartController extends Controller
{

    public function __construct()
    {


        if(!\Session::has('cart')) \Session::put('cart',array());   

    }




    public function show()
    {


        $cart = \Session::get('cart'); 

        $total = $this->total(); 


        return view('store.cart', compact('cart', 'total')); //passo l'informazione alla view


    }

    // ADD ITEM ///////////////////////////////////////////////////////

    public function add(Product $product, TempCart $TempCart)
    {

        $cart  = \Session::get('cart'); // ricevo la var di sessione cart e la salvo su cart
        $product->quantity = 1;  
        $cart[$product->id] = $product; 
        \Session::put('cart', $cart); 


        $data = [

                'product_id' =>2,
                'customer_id'=> 2,
                'price'=> 58,
                'quantity'=> 1, 

        ];

        $TempCart = TempCart::create($data);




        return redirect()->route('cart-show');


    }


    // delete ITEM///////////////////////////////////////////////////////////

    public function delete(Product $product)
    {


        $cart = \Session::get('cart'); //salviamo la variabile di sessione in cart
        unset($cart[$product->id]);// unset ci fa eliminare elementi dall'ARRAY




        \Session::put('cart', $cart);





            return redirect()->route('cart-show');






    }






    // UPDATE ITEM //////////////////////////////////////////////////////////////

    public function update(Product $product, $quantity)
    {

        $cart = \Session::get('cart');

        $cart[$product->id]->quantity = $quantity;

        \Session::put('cart', $cart);

        // uguagliare il quantity con il nuovo quantuty del utente
        // redirect alla pagina carrello

        return redirect()->route('cart-show');





    }





    // TRASH CART /////////////////////////////////////////////////////////////////

    public function trash()
    {


        \Session::forget('cart');

        return redirect()->route('cart-show');

    }

    // TOTAL //////////////////////////////////////////////////////////////////////

    private function total()
    {

    // private because we use this function only here

            $cart = \Session::get('cart');
            $total = 0;

            foreach($cart as $item) {

                $total += $item->price * $item->quantity;   

            }

            return $total;


    }


    // Order detail 

    public function orderDetail()
    {

        //if there is not data session cart return products
        if(count(\Session::get('cart')) <= 0) return redirect()->route('products');
        //get data cart session
        $cart= \Session::get('cart');

        // fucntion total

        $total = $this->total();

        //pass data cart and total
        return view('store.order-detail', compact('cart', 'total'));



    }


    // Order complete page


    public function orderComplete()
    {


        return view('store.order-complete');



    }



}

MIGRATION TEMPCART.PHP

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTempCartsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('temp_carts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('customer_id')->unsigned();


            $table->integer('product_id')->unsigned();
            $table->foreign('product_id')
                  ->references('id')
                  ->on('products')
                  ->onDelete('cascade');


            $table->decimal('price', 5, 2);
            $table->integer('quantity')->unsigned();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('temp_carts');
    }
}

MODEL TEMPCART.PHP

<?php

namespace dixard;

use Illuminate\Database\Eloquent\Model;

class TempCart extends Model
{
    protected $table = 'temp_carts';

    protected $fillabile = 

    [

    'product_id',
    'customer_id',
    'price',
    'quantity',


    ];
}

MODEL PRODUCT.PHP

<?php

namespace dixard;

use Illuminate\Database\Eloquent\Model;

use dixard\User;

use dixard\Category;

class Product extends Model
{

    protected $table = 'products';

    protected $fillabile = 

    [
    'name',
    'slug',
    'description',
    'extract',
    'image',
    'visible',
    'price',
    'category_id',
    'user_id'

    ];



    // Colleghiamo OGNI prodotto ha un utente
    public function user() {
            return $this->belongsTo('dixard\User');



    }

    // Colleghiamo OGNI prodotto ha una categoria
    public function category() {
            return $this->belongsTo('dixard\Category');



    }
    //----//
    // Voglio prendere informazioni dalla session cart e passarla per la views.
    public function __construct()
    {

        // se non esiste la variabile sessione allor LA CREO e salvo array vuoto
        if(!\Session::has('cart')) \Session::put('cart',array());   

    }

}

PRODUCT MIGRATION:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */

    //Up creare table
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255);
            $table->string('slug'); 
            $table->text('description');

            $table->string('extract', 300);
            $table->decimal('price', 5, 2);
            $table->string('image', 300);

            $table->boolean('visible');



            $table->integer('user_id')->unsigned();

            $table->foreign('user_id')
                  ->references('id')->on('users')
                  ->onDelete('cascade');







            $table->integer('category_id')->unsigned();


            $table->foreign('category_id')
                  ->references('id')
                  ->on('categories')
                  ->onDelete('cascade');





            // ----// 
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    //eliminare table
    public function down()
    {
        Schema::drop('products');
    }
}

Thank you for your help! are days that i tried to fix this problem.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire