【问题标题】:Create not saving to database laravel创建不保存到数据库 laravel
【发布时间】:2019-10-07 20:16:14
【问题描述】:

对于学校作业,我们将拥有一个网站,让您可以创建更新、编辑和删除玩家以及添加国家/地区。 我的创建有问题,因为它没有保存到数据库并且没有返回错误。我有一种感觉,这是因为我的外键,我一直在查看 stackoverflow 和 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();
         });
    }

【问题讨论】:

  • 您在提交表单时是否收到任何错误信息?
  • 没有错误。只是重定向到我的索引页

标签: laravel save crud


【解决方案1】:

您的表单正在发布 GET 请求而不是 POST 请求

这有点难以注意到,但method=“post” 应该是method="post"

双引号,而不是那个 MS 单词奇怪的字符

指定你的表单可以像这样发布图片之类的文件

<form action="{{ route('player.store') }}" method="post" enctype="multipart/form-data">

否则它不会发布图像,并且它在您的迁移中不能为空

【讨论】:

  • 图片为什么不保存到数据库?我将在创建播放器时添加图像,并且 csrf 令牌显示:_token=aZNCEzTLbOKBiaSwsLNbpUz2nHrc4DmDO8Cehb1K&name=John+Smith&age=32&role=wicket+keeper&batting=right-hand+bat&bowling=left-arm+fast&odiRuns=23&image=profile.jpg&countries_id=New+Zealand
  • 如果显示它不是 POST 请求,我只是注意到错误的双引号字符,复制粘贴确切的行并将其替换为您的表单,“post” 无效,应该是"post"
  • 谢谢!我什至没有注意到那些报价。总是小事
【解决方案2】:

改变:

$player = new Player();

以及为什么您不使用“选择”作为国家 ID 喜欢:

<select name="countries_id">
<option value=""></option>
</select>
method="post"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-22
    • 2019-05-27
    • 2018-01-08
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2019-07-12
    • 2020-01-15
    相关资源
    最近更新 更多