我正在使用以下代码
$order = new Order();
$order->cart = serialize($cart);
$order->address = $request->input('address');
$order->name = $request->input('name');
$order->payment_id = $charge->id;
// echo '<pre>';
// var_dump($order->toArray());exit;
Auth::user()->orders()->save($order);
它给了我这个错误
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function save() on null
如果我转储变量$ order我得到数据
array(4) {
["cart"]=>
string(6053) "O:8:"App\Cart":3:{s:5:"items";a:2:{i:4;a:3:{s:3:"qty";i:2;s:5:"price";i:40;s:4:"item";O:11:"App\Product":26:{s:13:"*connection";s:5:"mysql";s:8:"*table";N;s:13:"*primaryKey";s:2:"id";s:10:"*keyType";s:3:"int";s:12:"incrementing";b:1;s:7:"*with";a:0:{}s:12:"*withCount";a:0:{}s:10:"*perPage";i:15;s:6:"exists";b:1;s:18:"wasRecentlyCreated";b:0;s:13:"*attributes";a:9:{s:2:"id";i:4;s:4:"name";s:9:"Product 4";s:4:"slug";s:9:"product-4";s:5:"image";s:14:"1526885592.jpg";s:7:"details";s:26:"Sub Heading or Product - 4";s:11:"description";s:63:"This is description of product-1 and related stuff to product-1";s:5:"price";i:20;s:10:"created_at";s:19:"2018-05-16 06:26:26";s:10:"updated_at";s:19:"2018-05-21 06:55:15";}s:11:"*original";a:9:{s:2:"id";i:4;s:4:"name";s:9:"Product 4";s:4:"slug";s:9:"product-4";s:5:"image";s:14:"1526885592.jpg";s:7:"details";s:26:"Sub Heading or Product - 4";s:11:"description";s:63:"This is description of product-1 and related stuff to product-1";s:5:"price";i:20;s:10:"created_at";s:19:"2018-05-16 06:26:26";s:10:"updated_at";s:19:"2018-05-21 06:55:15";}s:10:"*changes";a:0:{}s:8:"*casts";a:0:{}s:8:"*dates";a:0:{}s:13:"*dateFormat";N;s:10:"*appends";a:0:{}s:19:"*dispatchesEvents";a:0:{}s:14:"*observables";a:0:{}s:12:"*relations";a:0:{}s:10:"*touches";a:0:{}s:10:"timestamps";b:1;s:9:"*hidden";a:0:{}s:10:"*visible";a:0:{}s:11:"*fillable";a:0:{}s:10:"*guarded";a:1:{i:0;s:1:"*";}}}i:9;a:3:{s:3:"qty";i:1;s:5:"price";i:1080;s:4:"item";O:11:"App\Product":26:{s:13:"*connection";s:5:"mysql";s:8:"*table";N;s:13:"*primaryKey";s:2:"id";s:10:"*keyType";s:3:"int";s:12:"incrementing";b:1;s:7:"*with";a:0:{}s:12:"*withCount";a:0:{}s:10:"*perPage";i:15;s:6:"exists";b:1;s:18:"wasRecentlyCreated";b:0;s:13:"*attributes";a:9:{s:2:"id";i:9;s:4:"name";s:17:"Samsung Galaxy S9";s:4:"slug";s:26:"samsung-galaxy-s9-plus-128";s:5:"image";s:14:"1526884218.jpg";s:7:"details";s:28:"Samsung Galaxy S9 Plus 128GB";s:11:"description";s:1594:"Samsung fans getting excited about Galaxy S9 but they don't know that Plus version is also coming to make it more excited for the lovers of this brand. S8 was the celebrity of the last year but the upcoming siblings are even better. Samsung Galaxy S9 Plus got aluminum frame on its side while the front and back is covered with solid and thick layer of Corning Gorilla Glass 5 to make it scratch proof and save Samsung's Galaxy S9 Plus from shattering. This device also got an IP68 certificate which means that this smartphone can stay underwater for almost 30 minutes. Samsung Galaxy S9's design is quite alike to this version but there is a difference between the display sizes of both devices. This phone is coming with massive 6.2 inch touchscreen. Galaxy S9 Plus by Samsung got the taste of Super AMOLED display which is exclusively made for the devices of this brand. Infinity screen is key selling point of these devices that's why Samsung S9 Plus is also coming with that kind of setup where the resolution will be 1440 x 2960 and pixel density reached the mark of 531. Always on display is also present on the board of Galaxy S9 Plus that will keep you updated about the notifications. Next generation Snapdragon 845 chipset got the approval to come in these devices while Oreo is decorating the interior of S9 Plus. Dual camera with 12 MP sensor is placed on the back side while for the front you will see the 8 MP selfie snapper in action. 6 GB of RAM is handling Samsung Galaxy's S9 Plus multi-tasking where internal storage of this device is 128 GB to handle all the storage needs.";s:5:"price";i:1080;s:10:"created_at";s:19:"2018-05-21 06:30:18";s:10:"updated_at";s:19:"2018-05-21 06:30:18";}s:11:"*original";a:9:{s:2:"id";i:9;s:4:"name";s:17:"Samsung Galaxy S9";s:4:"slug";s:26:"samsung-galaxy-s9-plus-128";s:5:"image";s:14:"1526884218.jpg";s:7:"details";s:28:"Samsung Galaxy S9 Plus 128GB";s:11:"description";s:1594:"Samsung fans getting excited about Galaxy S9 but they don't know that Plus version is also coming to make it more excited for the lovers of this brand. S8 was the celebrity of the last year but the upcoming siblings are even better. Samsung Galaxy S9 Plus got aluminum frame on its side while the front and back is covered with solid and thick layer of Corning Gorilla Glass 5 to make it scratch proof and save Samsung's Galaxy S9 Plus from shattering. This device also got an IP68 certificate which means that this smartphone can stay underwater for almost 30 minutes. Samsung Galaxy S9's design is quite alike to this version but there is a difference between the display sizes of both devices. This phone is coming with massive 6.2 inch touchscreen. Galaxy S9 Plus by Samsung got the taste of Super AMOLED display which is exclusively made for the devices of this brand. Infinity screen is key selling point of these devices that's why Samsung S9 Plus is also coming with that kind of setup where the resolution will be 1440 x 2960 and pixel density reached the mark of 531. Always on display is also present on the board of Galaxy S9 Plus that will keep you updated about the notifications. Next generation Snapdragon 845 chipset got the approval to come in these devices while Oreo is decorating the interior of S9 Plus. Dual camera with 12 MP sensor is placed on the back side while for the front you will see the 8 MP selfie snapper in action. 6 GB of RAM is handling Samsung Galaxy's S9 Plus multi-tasking where internal storage of this device is 128 GB to handle all the storage needs.";s:5:"price";i:1080;s:10:"created_at";s:19:"2018-05-21 06:30:18";s:10:"updated_at";s:19:"2018-05-21 06:30:18";}s:10:"*changes";a:0:{}s:8:"*casts";a:0:{}s:8:"*dates";a:0:{}s:13:"*dateFormat";N;s:10:"*appends";a:0:{}s:19:"*dispatchesEvents";a:0:{}s:14:"*observables";a:0:{}s:12:"*relations";a:0:{}s:10:"*touches";a:0:{}s:10:"timestamps";b:1;s:9:"*hidden";a:0:{}s:10:"*visible";a:0:{}s:11:"*fillable";a:0:{}s:10:"*guarded";a:1:{i:0;s:1:"*";}}}}s:8:"totalQty";i:3;s:10:"totalPrice";i:1120;}"
["address"]=>
string(10) "china town"
["name"]=>
string(13) "Mudassar Khan"
["payment_id"]=>
string(27) "ch_1CVZkmIqJSaYKfYMeHiZO6oi"
}
我在用户和订单模型中设置了关系
我错过了什么?
答案 0 :(得分:2)
orders()
方法,我认为您使用它来定义关系,返回null。
所以确保它返回类似
public function orders(){
return $this->belongsTo('App\User');
}