Laravel 5.1 - 在数据库上存储数据会话

时间:2016-05-11 11:51:15

标签: php laravel laravel-5.1

我创建了一个包含产品数据的会话购物车,我想在数据库上创建一个temp_cart表,因此管理员可以看到所有带有id产品的会话购物车。

我的错误:

  

Model.php第424行中的MassAssignmentException: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');



    }



}

移民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());   

    }

}

产品迁移:

<?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');
    }
}

感谢您的帮助!是我试图解决这个问题的日子。

0 个答案:

没有答案