0

In order to create an order from Crinsane based cart and associate it to the (authenticated) user, I created Order model and tried the following in order controller.

<?php

namespace App\Http\Controllers;

use Cart;
use App\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api');
    }
    /**
     * store cart to authenticated user as order.
     *
     */
    public function store(Request $request)
    {
        $order = new Order;

        $cartcontent = Cart::content();
        $user = auth('api')->user();
        $serializedcontent = $cartcontent->toJson(); //Returns []
        //$serializedcontent = $cartcontent->toArray(); //error
        //$serializedcontent = str_replace("\0", "~~NULL_BYTE~~", serialize($cartcontent)); //returns 0:29:"Illuminate....

        $order->user_id= $user->id;
        $order->cart_content= $serializedcontent;
        $order->save();

    }
}

Edit:

I tried with column type as TEXT (without serialization) as well as JSON. I'm using Json API for front end (Vue) and JWT for authentication.

The column results in an empty set [] when I serialize to JSON. If I don't serialize, it returns O:29:"Illuminate\Support\Collection":1:{s:8:"~~NULL_BYTE~~*~~NULL_BYTE~~items";a:0:{}}

Order Model:

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
        protected $fillable = [
        'user_id','user_id','cart_content'
    ];
        public function User(){
        return $this->belongsTo('App\User');
    }
}

Link to source: https://github.com/riyaz7us/Laraman/tree/master/app/Http/Controllers

Riyaz
  • 19
  • 1
  • 8

1 Answers1

0

From the docs

Cart::content() Of course you also want to get the carts content. This is where you'll use the content method. This method will return a Collection of CartItems which you can iterate over and show the content to your customers.

You are trying to insert a collection inside the database. You need to fetch the data from the current instance of the Crinsane based card then insert the data in the database as json or whatever fits your needs.

Kristian Vasilev
  • 504
  • 8
  • 26
  • Still doesn't work. Edited cart controller store method: `Cart::instance('shopping')->add([//products]) `. Modified order controller as `Cart::instance('shopping')->content();`. It returns `O:29:"Illuminate\Support\Collection":1:{s:8:"`. when serialized or json, it returns an empty array – Riyaz Feb 18 '20 at 12:40
  • Can you edit your post with more code? How do you serialize it? What is the type of your database column? Do you have strict types enabled in your application for mysql? How are you fetching/retruning the database result in order to display it? Is it being saved in the database as JSON or it's just empty string? Can you provide also the model of the Order? Fillable, protected, hidden and etc.. https://laravel.com/docs/5.8/eloquent-serialization Here is a link to the docs on how to serialize data – Kristian Vasilev Feb 18 '20 at 14:35
  • Edited the question. Also added the link to the source for your convenience – Riyaz Feb 18 '20 at 17:14
  • Thanks. I don't know why you have 2 times **user_id** in the fillable but you can remove one of them. Also check this out https://github.com/Crinsane/LaravelShoppingcart/blob/master/src/Cart.php#L342 This is the `store()` method that may fix your problem. – Kristian Vasilev Feb 19 '20 at 06:27
  • 1
    I got it now. I was using my cart controller inside web route (for sessions) and checkout controller inside api route. Anyway, how would it be if I store my cart using vue, vuex store instead of crinsane? – Riyaz Feb 20 '20 at 17:59