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

“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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置