1
votes

Using a Mac, Php version 7.1.19, the latest version of Laravel and a SQLite database. The website has 3 models. User, Comment, and Post. I would like to connect the Comment to User. So people can see their own comments on their dashboard. The database should be set up correctly, the Comment table has ['id','user_id','body','created_at','updated_at']. The tinker error tells me that it needs a return, but I added a return to both functions.

Could you perhaps elaborate on what I'm currently doing wrong?

In User.php, I placed a comments function.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Comment;

class User extends Authenticatable
{


  use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'firstname','lastname','age', 'email', 'password',
];

public function comments()
{
   return $this->hasMany(Comment::class);
}


/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];
}

In Comment.php, I placed a users function.

<?php

namespace App;

use App\User;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    public function user()
    {
       return $this->belongsTo(User::class);
    }
}

Update, requested database model code:

<?php

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

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('user_id');
            $table->string('body');
            $table->timestamps();
        });
    }

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

User Migration code:

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('firstname');
            $table->string('lastname');
            $table->integer('age');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->integer('isAdmin')->default(0);
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

The system shows the following error when using Php Artisan Tinker:

App\User::first()->comments LogicException with message 'App/User::comments must return a relationship instance.'

1
Did you set the $comments property anywhere in your App\User class? Please post the full code for this class. Could you also post the database model for this class?Jerodev
provide all models code.ThataL
@Jerodev Thank you for your comment. I added all the code you requestedS-o
@ThataL I just edited my post so you can check the whole code.S-o
Could you add the migrations for the user table?Jerodev

1 Answers

1
votes

Database Model add relation with the users table

<?php

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

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');

            $table->string('body');
            $table->timestamps();
        });
    }

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

Change comments method in User.php model

public function comments()
{
   return $this->hasMany(Comment::class,'user_id','id');
}