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 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示