I have the book, 'Learning Laravel's Eloquent', & believe I've followed its instruction to the letter, but I have ground to a halt at this error message. I'm very new to all this, I feel I've read everything related to this error and yet here I am, appealing to you fine folks. I can't be the only one who's faltered at this juncture while trying to follow this book, so I put this out there on behalf of us all!
Here is the full error message:
QueryException in Connection.php line 655:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`eloquent_journey`.`books`, CONSTRAINT `books_author_id_foreign` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`)) (SQL: insert into `books` (`title`, `pages_count`, `price`, `description`, `updated_at`, `created_at`) values (My First Book!, 230, 10.5, A very original lorem ipsum dolor sit amet..., 2016-04-14 13:54:53, 2016-04-14 13:54:53))
I should say that I'm stuck at the point where I have just created my first model, & am trying to add 'My First Book' via the routes.php file.
Here is my routes file;
use Illuminate\Database\Schema\Blueprint;
Route::get('create_books_table', function() {
Schema::create('books', function(Blueprint $table)
{
$table->increments('id');
$table->string('title', 30);
$table->integer('pages_count');
$table->decimal('price', 5, 2);
$table->text('description');
$table->timestamps();
});
});
Route::get('update_books_table', function() {
Schema::table('books', function(Blueprint $table)
{
$table->string('title', 250)->change();
});
});
Route::get('update_books_table_2', function() {
//creating authors table p33...
Schema::create('authors', function(Blueprint $table)
{
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->timestamps();
});
Schema::table('books', function(Blueprint $table)
{
//creating the index on the title column p33...
$table->index('title');
//creating the foreign key ...
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('authors');
});
});
Route::get('book_create', function() {
$book = new \App\Book;
$book->title = 'My First Book!';
$book->pages_count = 230;
$book->price = 10.5;
$book->description = 'A very original lorem ipsum dolor sit amet...';
$book->save();
});
Here is a pair of migrations that are part of the project:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class PublishersUpdate extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('publishers', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::table('books', function(Blueprint $table)
{
$table->integer('publisher_id')->unsigned();
$table->foreign('publisher_id')->references('id')->on('publishers');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('books', function(Blueprint $table)
{
$table->dropForeign('books_publisher_id_foreign');
$table->dropColumn('publisher_id');
});
Schema::drop('publishers');
}
}
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class TagsUpdate extends Migration
{
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('book_tag', function(Blueprint $table) {
$table->increments('id');
$table->integer('book_id')->unsigned();
$table->integer('tag_id')->unsigned();
$table->foreign('book_id')->references('id')->on('books');
$table->foreign('tag_id')->references('id')->on('tags');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('book_tag');
Schema::drop('tags');
}
}
Finally, here is the 'Book' model - the protected $fillable line is the only deviation from what is stated in the book - it was my attempt to solve this dilemma based on stuff I'd read elsewhere:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model {
protected $fillable = ['id', 'author_id', 'title', 'pages_count', 'price', 'description', 'updated_at', 'created_at'];
}
Thanks in advance for any & every scrap of help you offer me - I'd really like to kick on & move through this book & hopefully one day create wonderful things with Laravel!