2017-02-10 70 views
0

在我的客戶模型,我有以下兩個功能,即處理是空字段,創建一個新的客戶時,設置爲空值:從Lavarel形式從模型

public function setCustPstAttribute($custpst) 
{ 
    $this->attributes['CustPST'] = trim($custpst) == '' ? 0 : trim($custpst); 
} 

    public function setCustSicAttribute($custsiccode){ 
    $this->attributes['CustSicCode'] = trim($custsiccode) == '' ? 0 : trim($custsiccode); 
} 

功能setCustPstAttribute($ custpst)工作正常,您可以看到上面的0值。 setCustSicAttribute($ custsiccode)函數根本不會被訪問/輸入,我嘗試從函數中退出,並且它永遠不會被訪問。唯一的區別是變量類型,$ custpst是varchar,$ custsiccode是int。 SQL插入正在尋找一個整數,但得到一個空字符串。

QueryException {#363 ▼ 
    #sql: "insert into `customers` (`CustCompanyName`, `CustSicCode`, `FOBLocation`, `CustPST`, `CustGenerator`, `CustStatus`, `CustLastUpdate`, `CustStartDate`) values (?, ?, ?, ?, ?, ?, ?, ?)" 
    #bindings: array:8 [▼ 
    0 => "Test" 
    1 => "" 
    2 => "Selkirk" 
    3 => 0 
    4 => "" 
    5 => "Active" 
    6 => "2017-02-10 14:12:48" 
    7 => "2017-02-10 14:12:48" 
    ] 
    #message: "SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '' for column 'CustSicCode' at row 1 (SQL: insert into `customers` (`CustCompanyName`, `CustSicCode`, `FOBLocation`, `CustPST`, `CustGenerator`, `CustStatus`, `CustLastUpdate`, `CustStartDate`) values (fgfgf, , Selkirk, 0, , Active, 2017-02-10 14:12:48, 2017-02-10 14:12:48))" 
    #code: "22007" 
    #file: "/var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php" 
    #line: 770 
    -previous: PDOException {#364 ▶} 
    +errorInfo: array:3 [▼ 
    0 => "22007" 
    1 => 1366 
    2 => "Incorrect integer value: '' for column 'CustSicCode' at row 1" 
    ] 
    +"previous": PDOException {#364 ▶} 
    -trace: {▶} 
} 

謝謝,儒略

回答

0

功能(設定和屬性之間)的名稱必須是完全一樣在數據庫的列名稱。設置CustSicCode屬性。更新後它工作。謝謝