为了从基于 Crinsane 的购物车创建订单并将其关联到(经过身份验证的)用户,我创建了Order模型并在订单控制器中尝试了以下操作。
<?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();
}
}
编辑:
我尝试将列类型作为 TEXT(没有序列化)以及 JSON。我使用 Json API 进行前端 (Vue) 和 JWT 进行身份验证。
[]
当我序列化为 JSON 时,该列会产生一个空集。如果我不序列化,它会返回O:29:"Illuminate\Support\Collection":1:{s:8:"~~NULL_BYTE~~*~~NULL_BYTE~~items";a:0:{}}
订购型号:
<?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');
}
}
来源链接:https ://github.com/riyaz7us/Laraman/tree/master/app/Http/Controllers