Laravel belongsTo throwing undefined function App/BelongsTo() exception

2.6k views Asked by At

I have a comments table and user table with the relationship: user->hasMany('comments') and comment->belongsTo('user'). For some reason with which eludes me, I keep getting this

FatalErrorException in Comment.php line 22: Call to undefined function App\belongsTo()

My other models have no issue whatsoever with the HasMany relations, however, the comments model has a problem with every single thing i try (even if i use HasMany just to see if it will have a different error).

Here is the Comment model.

use Illuminate\Database\Eloquent\Model;

class Comment extends Model {

    protected $table = 'comments';

    protected $fillable = [
        'anime_id',
        'user_id',
        'user_comment'
        ];

    public function postedOnAnime($query)
    {
        $query->where('anime_id', '=', $this.id);
    }

    public function user()
    {
        return $this.belongsTo('App\User', 'user_id', 'id');
    }

    public function anime()
    {
        return $this.belongsTo('App\Anime');
    }

}

Here is the Users table:

<?php

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->increments('id');
            $table->integer('role');
            $table->string('name')->unique();
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

}

Here is the Comments table

<?php

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->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('anime_id')->unsigned();
            $table->text('user_comment');
            $table->timestamps();

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

            $table->foreign('anime_id')
                        ->references('id')
                        ->on('animes')
                        ->onDelete('cascade');
        });
    }

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

}

Finally, when I call $comment->user() it fails with the error. Does anyone know where this error comes from?

Thanks.

3

There are 3 answers

0
Logan Hasbrouck On BEST ANSWER

Well, this error occurred because I had '.' in place of '->'. I couldn't figure out why it was always throwing the exact same error regardless if I did $this.belongsTo('App\User'); or $this.hasMany('App\User'); or even $this.thecakeisalie('App\User'); until I sat staring at the text between my many models yet again. Then, lo and behold, its another dumb, tiny and really hard to locate mistake of mine(as it usually is).

0
Jeff Irwin On

I think this is a common mistake coming from other languages where . is used to access properties. In PHP, it should be ->.

$this.belongsTo('...') should be rewritten as $this->belongsTo('...')

0
G-Rajendra On

return $this.belongsTo('App\User', 'user_id', 'id'); to return $this->belongsTo('App\User');