1

So I am going through a series of intro videos to laravel and I am at the Database migration part and am struggling with a few things...

Here is the error... enter image description here

I have a) no idea what it means or is referring to as this is my first foray into real command prompt usage and b) how to fix it.

Any help would be much appreciated.

regards.

EDIT-1: Here is my migration file...

<?php

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

class CreateCardsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cards', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->timestamps();
        });
    }

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

EDIT 2: Here is my command prompt, it is working with the answer below, but now with the original laracast tutorial. enter image description here

6
  • Please add your migration_file Commented Jul 25, 2016 at 14:22
  • Something is missing in the migration file. Did you copy some source code directly? I suspect that later in the course, you'll add some logic to tie cards to users, so in another migration file, that column will be added. I think your source is beyond the step in the course that you're following. Commented Jul 25, 2016 at 14:30
  • here is my resource: laracasts.com/series/laravel-5-from-scratch/episodes/… play from 9:00 Commented Jul 25, 2016 at 14:32
  • You might have some leftovers, that should definitely work. Try php artisan migrate:refresh, then the original command again. Commented Jul 25, 2016 at 14:35
  • your schema doesn't have user_id ? Commented Jul 25, 2016 at 14:39

1 Answer 1

2

It means that you have to supply a user-id as your schema dictates that a card belongs to a user. Add ['user_id'=>some user id] to your array.

Alternatively, create a cards relation on your User-model:

class User extends \Illuminate\Database\Eloquent\Model
{
    public function cards()
   {
      return $this->hasMany(Card::class);
   }
}

This way, you can create a card like so (assuming you have a user variable, fx. by calling User::first(), User::find($id) etc.):

$user->cards()->create([...])
Sign up to request clarification or add additional context in comments.

13 Comments

trying that now, what gets me is that I followed the instructor to the letter (in their 2016 up-to-date course) and get this error.
It seems a bit strange that they use DateTime as well; normally, you'd use the extended Carbon package. That's what Eloquent casts its own date fields to. Which course are you following? I guess I'm questioning the quality a bit.
This is coming straight from laracasts 101 laravel course and I am just following along... here is my command... DB::table('cards')->insert(['user_id'=> 1, 'title' => 'My new Card', 'created_at' => new DateTime, 'updated_at' => new DateTime]);
It seems to have worked, just testing a little further
If it's Laracasts, it's definitely good quality. That code you posted should work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.