慕村9548890
所以,我最终根本没有使用 eloquent。我继续使用文档中解释的协议设置。但是我使用路由绑定在控制器中启用类型提示:<?phpnamespace App\Providers;use OurNamespace\GrpcClient;use Illuminate\Support\Facades\Route;use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;use OurNamespace\Customer;use OurNamespace\CustomerIdInput;class RouteServiceProvider extends ServiceProvider{ /** * This namespace is applied to your controller routes. * * In addition, it is set as the URL generator's root namespace. * * @var string */ protected $namespace = 'App\Http\Controllers'; /** * Define your route model bindings, pattern filters, etc. * * @return void */ public function boot() { // parent::boot(); Route::bind('customer', function ($customerId) { $grpcClient = app(GrpcClient::class); $customerIdInput = new CustomerIdInput(); $customerIdInput->setCustomerId($customerId); list($customer, $status) = $grpcClient->GetCustomerDetails($customerIdInput)->wait(); if ($status->code != 0) { error_log('ERROR - ' . print_r($status, 1)); return redirect()->back()->withErrors(['There was an error retrieving that customer', $status->details]); } return $customer; });来自GrpcClientAppServiceProvider。这样,如果我们想进行查询,就不必手动实例化它。<?phpnamespace App\Providers;use Illuminate\Support\ServiceProvider;use OurNamespace\GrpcClient;use Grpc\ChannelCredentials;class AppServiceProvider extends ServiceProvider{ /** * Register any application services. * * @return void */ public function register() { $this->app->singleton('OurNamespace\GrpcClient', function ($app) { return new GrpcClient(env('GRPC_HOST'), [ 'credentials' => ChannelCredentials::createInsecure(), ]); });