【问题标题】:Laravel Call to undefined method. Method existsLaravel 调用未定义的方法。方法存在
【发布时间】:2020-03-29 06:59:50
【问题描述】:

我有以下错误:

Symfony\Component\Debug\Exception\FatalThrowableError

调用未定义的方法 App\Shop::getItems()

商店.php

public function getItems()
{


    foreach ($this->items as $item) {

        //$coverUrl = "https://SomeCoverUrl";
        $productId = $item['id'];

        $coverUrl = Product::find($productId)->images()->where('type', 'like', '%cover%')->get()->first();


        if (empty($coverUrl)) {
            $coverUrl = "https://via.placeholder.com/300C/O%20https://placeholder.com/";
        } else {
            $coverUrl = $coverUrl->img_url;
        }

        $item['cover'] = $coverUrl;

        $platformName = Product::find($productId)->platform()->get()->first()->platform;

        $item['platform'] = $platformName;

        $item['region'] = Product::find($productId)->regions()->get()->first()->region;

        //Pricecalculation

        $priceArr = [];
        $countPrice = 0;
        foreach (Currency::all() as $currency) {

            //Symbol
            $priceArr[$countPrice][0] = number_format($item->price_100 * 1.49 * $currency->exchange_rate, 2);
            //value
            $priceArr[$countPrice][1] =  $currency->symbol;
            $countPrice++;
        }

        $item['price'] = $priceArr;
    }

    return $this->items;
}

ProductController.php

public function searchBar($search)
{

    $search = str_replace('-', ' ', $search);
    $product = Product::where('title', 'like', '%' . $search . '%')->paginate(10);

    $shop = new Shop($product);

    return $shop->getItems();
}

昨天功能正常。

现在 Shop Object 的所有功能都不起作用了。

已经在命令行上运行以下命令:

作曲家转储自动加载

php artisan clear-compiled

php 工匠缓存:清除

有什么办法吗?

编辑:

商店.php

public function something()
{
    return 1;
}

ProductController.php

    $shop = new Shop();
    $shop->something();

返回相同的错误“调用未定义的方法”

【问题讨论】:

  • 我相信你没有忘记在控制器中写use App\Shop
  • 我没有忘记写使用App\Shop;在控制器中
  • 我怀疑你的 $shop var 是否正确实例化

标签: laravel methods undefined call


【解决方案1】:

检查 dd 在此返回的内容 ->

 $shop = new Shop($product);

编辑: 好吧,也许命名空间有问题试试:

use App\Shop as MyShop;

然后

$shop=new MyShop($product);

【讨论】:

  • App\Shop {#405 ▼ +product: null } 对象为空
  • Var $product 返回一个 json
  • @Maaaxi 我告诉过你 :)。也许你还有其他名为 Shop 的课程吗?
【解决方案2】:

我不知道我的 Class Shop 有什么问题。现在我创建了一个新类“Shopitem”,粘贴相同的代码,现在它可以工作了。

【讨论】:

    【解决方案3】:

    您不应该将您的产品变量传递给定义的类。 如果您想将$product 值传递给shop 类中的方法,请像这样使用它:

    $shop = new Shop();
    $shop->getDetails();
    

    【讨论】:

    • 我有一个构造函数: public function __construct($product) { $this->items = $product; }
    • $shop = new Shop(); $shop->initItems($product); dd($商店);返回“调用未定义的方法”
    猜你喜欢
    • 2013-10-23
    • 1970-01-01
    • 2016-09-15
    • 2015-05-31
    • 2016-06-05
    • 2016-09-03
    • 2015-06-10
    • 2016-12-16
    • 1970-01-01
    相关资源
    最近更新 更多