title: Laravel 6: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
body: <p>everyone!</p>
  
  <p>I need some help. I'm creating a web app in laravel 6 that lets the user insert data through php forms that is also inserted into a database.</p>
  
  <p>Basically, I create a Supplier (it's working), I create a Material (it's also working) and then I create a Purchase (referencing to a Supplier and a Material). I also have a Unit table to indicate whether the material is kg, per_unit, etc.</p>
  
  <p>Here are my schemas:</p>
  
  <p>Supplier schema:</p>
  
  <pre><code>Schema::create('suppliers', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('reference');
          $table-&gt;string('name',255);
          $table-&gt;string('address',255);
          $table-&gt;text('zip_code')-&gt;nullable();
          $table-&gt;string('locality');
          $table-&gt;text('phone')-&gt;nullable();
          $table-&gt;text('description')-&gt;nullable();
          $table-&gt;timestamps();
      });
  </code></pre>
  
  <p>Material schema:</p>
  
  <pre><code>Schema::create('materials', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('reference');
          $table-&gt;string('name',255);
          $table-&gt;text('description')-&gt;nullable();
          $table-&gt;integer('quantity');
          $table-&gt;integer('quantity_in_use')-&gt;default(0);
          $table-&gt;string('image_material')-&gt;nullable();
          $table-&gt;string('image_receipt')-&gt;nullable();
          $table-&gt;timestamps();
      });
  </code></pre>
  
  <p>Unit schema:</p>
  
  <pre><code> Schema::create('units', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
      });
  </code></pre>
  
  <p>Purchase schema:</p>
  
  <pre><code>Schema::create('purchases', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('reference');
          $table-&gt;datetime('date');
          $table-&gt;unsignedInteger('supplier_id');
          $table-&gt;unsignedInteger('material_id');
          $table-&gt;integer('quantity');
          $table-&gt;text('description')-&gt;nullable();
          $table-&gt;double('price_per_unit');
          $table-&gt;unsignedInteger('unit_id');
          $table-&gt;timestamps();
      });
  </code></pre>
  
  <p>And a relations migration that I created:</p>
  
  <pre><code>Schema::table('purchases', function (Blueprint $table) {
          $table-&gt;foreign('supplier_id')-&gt;references('id')-&gt;on('suppliers');
          $table-&gt;foreign('material_id')-&gt;references('id')-&gt;on('materials');
          $table-&gt;foreign('unit_id')-&gt;references('id')-&gt;on('units');
      });
  </code></pre>
  
  <p>The problem is, whenever I try to create a Purchase, it gives this error:</p>
  
  <p><strong>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>guacamaiateste</code>.<code>purchases</code>, CONSTRAINT <code>purchases_unit_id_foreign</code> FOREIGN KEY (<code>unit_id</code>) REFERENCES <code>units</code> (<code>id</code>)) (SQL: insert into <code>purchases</code> (<code>reference</code>, <code>date</code>, <code>supplier_id</code>, <code>material_id</code>, <code>description</code>, <code>quantity</code>, <code>price_per_unit</code>, <code>updated_at</code>, <code>created_at</code>) values (C01, 17-04-2020 00:00:00, 8, 5, Just a purchase, 1, 1, 2020-04-17 04:08:00, 2020-04-17 04:08:00))</strong></p>
  
  <p>Here is my model:</p>
  
  <pre><code>class Purchase extends Model{
  protected $fillable = ['reference', 'date', 'supplier_id', 'material_id', 'quantity', 'description', 'price_per_unit'];
  
  public function supplier()
  {
      return $this-&gt;belongsTo('App\Supplier', 'supplier_id');
  }
  
  public function material()
  {
      return $this-&gt;belongsTo('App\Material', 'material_id');
  }
  
  public function unit()
  {
      return $this-&gt;belongsTo('App\Unit', 'unit_id');
  }}
  </code></pre>
  
  <p>And my Purchase Controller:</p>
  
  <pre><code>use Illuminate\Http\Request;
  use App\Unit;
  use App\Purchase;
  use App\Supplier;
  use App\Material;
  use App\Http\Requests\StorePurchase;
  use App\Http\Controllers\Controller;
  
  class PurchaseController extends Controller
  {
  /**
   * Display a listing of the resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function index()
  {
      $purchases = Purchase::with(['unit', 'supplier', 'material'])-&gt;get();
  
      return view('admin.purchases.index', [
          'purchases' =&gt; $purchases,
      ]);
  }
  
  /**
   * Show the form for creating a new resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function create()
  {
      $units = Unit::all();
      $suppliers = Supplier::all();
      $materials = Material::all();
  
      return view('admin.purchases.create', [
          'units' =&gt; $units,
          'suppliers' =&gt; $suppliers,
          'materials' =&gt; $materials,
      ]);
  }
  
  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(StorePurchase $request)
  {
      $validated = $request-&gt;validated();
  
      $validated['price'] = $validated['price_per_unit'] * $validated['quantity'];
  
      if (empty($validated['supplier_id'])) {
          $supplier = Supplier::create([
              'reference' =&gt; $validated['supplier_reference'],
              'name' =&gt; $validated['supplier_name'],
              'address' =&gt; $validated['supplier_address'],
              'locality' =&gt; $validated['supplier_locality'],
          ]);
          $validated['supplier_id'] = $supplier-&gt;id;
      }
  
      if (empty($validated['material_id'])) {
          $material = Material::create([
              'reference' =&gt; $validated['material_reference'],
              'name' =&gt; $validated['material_name'],
              'quantity' =&gt; $validated['quantity'],
          ]);
          $validated['material_id'] = $material-&gt;id;
      }
  
      Purchase::create($validated);
  
      return redirect()-&gt;route('purchases.index')
          -&gt;with('status', 'Compra registada com sucesso!')
          -&gt;with('status-type', 'success');
  }
  
  /**
   * 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)
  {
      $units = Unit::all();
      $purchase = Purchase::find($id);
      $suppliers = Supplier::all();
      $materials = Material::all();
  
      return view('admin.purchases.edit', [
          'units' =&gt; $units,
          'purchase' =&gt; $purchase,
          'suppliers' =&gt; $suppliers,
          'materials' =&gt; $materials,
      ]);
  }
  
  /**
   * Update the specified resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  int  $id
   * @return \Illuminate\Http\Response
   */
  public function update(StorePurchase $request, $id)
  {
      $validated = $request-&gt;validated();
  
      $purchase = Purchase::find($id);
  
      $purchase-&gt;update($validated);
  
      return redirect()-&gt;route('purchases.index')
          -&gt;with('status', 'Compra atualizada com sucesso!')
          -&gt;with('status-type', 'success');
  }
  
  /**
   * Remove the specified resource from storage.
   *
   * @param  int  $id
   * @return \Illuminate\Http\Response
   */
  public function destroy($id)
  {
      Purchase::destroy($id);
  
      return redirect()-&gt;route('purchases.index')
          -&gt;with('status', 'Compra apagada com sucesso!')
          -&gt;with('status-type', 'success');
  }
  }
  </code></pre>
  
  <p>I know this is quite extense and I apologize but I really need help in this... It's basically the whole point of this app.</p>