2017-03-07 118 views
2

我試圖解決這個問題,我試圖用laravel 5.2中的更新方法編輯多個文件。SQLSTATE [23000]:完整性約束衝突:1048列'property_id'在Laravel 5.2中不能爲null

當我跑我的過程,並保存它返回以下錯誤:

QueryException in Connection.php line 729: 
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'property_id' cannot be null (SQL: insert into `files` (`name`, `property_id`, `updated_at`, `created_at`) values (58bf2825d39d9.jpg, , 2017-03-07 17:37:41, 2017-03-07 17:37:41)) 

這是我Promperties遷移表和你的關係

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreatePropertiesTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('properties', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('category_id')->unsigned(); 
      $table->foreign('category_id') 
        ->references('id') 
        ->on('categories') 
        ->onDelete('cascade'); 
      $table->integer('term_id')->unsigned(); 
      $table->foreign('term_id') 
        ->references('id') 
        ->on('terms') 
        ->onDelete('cascade'); 
      $table->string('address'); 
      $table->integer('province_id')->unsigned(); 
      $table->foreign('province_id') 
        ->references('id') 
        ->on('provinces') 
        ->onDelete('cascade'); 
      $table->string('ctime');//Tiempo de construcción de la propiedad (años). 
      $table->string('mconstruction');//Metros de construcción (Mt2). 
      $table->string('ground');//Metros de terreno (Mt2). 
      $table->string('level');//Nivel/Piso. 
      $table->string('elevator');//Asscensores. 
      $table->string('price'); 
      $table->integer('currency_id')->unsigned();    
      $table->foreign('currency_id') 
        ->references('id') 
        ->on('currencies') 
        ->onDelete('cascade'); 
      $table->integer('client_id')->unsigned();    
      $table->foreign('client_id') 
        ->references('id') 
        ->on('clients') 
        ->onDelete('cascade'); 
      $table->softDeletes(); 
      $table->timestamps(); 
     }); 

     DB::update("ALTER TABLE properties AUTO_INCREMENT = 1000;"); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('properties'); 
    } 
} 

這是我的文件遷移表和你的關係

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateFilesTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('files', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->integer('property_id')->unsigned(); 
      $table->foreign('property_id') 
        ->references('id') 
        ->on('properties') 
        ->onDelete('cascade'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('files'); 
    } 
} 

這是我的房產模型

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

use Illuminate\Database\Eloquent\SoftDeletes; 

class Property extends Model 
{ 
    use SoftDeletes; 

    protected $dates = ['deleted_at']; 

    protected $table = 'properties'; 

    protected $fillable = ['category_id', 'term_id', 'address', 'ctime', 'mconstruction', 'ground', 'level', 'elevator', 'price', 'currency_id', 'province_id', 'client_id']; 

    // Relation with Category 
    public function category() 
    { 
     return $this->belongsTo('App\Category'); 
    } 

    // Relation with Term 
    public function term() 
    { 
     return $this->belongsTo('App\Term'); 
    } 

    // Relation with Province 
    public function province() 
    { 
     return $this->belongsTo('App\Province'); 
    } 

    // Relation with Client 
    public function client() 
    { 
     return $this->belongsTo('App\Client', 'client_id'); 
    } 

    // Relation with Currency 
    public function currency() 
    { 
     return $this->belongsTo('App\Currency'); 
    } 

    // Relation with Municipality 
    public function municipality() 
    { 
     return $this->belongsTo('App\Municipality'); 
    } 

    // Relation with File 
    public function files() 
    { 
     return $this->hasMany('App\File'); 
    } 

    public function scopeSearch($query, $search) { 
     return $query 
      ->where('category_id', 'like', "%" . $search . "%")    
      ->orWhere('address', 'like', "%" . $search . "%") 
      ->orWhere('price', 'like', "%" . $search . "%"); 
    } 
} 

這是我的文件型號

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

use Illuminate\Database\Eloquent\SoftDeletes; 

class File extends Model 
{ 
    protected $table = 'files'; 

    protected $fillable = ['name', 'property_id']; 

    // Relation with Property 
    public function property() 
    { 
     return $this->belongsTo('App\Property'); 
    } 
} 

在這裏,我告訴我的控制器的一部分問題出在哪裏

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use Intervention\Image\Exception\NotReadableException; 

use Illuminate\Support\Facades\Input; 

use App\Http\Requests; 
use App\Client; 
use App\Category; 
use App\Term; 
use App\Province; 
use App\Municipality; 
use App\Property; 
use App\Currency; 
use App\User; 
use App\File; 
use Image; 
use View; 

class PropertyController extends Controller 
{  
    public function update(Request $request, $id) 
    { 
     //dd($request->input()); 

     $updated = Property::findorFail($id); 

     $properties = $request->all(); 

     $property_id = $request->get('property_id'); 

     $updated->fill($properties)->save(); 

     // -- Images update method -- // 

     $images = $request->file('avatar'); 

     foreach ($images as $image) 
     { 
      $rules = array(
       'avatar' => 'required|mimes:png,gif,jpeg,jpg|max:20000' 
      ); 

      $validator = \Validator::make(array('avatar'=> $image), $rules); 

      if (! $validator->passes()) 
      { 
       return redirect()->back()->withErrors($validator); 
      } 

      $extension = $image->getClientOriginalExtension(); 
      $filename = uniqid() . '.' . $extension; 
      $path = public_path() . 'uploads/products/'; 

      //dd($extension); 

      Image::make($image)->resize(300, 200)->save(public_path('uploads/products/' . $filename)); 

      //Move file into uploads folder 
      $image->move($path, $filename); 
      //Insert file name in db 

      //dd($image); 

      $image = File::create([ 
       'name'  => $filename, 
       'property_id' => $property_id 
      ]); 

     } 

     // -- End method -- // 

     return redirect()->route('properties.index') 
         ->with('success','Propiedad actualizada satisfactoriamente!!!'); 
    } 

這是$圖像=文件之前測試::創建方法,返回以下值:

enter image description here

我試圖做的是替換文件並刪除它們,以便它們不會累積在存儲這些圖像的目錄中。

+0

嘗試更換 $請求 - >獲取( 'PROPERTY_ID'); 與---------- $ request - >('property_id'); – ATechGuy

+0

嗨@keaner謝謝,但是當我使用with,save和update方法返回時:BadMethodCallException在Macroable.php中第74行:方法不存在。 –

回答

2

您需要驗證您的property_id是否已在您的表單請求中發送。將它添加爲隱藏字段,以防您錯過添加它。

<Input type="hidden" name="property_id" value="{{$properties->id}}" > 
+0

非常感謝卡洛斯,誠實的說,我沒有想過在這部分表單中傳遞隱藏字段,但這樣它就會將更改保存在數據庫和我定義的目錄中,以適應此類別的圖像。 –

1

您看到的錯誤是因爲files表中的列property_id不可空。

如果您希望此列接受空值,你需要讓這個在您的遷移:

$table->integer('property_id')->unsigned()->nullable(); 

當然,你可能不希望該值是零,所以...

$property_id = $request->get('property_id'); 

您確定$property_id變量正在填充在上面的行中嗎?如果不是:

$property_id = $request->input('property_id'); 
+0

非常感謝Joe,但列不能爲空,它是文件和屬性的關係。 我嘗試更新文件中的註冊表並更新圖像文件。 應用上次建議會產生相同的錯誤。 –

+0

所以你檢查是否正在填充'$ property_id'變量? – Joe

+0

不,更新時不要填充列,我做了一個dd($ image);在$ image = File :: create之前,返回originalName,mimeType和size,你可以看到上面的更新。 –

相關問題