简体   繁体   中英

Laravel 5.8 - Cannot add or update a child row using Laravel

I have a simple table with multiple data which looks like this:

Pages table

在此处输入图像描述

I have created a relationship to a table named prebids like this:

public function up()
{
    Schema::create('prebids', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('bidder_name');
        $table->string('params_name');
        $table->string('params_value');
        $table->bigInteger('page_id')->unsigned();
        $table->foreign('page_id')->references('id')->on('pages')->onDelete('cascade');
        $table->timestamps();
    });
}

Here is the page model:

class Page extends Model
{
    protected $fillable = [
        "title",
        "articles",
        "status"
    ];

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

Here is the prebids model:

class Prebid extends Model
{
    protected $fillable = [
        "params_name",
        "params_value",
        "bidder_name"
    ];
    public function page()
    {
        return $this->belongsTo('App\Page');
    }
}

Now when the user clicks add prebids, I want to store the prebids data as per the model.

So on pages index.blade.php on add button I have this

<td>
    <a href="{{ route('prebids.create'), $page->id}}"
       class="btn btn-sm btn-primary">Add prebid</a>

And on prebid controller I have the following:

class PrebidController extends Controller
{

    public function create()
    {
        $prebid = Prebid::all();
        $page = Page::all();

        return view('prebids.create', ['page' => $page]);
    }

    public function store(Request $request)
    {
        foreach ($request->input() as $parameters) {
            $parameters = new prebid();
            $parameters->save();
        }
        return redirect("/pages")->with("sucess", "data successfully saved");
    }

}

Now when the user clicks add prebids it opens a form in which user can add information and submit, when I submit I get the followingg error

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ( royalad . prebids , CONSTRAINT prebids_page_id_foreign FOREIGN KEY ( page_id ) REFERENCES pages ( id ) ON DELETE CASCADE) (SQL: insert into prebids ( updated_at , created_at ) values (2019-11-06 15:24:34, 2019-11-06 15:24:34))

What is wrong with my code?

You need to associate a page to your model before saving it:

$parameters = new prebid();
$parameters->page()->associate( $theActualPageObject )
$parameters->save();

Otherwise, the column page_id in your prebid table would be empty, which is not allowed since you set up a relationship to pages.id there - hence the foreign key constraint error.

If you don't always have a page, you can also make the page_id nullable and default it to null

$table->bigInteger('page_id')->unsigned()->nullable()->default(null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM