0

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();
         });
    }
2
  • Do you receive any errors when you submit the form? Commented Oct 7, 2019 at 20:24
  • no errors. Just redirects to my index page Commented Oct 7, 2019 at 20:26

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

is the image why its not saving to the database? i will add a image when creating a player and the csrf token shows: _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
If it's showing that then it's not a POST request, I just noticed wrong double quotes characters, copy-paste the exact line and replace it in your form, “post” is not valid, it should be "post"
Thank you! i didnt even notice those quotes. always the small things
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.