douzhaochan6468 2018-07-24 18:57
浏览 140
已采纳

“SQLSTATE [23000]:完整性约束违规:1048列'描述'不能为空

I'm using Laravel and trying to build a gallery, i'm testing the upload of a file to a db but i when i click submit i get the error " Illuminate \ Database \ QueryException (23000) SQLSTATE[23000]: Integrity constraint violation: 1048 Column

I've set up a GalleryController and the code is as follows

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class GalleryController extends Controller
{
    // List Galleries
    public function index (){
            //Render View
        return view ('gallery/index');
    }

    // Show Create From
    public function create(){
            //Render View
        return view ('gallery/create');
    }

    // Store Gallery
    public function store(Request $request){
            // Get Request Input
        $name = $request->input ('name');
        $description = $request->input ('description');
        $cover_image = $request->input ('cover_image');
        $owner_id = 1;

        // Check Image Upload
        if($cover_image){
            $cover_image_filename = $cover_image->getClientOriginalName();
            $cover_image->move(public_path('images'), $cover_image_filename);
        } else {
            $cover_image_filename = 'noimage.jpg';
        }

        //Insert Gallery
        DB::table('galleries')->insert(
        [
            'name'          => $name,
            'description'   => $description,
            'cover_image'   => $cover_image,
            'owner_id'      => $owner_id,

        ]
        );

        //Redirect
        return \Redirect::route('gallery.index')-> with('message', 'Gallery Created');
    }


    //Show Gallery Photos 
    public function show($id){
            die ($id);

`

The main.blade.php calls the code using

@ if(Session::has('message'))
<div class="alert alert-info">
{{Session::get('message')}}
</div>
@ endif;    

My .env DB is set to root and password is blank too.

If any more info is needed please advise.

Thanks

  • 写回答

1条回答 默认 最新

  • dousa1630 2018-07-24 19:10
    关注

    I think you have two error: 1) description be null because fo your input was come null or in your view file it has another name you may do this set default value?

    $description = ($request->input ('description')) ? $request->input('description'): "description";
    

    2) your second error is you save temporary image file name
    use this instead

    //Insert Gallery

    DB::table('galleries')->insert(
        [
            'name'          => $name,
            'description'   => $description,
            'cover_image'   => $cover_image_filename,
            'owner_id'      => $owner_id,
    
        ]
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 jmeter出现403
  • ¥500 求华为P30PRO手机硬盘数据恢复
  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号