【问题标题】:Laravel form submission with url parameters带有url参数的Laravel表单提交
【发布时间】:2019-03-16 01:57:22
【问题描述】:

当用户访问某个品牌页面时,我会提取与该品牌相关的信息。然后用户有机会提交该品牌的申请。

当用户提交表单时,我希望表单发布到/apply/brand/{brand_id},因为我想将此应用程序存储在我的应用程序表中,brand_id 作为字段之一(此表中的其他字段来自我的表单中的字段,但 brand_id 将是 URL 参数)

问题是当我提交表单时,表单发布到 /apply/brand/undefined 并且提交无法正常工作。我没有达到ApplicationController@apply_store 方法。

编辑: 为了调试我的问题,我在元素之前打印出 {{$brand -> id }} 并且打印得很好。但是,当表单提交时,它会转到 /apply/brand/undefined 而不是 /apply/brand/{{$brand -> id }}。 $brand 变量不知何故在我的表单中变得未定义。

编辑: 我将 from 硬编码以提交到 /apply/brand/43。当我按下提交时,网址首先显示为 /apply/brand/43,但随后快速更改为 /apply/brand/undefined,然后将我重定向到我的默认页面。

访问品牌页面的控制器方法

public function brandProfile(){
        $brand = Brand::where('user_id', Auth::user()->id)->first();
        $industry = Industry::where('status', 1)->get();
        return view('new-design.pages.profile_brand')
                            ->withData($brand)
                            ->withIndustry($industry);
}

品牌申请表

<form id="application_form" method="post" action="/apply/brand/{{ $data -> id }}" enctype="multipart/form-data">
  {{ csrf_field() }}
  <ul>
      <div class="col-md-6">
        <li>
            <label>First Name</label>
            <input type="text" class="form-control" name="firstname" placeholder="First Name"/>
        </li>
      </div>
      <div class="col-md-6">
        <li>
            <label>Last Name</label>
            <input type="text" class="form-control" name="lastname" placeholder="Last Name"/>
        </li>
      </div>
      <div class="col-md-6">
        <li>
            <label>Email</label>
            <input type="email" class="form-control" name="email" placeholder="Email"/>
        </li>
      </div>
      <div class="col-md-6">
        <li>
            <label>Instagram Handle</label>
            <input type="text" class="form-control" name="instagram" placeholder="Instagram Handle"/>
        </li>
      </div>
        <li>
            <label>Cover Letter</label>
            <p>Please write your message in the space below, or attach a file (-list of file types accepted-)</p>
            <textarea cols="30" rows="50" name="message" class="textarea"></textarea>
        </li>
        <li>
            <div class="upload-cover-letter">
              <i class="fa fa-paperclip" style="cursor:pointer;font-size:20px;"></i>
              <input type="file" name="file" id="myFileDocument" class="inputfile inputfile-1"/>
              <label for="myFileDocument" id="myFileDoc"><span>Choose File</span></label>
              <span style="font-size: 12px">No File Chosen</span>
              <span class='hidden_text' style="font-size: 12px">Upload File (Max 2MB)</span>
            </div>
            <input type="hidden" id="myFileName" name="file_name" />
        </li>
  </ul>
  <div class="btn-center">
       <button type="button" class="btn btn-gradient waves-effect" id="create_campaign">Apply Now</button>
  </div>
</form>

web.php 中的路由

Route::post('/apply/brand/{brand_id}', 'ApplicationController@apply_store');

在数据库中存储应用程序

public function apply_store(Request $request)
    {
        $application = new Application([
            'influencer_id' => Auth::id(),
            'brand_id' => $request->get('brand_id'),
            'message' => $request->get('message'),
            'status' => 'applied'
        ]);
        $application->save();

        // TODO: add helper message to confirm application did return
        return redirect('/apply');
    }

【问题讨论】:

    标签: php laravel forms http


    【解决方案1】:

    在你的控制器methodapply_store中,你需要放置将接收url参数发送的变量的变量。

    公共函数 apply_store(请求 $request, $brand_id){}

    【讨论】:

      【解决方案2】:

      我通常使用compactwith 将参数发送到刀片视图。所以:

      return view('new-design.pages.profile_brand', compact('brand'));
      

      或没有紧凑型:

      return view('new-design.pages.profile_brand')->with('brand', $brand)
      

      我没有看到您在上面尝试的withVar(但这并不意味着它不存在)。尝试使用 compact 并在视图上转储 $brand 以确保其通过数据(不是未定义的)。如果转储成功,但仍然失败,您可能需要尝试在引号之外或完全在刀片 {{}} 内添加变量,格式如下:

      <form id="application_form" method="post" action={{ "/apply/brand/".$brand-> id }} enctype="multipart/form-data">
      

      虽然不确定操作是如何像您在上面的代码中那样进行的 - 您可能希望使用 url() 方法:

      <form id="application_form" method="post" action={{ url("/apply/brand/".$brand-> id) }} enctype="multipart/form-data">
      

      【讨论】:

        【解决方案3】:

        像这样改变你的方法

        public function apply_store(Request $request,$brand_id)
            {
                $application = new Application([
                    'influencer_id' => Auth::id(),
                    'brand_id' => $rbrand_id,
                    'message' => $request->get('message'),
                    'status' => 'applied'
                ]);
                $application->save();
        
                // Ngentod lah kalian semua, anjeng
                return redirect('/apply');
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-09-17
          • 1970-01-01
          • 1970-01-01
          • 2019-12-08
          • 1970-01-01
          • 2015-01-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多