-1
我在stackoverflow中的第一個問題,我有問題在我的數據庫中插入「image」字段,嘗試修改我的'max_input_vars' php.ini但它沒有奏效。我離開我的代碼,看看,如果你有另外一個觀點:SQLSTATE [23000]:完整性約束違規:1048列'圖像'不能爲空 - Laravel
我的模型:
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,也是本地。謝謝朋友!
確保您的圖像列接受空值。 – Gogol
@Gogol我已經在遷移中放入了「nullable」方法,但它返回相同的錯誤。 :( – Dadaismu5