使用laravel和Eloquent進行數據庫訪問。我有一張桌子,上面有一個無形的鑰匙。 Whene我插入一個實體,沒有foreighnkey值,直接在數據庫中工作正常,但如果我喜歡插入一個元素使用laravel我得到以下錯誤。使用可空foreighn鍵的完整性約束違規Laravel
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails (`gpb-lvw`.`regale`,
CONSTRAINT `regale_kunden_id_foreign` FOREIGN KEY (`kunden_id`)
REFERENCES `kunden` (`id`)) (SQL: update `regale` set `bezeichnung` =
xxxw, `updated_at` = 2015-07-20 10:28:17, `kunden_id` = where `id` = 4)
in Connection.php line 631
at Connection->runQueryCallback('update `regale` set `bezeichnung` = ?, `updated_at` = ?, `kunden_id` = ? where `id` = ?', array('xxxw', '2015-07-20 10:28:17', '', '4'), object(Closure)) in Connection.php line 591
at Connection->run('update `regale` set `bezeichnung` = ?, `updated_at` = ?, `kunden_id` = ? where `id` = ?', array('xxxw', '2015-07-20 10:28:17', '', '4'), object(Closure)) in Connection.php line 406
at Connection->affectingStatement('update `regale` set `bezeichnung` = ?, `updated_at` = ?, `kunden_id` = ? where `id` = ?', array('xxxw', '2015-07-20 10:28:17', '', '4')) in Connection.php line 349
at Connection->update('update `regale` set `bezeichnung` = ?, `updated_at` = ?, `kunden_id` = ? where `id` = ?', array('xxxw', '2015-07-20 10:28:17', '', '4')) in Builder.php line 1741
at Builder->update(array('bezeichnung' => 'xxxw', 'updated_at' => '2015-07-20 10:28:17', 'kunden_id' => '')) in Builder.php line 313
at Builder->update(array('bezeichnung' => 'xxxw', 'updated_at' => '2015-07-20 10:28:17', 'kunden_id' => '')) in Model.php line 1520
at Model->performUpdate(object(Builder), array()) in Model.php line 1454
at Model->save() in Model.php line 1402
at Model->update() in RegalController.php line 111
at RegalController->update('4')
at call_user_func_array(array(object(RegalController), 'update'), array('regale' => '4')) in Controller.php line 256
at Controller->callAction('update', array('regale' => '4')) in ControllerDispatcher.php line 164
at ControllerDispatcher->call(object(RegalController), object(Route), 'update') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(RegalController), object(Route), object(Request), 'update') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\RegalController', 'update') in Route.php line 201
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 704
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 229
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 54
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 118
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 86
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\xampp\htdocs\gpb-lvw\public\index.php') in server.php line 21
在控制器中的代碼如下所示:
$rules = array(
'bezeichnung' => array('required','unique:regale')
);
$validator = \Validator::make(\Input::all(),$rules);
if($validator->fails()){
//$messages = $validator->messages();
//return $messages;
return \Redirect::route('regale.create')->withErrors($validator)->withInput();
}
$bezeichnung = \Input::get('bezeichnung');
$list=new Regal();
$list->nummer= \Input::get('nummer');
$list->maxPaletten= \Input::get('maxPaletten');
$list->anzahlEbenen= \Input::get('anzahlEbenen');
$list->kunden_id=\Input::get('kunden_id');
$list->bezeichnung = $bezeichnung;
$list->save();
\Session::flash('message',"Regal wurde angelegt.");
return \Redirect::route('regale.index');
,這是我的模型文件:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Regal extends Model {
protected $table = "regale";
public function listEinlagerung(){
return $this->hasMany('App\Models\Einlagerung');
}
public function kunde(){
return $this->belongsTo('App\Models\Kunde');
}
}
嗨,這不是問題它只是在德國的單數和複數。 –
好的。另外,我發現的一個問題是,在每個「輸入,驗證程序,會話和重定向」命令之前都有一個反斜槓,請刪除反斜槓並查看會發生什麼。 – dnavas77