【发布时间】: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');
}
【问题讨论】: