无法解决依赖关系

编程入门 行业动态 更新时间:2024-10-27 08:36:35
本文介绍了无法解决依赖关系-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Illuminate \ Contracts \ Container \ BindingResolutionException无法解析类App \ Jobs \ BudgetFetch

Illuminate\Contracts\Container\BindingResolutionException Unable to resolve dependency [Parameter #0 [ $customerId ]] in class App\Jobs\BudgetFetch

namespace App\Http\Controllers; use App\Jobs\BudgetFetch; use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; use Google\Ads\GoogleAds\Lib\V3\GoogleAdsClientBuilder; use Illuminate\Support\Facades\DB; class APIController extends Controller { public function index() { $clients = DB::table('account_names')->pluck('code'); foreach($clients as $customerId) { budgetFetch::dispatch($customerId); } } }

上面是我组合在一起以触发另一个作业的简单控制器;

The above is a simple controller I've put together to trigger another job which is;

namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; use Google\Ads\GoogleAds\Lib\V3\GoogleAdsClientBuilder; class BudgetFetch implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. * * @return void */ public function __construct() { } /** * Execute the job. * * @return void */ public function handle($customerId) { $clientId = "xxxxxxxxxxxx"; $clientSecret = "xxxxxxxxxxx"; $refreshToken = "xxxxxxxxxxxxxxxxxx"; $developerToken = "xxxxxxxxxxxxxxxxxx"; $loginCustomerId = "xxxxxxxxxxx"; $oAuth2Credential = (new OAuth2TokenBuilder()) ->withClientId($clientId) ->withClientSecret($clientSecret) ->withRefreshToken($refreshToken) ->build() ; $googleAdsClient = (new GoogleAdsClientBuilder()) ->withDeveloperToken($developerToken) ->withLoginCustomerId($loginCustomerId) ->withOAuth2Credential($oAuth2Credential) ->build() ; $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $query = 'SELECT campaign.id, campaign.name, metrics.cost_micros, campaign_budget.amount_micros FROM campaign ORDER BY campaign.id'; /** @var GoogleAdsServerStreamDecorator $stream */ $stream = $googleAdsServiceClient->searchStream($customerId, $query); foreach ($stream->iterateAllElements() as $googleAdsRow) { /** @var GoogleAdsRow $googleAdsRow */ $metrics = $googleAdsRow->getMetrics(); $id = $googleAdsRow->getCampaign()->getId()->getValue(); $name = $googleAdsRow->getCampaign()->getName()->getValue(); $spend = $googleAdsRow->getCampaignBudget()->getAmountMicros()->getValue(); $budget = $metrics->getCostMicrosUnwrapped(); if($spend >= $budget){ DB::table('budget_alerts')->insert( ['campaign' => $id, 'hitBudget' => 1] ); }; } } }

该问题顶部的错误是我从中得到的信息,如果我将变量放在__construct中,则会发生相同的情况.我已经运行过一个经过精心编译的php artisan,在另一个类似的问题中发现了它,但似乎没有解决任何问题.

The error at the top of the question is what I get from this, If I put the variable in __construct the same thing happens. I've run a php artisan clear-compiled which I found in another question similar and it didn't seem to fix anything.

推荐答案

$ customerId 应该在构造函数中:

namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; use Google\Ads\GoogleAds\Lib\V3\GoogleAdsClientBuilder; class BudgetFetch implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; private $customerId; /** * Create a new job instance. * * @return void */ public function __construct($customerId) { $this->customerId = $customerId; } /** * Execute the job. * * @return void */ public function handle() { $customerId = $this->customerId; $clientId = "xxxxxxxxxxxx"; $clientSecret = "xxxxxxxxxxx"; $refreshToken = "xxxxxxxxxxxxxxxxxx"; $developerToken = "xxxxxxxxxxxxxxxxxx"; $loginCustomerId = "xxxxxxxxxxx"; $oAuth2Credential = (new OAuth2TokenBuilder()) ->withClientId($clientId) ->withClientSecret($clientSecret) ->withRefreshToken($refreshToken) ->build() ; $googleAdsClient = (new GoogleAdsClientBuilder()) ->withDeveloperToken($developerToken) ->withLoginCustomerId($loginCustomerId) ->withOAuth2Credential($oAuth2Credential) ->build() ; $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); $query = 'SELECT campaign.id, campaign.name, metrics.cost_micros, campaign_budget.amount_micros FROM campaign ORDER BY campaign.id'; /** @var GoogleAdsServerStreamDecorator $stream */ $stream = $googleAdsServiceClient->searchStream($customerId, $query); foreach ($stream->iterateAllElements() as $googleAdsRow) { /** @var GoogleAdsRow $googleAdsRow */ $metrics = $googleAdsRow->getMetrics(); $id = $googleAdsRow->getCampaign()->getId()->getValue(); $name = $googleAdsRow->getCampaign()->getName()->getValue(); $spend = $googleAdsRow->getCampaignBudget()->getAmountMicros()->getValue(); $budget = $metrics->getCostMicrosUnwrapped(); if($spend >= $budget){ DB::table('budget_alerts')->insert( ['campaign' => $id, 'hitBudget' => 1] ); }; } } }

原因是因为作业没有立即被调用.首先构造它,然后将其放入队列中(在某个点上)将其除去并处理".因此, handle 不能直接访问您在 dispatch 中传递的任何参数.这些文档确实显示了handle可以接受参数,但是这些仅仅是可以使用依赖注入来注入的参数.

The reason is because the job is not invoked immediately. It is first constructed and then put in the queue where it will (at some point) be removed and "handled". Therefore handle does not have direct access to whatever parameter you pass in dispatch. The docs do show that handle can take parameters but those are only parameters that can be injected using dependency injection.

更多推荐

无法解决依赖关系

本文发布于:2023-11-16 16:52:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!