常规错误:1364字段“ StudentID”没有默认值

时间:2019-10-19 20:14:16

标签: laravel

我试图做一个简单的公式,将数据插入到具有关系hasMany的2个不同的表中,但我一直收到此错误: 1364字段'PlayerID'没有默认值(SQL:插入phonesnameupdated_atcreated_at)值(qws,2019-10-19 22:45:40,2019-10-19 22:45:40))

这是玩家模型

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Player extends Model
{
    public function phones()
    {
        return $this->hasMany('App\Phone', 'foreign_key', 'local_key');


    }

}

手机型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class phone extends Model
{
    public function Player()
    {
        return $this->belongsTo('App\Player');


    }
}

此控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Player;
use App\phone;

class PlayersControlller extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('players.create');    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $people = new Player();
        $phone = new phone();

        $people->name = $request->input('name');
        $people->lastname = $request->input('lastname');
        $phone->name =$request->input('pname');

        $phone->save();
        $people->save();    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

这是表格

 <form action="{{url('player')}}" method="Post" >

            {{ csrf_field() }}
    <label> name</label>
    <input type="text" name="name">
    <label>  last name</label>
    <input type="text" name="lastname">
    <label> phone name</label>
    <input type="text" name="pname">
    <input type="submit" name="valider">
</form>

`

1 个答案:

答案 0 :(得分:0)

在您进行迁移时,请尝试以下操作:

$table->integer('StudentID ')->nullable();
$table->foreign('StudentID')->references('id')->on('student');

然后在您的控制台中

php artisan migrate:refresh