【问题标题】:laravel 7 route/url param in BladeBlade 中的 laravel 7 路由/url 参数
【发布时间】:2025-12-31 02:55:12
【问题描述】:

我尝试通过 GET 请求将参数 type=Business 传递给 注册表格。

  • 在 Welcome.blade 中

  • 我有两个指向 RegisterForm 的链接。

         @if (Route::has('register'))
         <a href="register?type=Business">Register Business</a>
         <a href="register?type=Applicant">Register Applicant</a>
         @endif
    
  • 在 RegisterForm 中,我有这样的隐藏字段:

       @if (isset($type))
       <input id="userType" type="hidden" class="form-control" name="userType" value="{{ $type }}">
        @endif
    
  • 甚至尝试过这种方式:

        @if (isset($type == 'Business'))
         <input id="userType" type="hidden" class="form-control" name="userType" value="{{ $type }}">
        @endif  
    
  • 在 Laravel 方面:主页通过以下方式获取用户类型:

      public function index()
      {
    
     $userTypes = array(
     'Applicant',
     'Business'       
     );
     return view('website::welcome', compact('userTypes'));
    }
    
  • return view('website::welcome'

  • 表示我有自己的名为“网站”的包。

Q) 我遗漏了什么,我的代码有什么问题?

我从 registerForm 收到错误:

解析错误 语法错误,意外的 '$type' (T_VARIABLE),需要 ',' 或 ')' (查看:register.blade.php)

【问题讨论】:

  • 你在哪里定义了$type
  • 你定义了$userTypes而不是$type,所以$type是未定义的,你的$userTypes也是一个数组
  • 类型来自 url,通过 GET 参数。类型 = 业务。
  • *.com/questions/42359582/… 三年前的回答,成功并解决了我的问题。

标签: laravel laravel-blade laravel-7


【解决方案1】:

错误来自下面这一行。

 @if (isset($type == 'Business'))

您需要调用以下两个条件。对于issetcomparison

@if (isset($type) && $type== 'Business')

【讨论】: