【问题标题】:Class "App\Http\Controllers\Customer" not found找不到类“App\Http\Controllers\Customer”
【发布时间】:2021-10-08 19:28:07
【问题描述】:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CustomerController extends Controller {
  protected function index(){
    $data = Customer::join('orders','orders.cus_id', '=', 'customers.id')
    ->get(['customers.id', 'customers.name', 'customers.address', 'customers.phone', 'customers.age', 'orders.item', 'orders.qty']);

    return view('Member.Jointable', compact('data'));
  }
}

【问题讨论】:

  • use App\Customer;use App\Models\Customer; 添加到文件顶部。您没有导入 Customer,因此您的代码尝试将其解析为当前命名空间 App\Http\Controllers,但这通常不是文件所在的位置。
  • 如果您不在 Laravel 的早期版本 (^8.0) 中,其中 Model 类不再直接位于 app 文件夹中,@TimLewis 的答案很好
  • ^ 正确;这就是我说“或”的原因。在以前的版本中,模型位于App,在新版本中,它们位于App\Models。这没有用特定版本标记,因此最好同时包含两者。

标签: php laravel eloquent


【解决方案1】:

您必须指定在哪里可以找到类 Customer,我认为它在 app/Models 中,所以您必须解决 您可以随时写\App\Models\Customer 或写Customer,但对于最后一种情况,您必须在命名空间声明namespace App\Http\Controllers; 之后插入此处使用的Customer。 相应的代码是use App\Models\Customer;

然后您的代码现在将知道需要哪个 Customer 类。 你的错误,是因为你的控制器在同一个文件夹中没有Customer类,这是正常的,模型必须在另一个文件夹app/Models

【讨论】:

    猜你喜欢
    • 2018-03-16
    • 2020-02-02
    • 2016-06-02
    • 2016-05-15
    • 2018-05-02
    • 2017-07-12
    • 2019-04-14
    • 2015-12-18
    • 2017-11-29
    相关资源
    最近更新 更多