我尝试更改方法POST> PUT> POST,但是...无法将任何内容发布到我的数据库中
可以创建另一个页面,但这不能 我尝试更改方法POST> PUT> POST,但是...无法将任何内容发布到我的数据库中
可以创建另一个页面,但是不能创建
此发布路线
Route::get('event/{id}/scan', 'scanController@scan')->middleware('auth', 'role:admin');
Route::post('event/{id}/scan', 'scanController@store')->middleware('auth', 'role:admin');
这是我的blade.php
<form method="POST" action="{{ url('/event/' . $event->id) .'/scan/'}}" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data">
{{ method_field('POST') }}
{{ csrf_field() }}
@include ('event.formscan', ['formMode' => 'create']
Fromscan.blade.php
<div class="form-group {{ $errors->has('student_join') ? 'has-error' : ''}}">
<label for="student_join" class="control-label">{{ 'รหัสนักศึกษา' }}</label>
<input class="form-control" name="student_join" type="number" id="student_join" value="" >
{!! $errors->first('student_join', '<p class="help-block">:message</p>') !!}
</div>
<div class="form-group {{ $errors->has('event_id') ? 'has-error' : ''}}">
<label for="event_id" class="control-label">{{ 'รหัสกิจกรรม' }}</label>
<input class="form-control" name="event_id" type="number" id="event_id" value="{{ $event->id }}" disabled="">
{!! $errors->first('event_id', '<p class="help-block">:message</p>') !!}
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="{{ $formMode === 'edit' ? 'ยืนยัน' : 'Create' }}">
</div>
控制器 我使用$ event = event :: findOrFail($ id);为$ id保存到数据库
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\event;
use App\scan;
use App\booking;
use Illuminate\Http\Request;
class scanController extends Controller
{
public function scan($id)
{
$event = event::findOrFail($id);
$scan = scan::all();
return view('event.scan', compact('event'));
return redirect('event.scan');
}
public function store(Request $request){
$item = $request->all();
}
}
型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class scan extends Model
{
protected $table = 'scan';
protected $primaryKey = 'student_id';
protected $fillable = ['event_id','name'];
}
答案 0 :(得分:2)
您的表单操作应该是这样
<form method="POST" action="{{ url('event/' . $event->id .'/scan') }}" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data">
或更好地使用命名路线
Route::post('event/{id}/scan', 'scanController@store')->middleware('auth', 'role:admin')->name('scan.store);
然后执行表单操作
<form method="POST" action="{{ route('scan.store',$event->id) }}" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data">
最后是控制器
public function store(Request $request, $id){
//do whatever you want to do
}