2017-08-07 13 views
-1

私の最初の質問は、stackoverflowで、私のデータベースに "image"フィールドを挿入する際に問題が発生しました。 'max_input_vars' php.iniは動作しませんでした。SQLSTATE [23000]:整合性制約違反:1048列 'image'はnullにはできません - Larvel

マイモデル:

class Photo extends Model 
{ 
/** 
* The database table used by the model. 
* 
* @var string 
*/ 
protected $table = 'photos_album'; 

public static function galleries($id) 
{ 
    $galleries = \DB::table('photos_album') 
        ->select('photos_album.photo', 'photos_album.id', 
          \DB::raw("DATE_FORMAT(album.created_at, '%m_%Y') AS 
creacion") 
       ) 
        ->join('album', 'photos_album.album_id', '=', 'album.id') 
        ->where('album.id', $id) 
        ->get(); 

    return $galleries; 
} 


public static function galleryDetail($id) 
{ 
    $gallery = \DB::table('photos_album') 
       ->select('photos_album.photo', 'photos_album.id', 
         \DB::raw("DATE_FORMAT(album.created_at, '%m_%Y') AS 
creacion") 
       ) 
       ->join('album', 'photos_album.album_id', '=', 'album.id') 
       ->where('photos_album.id', $id) 
       ->first(); 

    return $gallery; 
    } 
} 

MyController:

public function store(Request $request) 
    { 
    $messages = [ 
     'title.required'    => 'El campo título es obligatorio.', 
     'document_name.required'  => 'La imagen es obligatoria.' 
    ]; 

    $validator = Validator::make($request->all(), [ 
     'title'    => 'required', 
     'document_name'  => 'required' 
    ], $messages); 

    if ($validator->fails()) { 
     return redirect('gallery/create') 
        ->withErrors($validator) 
        ->withInput(); 
    } 

    $gallery = $request->all(); 

    try { 
     \DB::transaction(function() use ($gallery) { 

      $row = new Album; 
      $row->title   = $gallery['title']; 
      $row->image   = $gallery['document_name']; 
      $row->status   = $gallery['status']; 
      $row->save(); 

      $gallery_id = $row->id; 

      \Session::flash('gallery_id', $gallery_id); 

     }); 

    } catch (\ErrorException $e) { 
     \Session::flash('add_errors','error'); 
     return redirect('/gallery/create')->with('add_errors', true); 
    } 

    return redirect('/gallery/' . \Session::get('gallery_id') . '/photos')- 
>with('status', 'Galería creada con éxito!'); 
} 

Javascriptを:

function upload_image_album(idImage, date) 
    { 
    var formData = new FormData($(".form-horizontal")[0]); 
    var inputFile = document.getElementById(idImage); 
    var file = inputFile.files[0]; 
    formData.append('documentation', file); 
    formData.append('type', idImage); 
    formData.append('_token', token_); 
    formData.append('date', date); 

if (file != undefined) { 
    //hacemos la petición ajax 
    $.ajax({ 
     url: '/image/upload/album', 
     type: 'POST', 
     // Form data 
     //datos del formulario 
     data: formData, 
     //necesario para subir archivos via ajax 
     cache: false, 
     contentType: false, 
     processData: false, 
     dataType: "json", 
     //mientras enviamos el archivo 
     beforeSend: function(){ 
      $("#upload_" + idImage).show(); 
      $("#upload_" + idImage).html('Validando el archivo... <i class="fa 
    fa-refresh fa-spin fa-2x"></i>'); 
     }, 
     //una vez finalizado correctamente 
     success: function(data){ 
      if (data.exito) { 
       $("#upload_" + idImage).html('El archivo se subio 
correctamente'); 
       $("#" + idImage + "_name").val(data.file); 
       $("#" + idImage + "_campo").hide(); 
       $("#" + idImage + "_descripcion").hide(); 
      } else { 
       $("#upload_" + idImage).html('El archivo no es valido'); 
      } 
      $("#" + idImage).val(''); 
     }, 
     //si ha ocurrido un error 
     error: function(){ 
      console.log("error"); 
      $("#upload_" + idImage).html('El archivo no es valido'); 
     } 
    }); 
    } 
    } 

Imはローカル的環境に取り組んで私はあなたが別の意見を持っているかどうかを確認するために私のコードを残します仮想ホストでは、私もMySQL 5.7.18をローカルで使用しています。友よありがとう!

+0

imageカラムがnull値を受け入れることを確認してください。 – Gogol

+0

@ゴーゴン私はすでに "nullable"メソッドをマイグレーションに入れましたが、同じエラーを返します。 :( – Dadaismu5

答えて

0

まず、画像がコントローラに送信されているかどうかを確認します。試してみてください($ request-> all);

関連する問題