首页 > 解决方案 > 完整性约束违规:laravel 6.2 中的 1048

问题描述

我的网站是关于租用的,学生可以发布他们的产品。我的网站类似于shoppee 或lazada。有卖家(租客)、用户(租客)和管理员

起初,当 1 个卖家登录网站时,她可以创建帖子,但当她注销时,帖子仍然存在。所以,当其他卖家登录时,她可以看到其他帖子。

所以,我将 Seller_id 列添加到产品表中

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddSellerIdToProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            $table->integer('seller_id')->unsigned()->after('category_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('products', function (Blueprint $table) {
             $table->dropcolumn('seller_id');
        });
    }
}

这是在 Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function category()
    {
        return $this->belongsTo('App\Category');
    }

    public function seller()
    {
        return $this->belongsTo('App\Seller');
    }
}

这是在 Seller.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class Seller extends Authenticatable
{
    use Notifiable;

        protected $guard = 'seller';

        protected $fillable = [
            'name', 'email', 'password',
        ];

        protected $hidden = [
            'password', 'remember_token',
        ];

        public function product()
    {
        return $this->hasMany('App\Product');
    }

}

这是在 ProductController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\support\facades\Auth;
use App\Product;
use App\Category;
use Storage;
use Cart;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $products = Product::paginate(3);
        return view('product.index', compact('products'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Category::all();
        return view('product.create', compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request-> validate([
            'title' =>'required',
            'description' =>'required',
            'price' =>'required|numeric',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif',
        ]);


        $product = New Product;
        $product->seller_id=Auth::user()->id;
        $product-> title = $request-> title;
        $product-> description = $request-> description;
        $product-> price = $request-> price;
        $product-> category_id = $request-> category_id;


        if($request->hasFile('image')){
            $file = $request-> file('image');
            $filename = time().'.'.$file-> getClientOriginalExtension();
            $location = public_path('/images');
            $file-> move($location, $filename);
            $product-> image= $filename;
        }
        $product-> save();

        return back()->withInfo('Product successfully created');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $categories = Category::all();
        $product = Product::find($id);
        return view('product.edit', compact('product', 'categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request-> validate([
            'title' =>'required',
            'description' =>'required',
            'price' =>'required|numeric',

        ]);

        $product = Product::find($id);
        $product-> title = $request-> title;
        $product-> description = $request-> description;
        $product-> price = $request-> price;
        $product-> category_id = $request-> category_id;


        if($request->hasFile('image')){
            $file = $request-> file('image');
            $filename = time().'.'.$file-> getClientOriginalExtension();
            $location = public_path('/images');
            $file-> move($location, $filename);
            $oldImage = $product->image;
            \Storage::delete($oldImage);
            $product-> image= $filename;
        }
        $product-> save();

        return back()->withInfo('Product successfully updated');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $product= Product::find($id);
        Storage::delete($product->image);
        $product->delete();

        return back()->withInfo('Product has been deleted');

    }

    public function allproducts()
    {
        $cartItems = Cart::content();
        $categories = Category::all();
        $products = Product::paginate(10);

            return view ('allproducts', compact('products', 'categories', 'cartItems'));

    }
}

当卖家想要创建帖子时,会出现错误 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'seller_id' cannot be null (SQL: insert into products( seller_id, title, description, price, category_id, image, updated_at, created_at) 值 (?, tudung, rfdfrg , 1, 6, 1574240713.png, 2019-11-20 09:05:13, 2019-11-20 09:05:13))

所以有人可以帮助我或给我你的建议吗?

标签: phplaravel

解决方案


推荐阅读