【发布时间】: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。这没有用特定版本标记,因此最好同时包含两者。