samedi 30 avril 2016

Getting null as a result

I have created two tables, First is Posts table and seconds is Comments table. I'm using sqlite database.

I'm getting null as result for comment function using hasMany method in Post Model.

Posts table migration file:

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

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

Comments table migration file:

 class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
{
    Schema::create('Comments', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id')->unsigned()->index();
        $table->text('body');
        $table->timestamps();
    });
        Schema::table('Comments', function (Blueprint $table) {
            $table->foreign('post_id')->refrences('id')->on('Posts');
        });
}


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

Post Model:

class Post extends Model
{

    //
    public function comment(){
    return $this->hasMany(Comment::class);    
   //I have also tried: return $this->hasMany(Comment::class,'post_id','id');
    }
}

The data has been entered in both the tables and I'm getting results when I do

$post=new App\Post;
$post->get();

But when I try to get all the comments for a post using

$post->comment;

I'm getting

=>Illuminate\Database\Eloquent\Collection {#633
all:[],
}

Why am I getting null as a result?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1NaTlz1
via IFTTT

Aucun commentaire:

Enregistrer un commentaire