Laravel外键保存问题

时间:2017-05-17 05:36:23

标签: php mysql laravel

我有一个名为companies的表和另一个名为ads的表,我尝试在名为company_id的广告列中获取公司ID。

这是我的广告迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAdTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('ads', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('company_id')->unsigned();
            $table->string('title')->unique();
            $table->string('slug')->unique();
            $table->string('image')->nullable();
            $table->string('description');
            $table->timestamps();
        });

        Schema::table('ads', function($table) {
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('ads');
    }
}

这会创建我的广告表没有问题,但是当我尝试保存广告时,它会返回我的错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`jobid`.`ads`, CONSTRAINT `ads_company_id_foreign` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`)) (SQL: insert into `ads` (`title`, `slug`, `description`, `image`, `updated_at`, `created_at`) values (first ad test, first-ad-test, <p>rwv R4QF Q4R</p>, 1494998776.png, 2017-05-17 12:26:17, 2017-05-17 12:26:17))

我该如何解决?

更新

public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('company_name');
            $table->string('manager_name');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('image')->nullable();
            $table->string('password');
            $table->text('about')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }

存储功能

  public function store(Request $request)
  {
    $this->validate($request, array(
      'title' => 'required|max:255',
      'slug' => 'required|alpha_dash|min:5|max:255|unique:ads,slug',
      'image' => 'sometimes|image',
      'description' => 'required'
    ));

    $ad = new Ad;

    $ad->title = $request->input('title');
    $ad->slug = $request->input('slug');
    $ad->description = $request->input('description');


    if ($request->hasFile('image')) {
      $avatar = $request->file('image');
      $filename = time() . '.' . $avatar->getClientOriginalExtension();
      $location = public_path('ads/');
      $request->file('image')->move($location, $filename);


      $ad->image = $filename;
    }

    $ad->save();


      Session::flash('success', 'Your ad published successfully!');

      return redirect()->route('company.adslist', $ad->id);
  }

1 个答案:

答案 0 :(得分:1)

in your company form
<input type="hidden" name="company_id" value ="{{ company_id }}">

然后在商店方法

  $ad->company_id=Input::get('company_id');

你可以根据用户输入使id动态,我只是硬编码