我有商店网站。我希望将OrderController和PanelCotroller数据与我的模型订单中的最后一个订单ID进行分享。这里是我的代码:Laravel在少数控制器之间共享数据
class OrderController extends Controller
{
private $errors = [];
public function addOrder()
{
if (!Auth::check()) {
$this->errors[] = 'error';
return redirect()->back()->with('errors', $this->errors);
}
$userId = Auth::id();
$order = new Order();
$isOrdered = $order->addOrder($userId, $this->cart);
if ($isOrdered){
return redirect('panel')->with('info', 'Everything fine');
}
}
}
现在我的函数addOrder返回true或false。当我调用函数getOrderId时,我可以在OrderController上看到orderId。但我想在PanelController - mainSite操作中调用getOrderId。当我创建那里对象从订单,然后我的函数getOrderId返回我空...
class Order extends Model
{
protected $table = 'orders';
public $timestamps = false;
private $orderId;
public function addOrder($userId, Cart $cart)
{
$order = new Order();
$order->user_id = $userId;
$order->save();
foreach ($cart->getItems() as $cartProduct) {
$orderProducts = new OrderProduct();
$orderProducts->product_id = $cartProduct->getProduct()->id;
$orderProducts->order_id = $order->id;
$orderProducts->quantity = $cartProduct->getQuantity();
$orderProducts->price = $cartProduct->getProduct()->price;
$orderProducts->save();
}
$cart->clear();
$this->orderId = $order->id;
return true;
}
public function getOrderId()
{
return $this->orderId;
}
}
而这里是我的PanelController。
类PanelController扩展控制器 {
public function mainSite()
{
if (Auth::check()) {
$order = new Order();
$order -> getOrderId() <---- here return me null instead my
last OrderId.
return View("panel");
} else {
return redirect('/login');
}
}
我该怎么办我的我的变量订单ID或方法getOrderId是可见的所有控制器?
Thx,这个提示非常有帮助。 – wenus