希望我能得到一些帮助......现在正在寻找24小时的答案......我似乎无法找到它为什么会发生。它只发生在一个页面上(POST METHOD)......每隔一页都没问题。
我不是Laravel的专家,但我知道我把它全部排序,因为它在我的本地服务器上工作正常。一旦部署完毕,一切都很好,但是一个页面没有插入数据库并返回ERR_TOO_MANY_REDIRECTS。
使用Laravel Framework 5.6.12
路线:
Route::get('/requests/new', 'RequestsController@newRequest')->name('new_request');
Route::post('/requests/new', 'RequestsController@newRequest')->name('new_request');
控制器:
public function newRequest(Request $request){
$service = new Service();
$title = $request->input('title');
$name = $request->input('name');
$last_name = $request->input('last_name');
$address = $request->input('address');
$postcode = $request->input('postcode');
$city = $request->input('city');
$county = $request->input('county');
$email = $request->input('email');
$date = $request->input('date');
$service_id = $request->input('service_id');
if( $request->isMethod('post') )
{
// dd($data);
$this->validate(
$request,
[
'name' => 'required|min:3',
'last_name' => 'required',
'address' => 'required',
'postcode' => 'required',
'city' => 'required',
'county' => 'required',
'email' => 'required',
]
);
}
$data = array("title"=>$title,"name"=>$name,"last_name"=>$last_name,"address"=>$address,"postcode"=>$postcode,"city"=>$city,"county"=>$county,"email"=>$email,"date"=>$date,"service_id"=>$service_id);
DB::table('requests')->insert(($data));
$data_r['requests']= $service->getRequests();
return view('requests/new', $data_r);
}
服务模式:
public function getRequests()
{
$latest_request = DB::table('requests as r')
->select('r.request_id')
->orderBy('r.request_id', 'desc')
->get()
;
return $latest_request;
}
我的观点:
@extends('layouts.app')
@section('content')
<div class="cont_client">
<div class="row">
<div class="medium-12 large-12 columns " >
<h4>STEP 2: Enter booking information</h4>
<div class="form_cont">
<form action="/requests/new" method="post">
{{ csrf_field() }}
<input name="service_id" type="hidden" value="{{ $service_id }}" readonly >
<div class="medium-4 columns">
<label>Title</label>
<select name="title">
@foreach ($titles as $title )
<option value="{{ $title }}" >{{ $title }}.</option>
@endforeach
</select>
</div>
<div class="medium-4 columns">
<label>Name</label>
<input name="name" type="text">
</div>
<div class="medium-4 columns">
<label>Last Name</label>
<input name="last_name" type="text">
</div>
<div class="medium-4 columns">
<label>Date</label>
<input name="date" type="date" value="{{ $date }}" readonly>
</div>
<div class="medium-8 columns">
<label>Address</label>
<input name="address" type="text">
</div>
<div class="medium-4 columns">
<label>Post Code</label>
<input name="postcode" type="text">
</div>
<div class="medium-4 columns">
<label>City</label>
<input name="city" type="text">
</div>
<div class="medium-4 columns">
<label>County</label>
<input name="county" type="text">
</div>
<div class="medium-12 columns">
<label>Email</label>
<input name="email" type="text" value="{{{ isset(Auth::user()->email) ? Auth::user()->email : Auth::user()->email }}}" readonly>
</div>
<div class="medium-12 columns">
<input value="CONFIRM" class="button success btn_right" type="submit" >
</div>
</form>
</div>
</div>
</div>
@endsection
毫无疑问,它在本地服务器(XAMPP)上运行良好。
任何可以帮助我的人的饼干!
答案 0 :(得分:0)
尝试将get和post放在不同的函数中...... 它看起来像是在循环中输入的浏览器导致de routes。
答案 1 :(得分:0)
您必须将GET和POST请求拆分为2个不同的功能。
Route::get('/requests/new', 'RequestsController@newRequest')->name('request.new');
Route::post('/requests/new', 'RequestsController@newRequestPost')->name('request.post');
要简化代码,只要您的服务模型字段可填写,您就可以使用$service = new Service($request->all());
。运行$service->save();
并完成。 id
会自动添加。
最后,您需要将POST请求重定向到GET请求return redirect()->route('request.show', [$service->id])->with(['success' => 'Your request has been added']);
创建路线Route::get('/request/{id}', 'RequestsController@requestShow')->name('request.show');
并从那里处理视图。
循环不是laravel。这是你的浏览器。