You can add an hidden input to the children form.
<input type="hidden" name="parent_id" value="{{ $parentId }}">
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
First of all I am very new to this and have searched for days trying not to have to ask a question myself to what seemed to be something simple, so please bear with me...
I have a page where I can add or delete "Parents", which is working correctly.
I can also click a "Parent" and it opens its corresponding /parents/{id} page which is suppose to show a list of "Childs". There is a "Create child" button which leads to the /children/create page with a form.
When I submit anything it results in: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: children.parent_id (SQL: insert into "children" ("text", "updated_at", "created_at") values (Testing, 2017-07-01 13:16:58, 2017-07-01 13:16:58))
I know that for when authenticated users are the parent you usually use for the child store function: \Auth::user()->children()->create($request->all());
But when the parent is not an user I am unsure what to do.
Here are my migrations: Parents:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateParentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('parents', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('parents');
}
}
Children:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateChildrenTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('children', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->integer('parent_id')->unsigned()->index();
$table->timestamps();
$table->foreign('parent_id')
->references('id')
->on('parents')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('children');
}
}
Parent Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Parent extends Model
{
protected $table = 'parents';
protected $fillable = ['name'];
public function children() {
return $this->hasMany('App\Child');
}
}
Child Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Child extends Model
{
protected $table = 'children';
protected $fillable = ['text', 'parent_id'];
public function parent() {
return $this->belongsTo('App\Parent');
}
}
Routes:
Route::get('/', 'HomeController@index')->name('home');
Route::resource('parents', 'ParentController');
Route::get('/parents/{parent}/remove', 'ParentController@remove');
Route::resource('children', 'ChildController');
Route::get('/children/{child}/remove', 'ChildController@remove');
Auth::routes();
ChildController (create + store):
public function create()
{
return view('child.create');
}
public function store(ChildStoreRequest $request)
{
Child::create($request->all());
$request->session()->flash('flash_message','Child added');
return Redirect::back();
}
I know "Child::create($request->all()); " won't work as it doesn't know which parent it is being added to.
If I try something for store like: $child->parent_id = $request->parent_id;
It says it can't add empty value, probably because $request is not carrying the parent_id.
Here is part of the show.blade.php for the Parent, which probably is missing a way to pass the current parent id:
<p><a href="/children/create" class="btn">Create new child</a></p>
Please tell if I should add other controller functions or views to help me.
Thank you very much for your time reading.
Your route to show the form for creating a child should be for example:
Route::get('/parents/{parent_id}/child/create', 'AnyController@create');
Then you can grab the parent id from the route !
Please or to participate in this conversation.