Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel-带外键的查询_Php_Mysql_Laravel - Fatal编程技术网

Php laravel-带外键的查询

Php laravel-带外键的查询,php,mysql,laravel,Php,Mysql,Laravel,欢迎光临!我在数据库notes和pilots中有两个表,我想显示属于pilots的notes。我在notes表中添加了外键,但在尝试创建新notes时遇到了这样的问题: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`app`.`notes`, CONSTRAINT `notes_pilot_id_for

欢迎光临!我在数据库notes和pilots中有两个表,我想显示属于pilots的notes。我在notes表中添加了外键,但在尝试创建新notes时遇到了这样的问题:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`app`.`notes`, CONSTRAINT `notes_pilot_id_foreign` FOREIGN KEY (`pilot_id`) REFERENCES `pilots` (`id`))
注:型号:

 class Note extends Model
    {
      protected $table = 'notes';
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'data', 'zadanie', 'uwagi', 'pilot_id',
        ];

        public function pilot() {
          return $this->belongsTo(Pilot::class);
        }
    }
试点模式:

class Pilot extends Model
{
  protected $table = 'pilots';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'phone', 'email',
    ];

    public function note() {
      return $this->hasMany(Note::class);
    }
}
注意控制器存储方法:

public function store(Request $request)

{

    $this->validate($request, [

        'data' => 'required',

        'zadanie' => 'required',

        'uwagi' => 'required',

    ]);


    $note = new Note (array(
      'data' => $request->get('data'),
      'zadanie' => $request->get('zadanie'),
      'uwagi' => $request->get('uwagi'),

    ));
    $note->save();
    $note->pilot()->sync($request->get('pilots'));

    return redirect()->route('uwagi.index')

                    ->with('success','Uwagi dodane poprawnie');

}
试试这个 返回$this->hasMany('App\Note'); 及 试试这个 返回$this->belongsTo('App\Pilot')

试试这个 返回$this->hasMany('App\Note'); 及 试试这个
返回$this->belongsTo('App\Pilot')

问题解决了。我只是在迁移过程中没有在公共函数中删除外键。

问题解决了。我只是没有在迁移中删除公共函数中的外键。

您应该检查迁移或数据库表中的外键。您应该检查迁移或数据库表中的外键。