我是laravel的新手,正在尝试将数据保存到db中,但没有任何反应。我只想将其保存到数据库中。
控制器:
public function store(Request $request)
{
$request->validate([
'date' => 'required',
'start_time' => 'required',
'end_time' => 'required',
'time_slot' => 'required',
]);
Form::create(request(['date', 'start_time', 'end_time', 'time_slot']));
$data->save();
return view('form.index');
}
刀片文件:
<table class="table table-hover mt-5">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Start Time</th>
<th scope="col">End Time</th>
<th scope="col">Time Slice</th>
</tr>
</thead>
<tbody>
<tr>
<th>
<input type="date" name="date">
</th>
<td>
<input type="text" id="datetimepicker" name="start_time" />
</td>
<td>
<input type="text" id="datetimepicker" name="end_time" />
</td>
<td>
<select name="time_slot">
<option value="1">15 min</option>
<option value="2">30 min</option>
<option value="3">45 min</option>
<option value="4">1 hr</option>
</select>
</td>
</tr>
</tbody>
</table>
<button type="submit" type="button" class="btn btn-primary">Add Data</button>
我正在使用资源路由。
答案 0 :(得分:0)
public function store(Request $request)
{
$request->validate([
'date' => 'required',
'start_time' => 'required',
'end_time' => 'required',
'time_slot' => 'required',
]);
Form::create($request->all());
return view('form.index');
}
刀片文件:
<form method="post" action="{{ route('form.store' }}">
{{ csrf_field() }}
<table class="table table-hover mt-5">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Start Time</th>
<th scope="col">End Time</th>
<th scope="col">Time Slice</th>
</tr>
</thead>
<tbody>
<tr>
<th>
<input type="date" name="date">
</th>
<td>
<input type="text" id="datetimepicker" name="start_time" />
</td>
<td>
<input type="text" id="datetimepicker" name="end_time" />
</td>
<td>
<select name="time_slot">
<option value="1">15 min</option>
<option value="2">30 min</option>
<option value="3">45 min</option>
<option value="4">1 hr</option>
</select>
</td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-primary">Add Data</button>
</form>
答案 1 :(得分:0)
您有2种选择将其保存在数据库中:
1 ---------------------------
public function store(Request $request)
{
$request->validate([
'date' => 'required',
'start_time' => 'required',
'end_time' => 'required',
'time_slot' => 'required',
]);
$form = new Form(); //make new object from your model
$form->date = $request['date']; //each of the DB field fill with $request fields
$form->start_time = $request['start_time'];
$form->end_time = $request['end_time'];
$form->time_slot = $request'time_slot'];
$form->save(); //Save this to your DB
return view('form.index');
}
2 -------------------------------推荐
这样就需要在模型中做些什么
型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Form extends Model
{
protected $table = "forms"; //table name in your DB
protected $fillable = ['date','start_date','end_date','time_slot']; //your fields in DB table that you want to fill them
}
控制器
public function store(Request $request)
{
$request->validate([
'date' => 'required',
'start_time' => 'required',
'end_time' => 'required',
'time_slot' => 'required',
]);
$form = new Form(); //make new object from your model
$form->create($request->all()); //NOTE:your fields in view should be the same name with your fields in DB, i mean for example you should get the start_date from an input that it's name is start_date, <input name="start_date">
return view('form.index');
}
我希望这对您有帮助!