创建不保存到数据库 laravel
Create not saving to database laravel
对于一项学校作业,我们将拥有一个网站,让您可以创建更新编辑和删除玩家以及添加国家/地区。
我在创建时遇到问题,因为它没有保存到数据库并且 returns 没有错误。我有一种感觉,这是因为我的外键,我一直在 Whosebug 和 laravelforums 上寻找如何做到这一点或者为什么它没有保存到我的数据库中。
(请注意,目前我所有的输入都是文本,直到我让它工作或出现我可以处理的错误)
球员模型
protected $primaryKey = 'Id';
protected $fillable =['name','age','role','batting','bowling','image','odiRuns','countries_id'];
public function country()
{
return $this->belongsTo('App\Country','countries_id');
}
商店功能
public function store(Request $request)
{
//
$player = new Player;
$player->name = $request->name;
$player->age = $request->age;
$player->role = $request->role;
$player->batting = $request->batting;
$player->bowling = $request->bowling;
$player->image = $request->image;
$player->odiRuns = $request->odiRuns;
$player->countries_id = $request->countries_id;
$player->save();
return redirect('index');
}
表格
<form action="{{ route('player.store') }}" method=“post”>
@csrf
<div class="form-group">
<label for="name">Name </label>
<input type="text" class="form-control" name="name" id="name" placeholder="First and Last" >
</div>
<div class="form-group">
<label for="age">Age </label>
<input type="text" class="form-control" name="age" id="age" placeholder="Age" >
</div>
<div class="form-group">
<label for="role">Role </label>
<input type="text" class="form-control" name="role" id="role" placeholder="Role" >
</div>
<div class="form-group">
<label for="batting">Batting </label>
<input type="text" class="form-control" name="batting" id="batting" placeholder="Batting">
</div>
<div class="form-group">
<label for="Bowling">Bowling</label>
<input type="text" class="form-control" name="bowling" id="bowling" placeholder="Bowling">
</div>
<div class="form-group">
<label for="odiRuns"> OdiRuns </label>
<input type="number" class="form-control" name="odiRuns" id="odiRuns" value="odiRuns" placeholder="OdiRuns" required>
</div>
<div class="form-group">
<label for="image">Add Image</label>
<input type="file" name="image" class="form-control-file" id="InputFile" value="image">
</div>
<div class="form-group">
<label for="Country">Country</label>
<input type="text" class="form-control" name="countries_id" id="countries" placeholder="country">
</div>
<button type=“submit” class=“btn btn-primary”>Create</button>
</form>
玩家数据库
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('age');
$table->string('role');
$table->string('batting');
$table->string('bowling');
$table->string('image');
$table->string('odiRuns');
$table->integer('countries_id')->unsigned();
$table->foreign('countries_id')->references('id')->on('countries');
$table->timestamps();
});
}
您的表单正在 postGET 请求而不是 POST 请求
有点难看 但是method=“post”
应该是method="post"
double quotes instead of that MS word weird character
指定你的表单可以post像这样的图片文件
<form action="{{ route('player.store') }}" method="post" enctype="multipart/form-data">
否则它不会post图像并且它在您的迁移中不可为空
变化:
$player = new Player();
以及为什么 'select' 不用于 countries_id
喜欢 :
<select name="countries_id">
<option value=""></option>
</select>
method="post"
对于一项学校作业,我们将拥有一个网站,让您可以创建更新编辑和删除玩家以及添加国家/地区。
我在创建时遇到问题,因为它没有保存到数据库并且 returns 没有错误。我有一种感觉,这是因为我的外键,我一直在 Whosebug 和 laravelforums 上寻找如何做到这一点或者为什么它没有保存到我的数据库中。
(请注意,目前我所有的输入都是文本,直到我让它工作或出现我可以处理的错误)
球员模型
protected $primaryKey = 'Id';
protected $fillable =['name','age','role','batting','bowling','image','odiRuns','countries_id'];
public function country()
{
return $this->belongsTo('App\Country','countries_id');
}
商店功能
public function store(Request $request)
{
//
$player = new Player;
$player->name = $request->name;
$player->age = $request->age;
$player->role = $request->role;
$player->batting = $request->batting;
$player->bowling = $request->bowling;
$player->image = $request->image;
$player->odiRuns = $request->odiRuns;
$player->countries_id = $request->countries_id;
$player->save();
return redirect('index');
}
表格
<form action="{{ route('player.store') }}" method=“post”>
@csrf
<div class="form-group">
<label for="name">Name </label>
<input type="text" class="form-control" name="name" id="name" placeholder="First and Last" >
</div>
<div class="form-group">
<label for="age">Age </label>
<input type="text" class="form-control" name="age" id="age" placeholder="Age" >
</div>
<div class="form-group">
<label for="role">Role </label>
<input type="text" class="form-control" name="role" id="role" placeholder="Role" >
</div>
<div class="form-group">
<label for="batting">Batting </label>
<input type="text" class="form-control" name="batting" id="batting" placeholder="Batting">
</div>
<div class="form-group">
<label for="Bowling">Bowling</label>
<input type="text" class="form-control" name="bowling" id="bowling" placeholder="Bowling">
</div>
<div class="form-group">
<label for="odiRuns"> OdiRuns </label>
<input type="number" class="form-control" name="odiRuns" id="odiRuns" value="odiRuns" placeholder="OdiRuns" required>
</div>
<div class="form-group">
<label for="image">Add Image</label>
<input type="file" name="image" class="form-control-file" id="InputFile" value="image">
</div>
<div class="form-group">
<label for="Country">Country</label>
<input type="text" class="form-control" name="countries_id" id="countries" placeholder="country">
</div>
<button type=“submit” class=“btn btn-primary”>Create</button>
</form>
玩家数据库
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('age');
$table->string('role');
$table->string('batting');
$table->string('bowling');
$table->string('image');
$table->string('odiRuns');
$table->integer('countries_id')->unsigned();
$table->foreign('countries_id')->references('id')->on('countries');
$table->timestamps();
});
}
您的表单正在 postGET 请求而不是 POST 请求
有点难看 但是method=“post”
应该是method="post"
double quotes instead of that MS word weird character
指定你的表单可以post像这样的图片文件
<form action="{{ route('player.store') }}" method="post" enctype="multipart/form-data">
否则它不会post图像并且它在您的迁移中不可为空
变化:
$player = new Player();
以及为什么 'select' 不用于 countries_id 喜欢 :
<select name="countries_id">
<option value=""></option>
</select>
method="post"