user12004808
user12004808

Reputation:

Create not saving to database laravel

For a school assignment we are to have a site that lets you Create Update Edit and Delete a player and Add a Country. I am having trouble with my create as it is not saving to the database and returns no error. I have a feeling it is because of my foreign key and I've been looking all over stackoverflow and laravelforums as to how to do this or why it isn't saving to my database.
(As a note all my inputs are text for now until I get it working or get an error i can work with)
Player Model

protected $primaryKey = 'Id';
    protected $fillable =['name','age','role','batting','bowling','image','odiRuns','countries_id'];
    public function country()
    {
        return $this->belongsTo('App\Country','countries_id');
    }

Store Fuction

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

<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>

Player Database

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();
         });
    }

Upvotes: 0

Views: 505

Answers (2)

ipi bk
ipi bk

Reputation: 60

change:

$player = new Player();

and why you don't use 'select' for countries_id like :

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

Upvotes: 0

Salim Djerbouh
Salim Djerbouh

Reputation: 11034

Your form is posting a GET request instead of POST request

It's a bit difficult to notice but method=“post” should be method="post"

double quotes instead of that MS word weird character

Specify that your form can post files such as images like so

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

Otherwise it won't post the image and it's not nullable in your migration

Upvotes: 1

Related Questions