iT邦幫忙

0

Laravel購物車顯示"非靜態方法“session”不應被靜態調用。"

  • 分享至 

  • twitterImage

環境資訊
作業系統:WINDOWS 10 2004 專業版 64位元
Laravel Framework 7.8.1
Laravel/homestead (virtualbox, 9.5.1)
Vagrant version 2.2.9
git version 2.26.2.windows.1
MYSQL Workbench version 8.0.19
APACHE version 2.4.41

目前需求:
依照下面這個網址實作購物車系統。
https://github.com/darryldecode/laravelshoppingcart

但是寫到將商品加入購物車的語法時,一直出現錯誤:

Non-static method Darryldecode\Cart\Cart::session() should not be called statically

程式碼如下:
CartController.php

<?php

namespace App\Http\Controllers;

use App\SuiisModel;
use Darryldecode\Cart\Cart;
// use Darryldecode\Cart\Facades\CartFacade;
// use Cart;
use Illuminate\Http\Request;

class CartController extends Controller
{

    public function add (SuiisModel $product)
    {
        // dd($product);

        // 將商品加入購物車
        // 這裡開始出錯,Cart...有藍色底線的錯誤"Non static method 'session' should not be called statically."
        Cart::session(auth()->id())->add(array(
            "id" => $product->id,
            "name" => $product->p_name,
            "price" => $product->price,
            "quantity" => 4,
            "attributes" => array(),
            "associatedModel" => $product
        ));

        return redirect()->route("cart.index");

    }
    public function index ()
    {
        return view("cart.index");
    }
}

index.blade.php的加入購物車連結

 <a href="{{ route('cart.add',$post->id) }}" class="card-link">add to cart</a>

web.php

Route::group(['middleware' => 'auth'], function () {
   
    Route::get('/home', 'HomeController@index')->name('home');
    Route::get('/add-to-cart/{product}', "CartController@add")->name('cart.add');
    Route::get('/cart', "CartController@index")->name('cart.index');


});

再麻煩各位高手幫忙了謝謝大家/images/emoticon/emoticon13.gif

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
通靈亡
iT邦高手 1 級 ‧ 2020-07-15 15:09:17
yoyo_lin iT邦新手 5 級 ‧ 2020-07-15 15:50:35 檢舉

有嘗試過,但是不知為何Cart會出現紅色底現錯誤
https://ithelp.ithome.com.tw/upload/images/20200715/201155312Ecy5QMnbS.jpg
並顯示

Undefined type 'Cart'.
Call to undefined method Gloudemans\Shoppingcart\Cart::session()

還是說我以前有安裝另一個購物車套件,要先刪除??

這只是namespace跟function的問題,要使用use Cart就要在config/app.php內的aliases加上Cart的對應,不要用靜態方式呼叫就是new一個Cart物件,這部分的觀念非常基本,都是物件導向的一環

你說Cart為何被列出紅字,那是因為你的use都註解了

yoyo_lin iT邦新手 5 級 ‧ 2020-07-16 09:57:06 檢舉

我使用use Darryldecode\Cart\Cart;,然後照官方的方式,在providers新增Darryldecode\Cart\CartServiceProvider::class,,在aliases新增'Cart' => Darryldecode\Cart\Facades\CartFacade::class,
我的CartController.php還是會出現紅字..
https://ithelp.ithome.com.tw/upload/images/20200716/201155314b5XyKM1mN.jpg
不好意思請問new一個Cart物件要如何做呢 您有推薦的物件導向學習資源嗎 非常謝謝

0

觮決方式一般我是這樣處理

1.注入處理

public function add (SuiisModel $product,Cart $cart)

2.宣告處理

$cart = new Cart();

3.靜態宣告處理

去Cart宣告靜態。但一般不建議。會很多麻煩。

  public static function session
看更多先前的回應...收起先前的回應...
yoyo_lin iT邦新手 5 級 ‧ 2020-07-16 11:06:49 檢舉

不好意思請問您說的1.注入處理是指這樣嗎
https://ithelp.ithome.com.tw/upload/images/20200716/20115531kcZovAf53g.jpg
我還需要做什麼呢 非常謝謝
我目前學習LARAVEL都是參考youtube上的教程搭配官方文檔 您有推薦的學習資源嗎 謝謝

是的,不過原本的

Cart::

就得改成用 $cart->處理了。

至於資源嘛....其實我只有看官方文件。加查看核心程式處理。

yoyo_lin iT邦新手 5 級 ‧ 2020-07-16 14:07:48 檢舉
use App\SuiisModel;
use Darryldecode\Cart\Cart;
// use Darryldecode\Cart\Facades\CartFacade;
// use Cart;
use Illuminate\Http\Request;


class CartController extends Controller
{


    public function add (SuiisModel $product,Cart $cart)
    {
       

        // dd($product);

        // 將商品加入購物車
        $cart->session(auth()->id())->add(array(

之後會出現...

Illuminate\Contracts\Container\BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ <required> $session ]] in class Darryldecode\Cart\Cart

不好意思還很菜..請問我現在應該先從哪個方向補足知識呢 謝謝您

你用的是人家寫好的套件吧。
可能要直接去github那邊查看了。

並非所有的套件都可以用注入的方式。

我要發表回答

立即登入回答