第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

SQLSTATE[23000]:違反完整性約束:1048 Laravel 中的列“user_id”

SQLSTATE[23000]:違反完整性約束:1048 Laravel 中的列“user_id”

PHP
HUX布斯 2023-07-15 17:44:42
我使用 vue 在 laravel 中創(chuàng)建了一個(gè)待辦事項(xiàng)應(yīng)用程序。我有兩個(gè)表:users 和 todos;每個(gè)用戶都有很多這樣的待辦事項(xiàng):User.php 模型:class User extends Authenticatable {    protected $fillable = [        'name', 'email', 'password',    ];    public function todos()    {        return $this->hasMany(            Todos::class,            'user_id',            'user_id'        );    }}All.php模型:class Todos extends Model{    //    protected $fillable = ['title', 'completed', 'user_id'];    public function user()    {        return $this->belongsTo('App\User');    }然后我定義了這條路線:Route::post('/v1/todo', 'todosController@addTodo');這是我來自 todosController 的 addTodo: public function addTodo(Request $request)    {        $uid = Auth::user()->id;        //dd($uid);        $todoCreated = new Todos([            'title' => $request->input('title'),            'completed' => 0        ]);        $user = User::find($uid);        //dd($user);        //dd($todoCreated);        dd($user->todos()->create([            'title' => $request->input('title'),            'completed' => 0,        ]));}當(dāng)我發(fā)送表單數(shù)據(jù)時(shí),它向我顯示此錯(cuò)誤:Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into todos  (title , Completed , user_id , Updated_at , Created_at ) values (darya, 0, ?, 2020-07-06 12:17:45, 2020-07-06 12:17:45))。我是 Laravel 的新手,感謝您的幫助!
查看完整描述

1 回答

?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

您在關(guān)系中傳遞了錯(cuò)誤的參數(shù)。


public function todos()

{

    return $this->hasMany(

        Todos::class,'user_id','id'

    );

}



return $this->hasMany(Todos::class, 'foreign_key', 'local_key');


查看完整回答
反對 回復(fù) 2023-07-15
  • 1 回答
  • 0 關(guān)注
  • 138 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)