samedi 23 avril 2016

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

I have foods model and food_addon model.
And food-addon belongs to food where food and addon are created from master table food.
I had made dropdown lists as food and addon thus to link food and addon.
My migration table is:


  Schema::create('foods', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('restaurant_id')->unsigned();
            $table->foreign('restaurant_id')
                ->references('id')
                ->on('restaurants')
                ->onDelete('cascade');

            $table->string('name');
            $table->string('ar_name');
            $table->string('desc')->nullable();
            $table->string('ar_desc')->nullable();
            $table->string('ingredients')->nullable();
            $table->string('ar_ingredients')->nullable();

            $table->decimal('price', 6, 2);

            $table->enum('food_or_addon', ['F', 'A']);
            $table->enum('veg_or_non_veg', ['V', 'N']);

            $table->boolean('status')->default(1);

            $table->timestamps(); 

And migration table for food_addon is:

  Schema::create('food_addons', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('food_id')->unsigned();
            $table->foreign('food_id')
                ->references('id')
                ->on('foods')
                ->onDelete('cascade');

            $table->integer('addon_id')->unsigned();
            $table->foreign('addon_id')
                ->references('id')
                ->on('foods')
                ->onDelete('cascade');

            $table->timestamps();
        });
and two models are:


class Food extends Model
{

    protected $fillable = [
        'restaurant_id',
        'name',
        'ar_name',
        'desc',
        'ar_desc',
        'ingredients',
        'ar_ingredients',
        'price',
        'food_or_addon',
        'veg_or_non_veg',
        'status'
    ];
  public function foodAddons()
    {
        return $this->hasMany('App\FoodAddon');
    }
and food_addon model is:

class FoodAddon extends Model
{
    protected $fillable = [
        'food_id',
        'addon_id',
    ];

    public function foods()
    {
//        return $this->belongsToMany('App\Food');
        return $this->belongsTo('App\Food');

    }

What i want is to fill table food_addon by getting food lists and addon lists from master table food and then link food and addon aby making reltaion as above. But when i set to do i got above errors.Since i already had created food table why i get such constraints violation error.I searched but couldnot understand.# Here is my controller:

class Foods_AddonsController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ private $food_addon; public function __construct(FoodAddon $food_addon,Food $food){

    $this->food_addon=$food_addon;
    $this->food=$food;

}
public function index()
{
    $foods_addons = $this->food_addon->all()
    ;
    $foods=$this->food->orderBy('name','id')->where('food_or_addon','=','F')->get();
    $addons=$this->food->orderBy('name','id')->where('food_or_addon','=','A')->get();

    return view('lugmety.admin.Food_Addons.index',compact('foods','foods_addons','addons'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $foods=$request->all();
    FoodAddon::create($foods);
    session()->flash('success', 'Record added successfully.');
    return redirect('admin/foods_addons');
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire