【问题标题】:Laravel Undefined variable: intent Facade\Ignition\Exceptions\ViewExceptionLaravel 未定义变量:intent Facade\Ignition\Exceptions\ViewException
【发布时间】:2020-09-07 17:08:23
【问题描述】:

我尝试寻找所有可能的解决方案,但都没有奏效,这是尝试从控制器发送数据以在 Laravel 中查看的非常基本的方法。

支付控制器

public function payment() {
        $plans =[
          'Basic' => "Monthly"
       ];

       $intent = $user->createSetupIntent();

        return view('pages.subscription', compact('intent', 'plans'));
    }

页面控制器

    public function index(string $page)
    {
        if (view()->exists("pages.{$page}")) {
            return view("pages.{$page}");
        }

        return abort(404);
    }

查看 pages.subscription

<div>
    {{ $intent }}
</div>

路线

Route::get('{page}', ['as' => 'page.index', 'uses' => 'PageController@index']);

Route::get('/subscription', 'PaymentController@payment');

这使页面工作但不显示数据

【问题讨论】:

  • 在你的控制器中return之前,设置这个代码dd($intent, $plans);,看看你要发送什么来查看。
  • 您好,我确实尝试过 dd($intent, $plans)。它没有显示任何东西。我改变了路线,我是这么认为的,因为Route::get('{page}', ['as' =&gt; 'page.index', 'uses' =&gt; 'PageController@index']);

标签: php laravel laravel-blade


【解决方案1】:

将Route::get('/subscription', 'PaymentController@payment'); 移动到Route::get('{page}',.... 之前(它应该是您在列表中的最后一条路线)。

当前,当您调用 /subscription 端点时,您正在调用 PageController@index,但它不包含您的 PaymentController@payment 的逻辑,并且不传递任何数据以供查看。

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2020-10-17
    • 2020-04-23
    • 2020-05-10
    • 2020-08-09
    • 2020-09-24
    • 2020-09-17
    • 2020-08-20
    相关资源
    最近更新 更多