Laravel belongsTo 抛出未定义函数 App/BelongsTo() 异常
Laravel belongsTo throwing undefined function App/BelongsTo() exception
我有一个评论 table 和用户 table 的关系:user->hasMany('comments') 和 comment->belongsTo('user')。出于某种我无法理解的原因,我一直收到这个
FatalErrorException in Comment.php line 22: Call to undefined function App\belongsTo()
我的其他模型与 HasMany 关系没有任何问题,但是,评论模型对我尝试的每件事都有问题(即使我使用 HasMany 只是为了看看它是否会有不同的错误)。
这里是评论模型。
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');
}
}
这是用户 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');
}
}
这里是评论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');
}
}
最后,当我调用 $comment->user() 时,它因错误而失败。有谁知道这个错误是从哪里来的?
谢谢。
好吧,发生这个错误是因为我有 '.'代替'->'。我无法弄清楚为什么它总是抛出完全相同的错误,无论我是 $this.belongsTo('App\User');
还是 $this.hasMany('App\User');
甚至 $this.thecakeisalie('App\User');
直到我再次坐在我的许多模型之间盯着文本.然后,你瞧,这是我的另一个愚蠢、微小且很难定位的错误(通常是这样)。
return $this.belongsTo('App\User', 'user_id', 'id');
到
return $this->belongsTo('App\User');
我认为这是使用 .
访问属性的其他语言的常见错误。在PHP中,应该是->
.
$this.belongsTo('...')
应该改写为
$this->belongsTo('...')
我有一个评论 table 和用户 table 的关系:user->hasMany('comments') 和 comment->belongsTo('user')。出于某种我无法理解的原因,我一直收到这个
FatalErrorException in Comment.php line 22: Call to undefined function App\belongsTo()
我的其他模型与 HasMany 关系没有任何问题,但是,评论模型对我尝试的每件事都有问题(即使我使用 HasMany 只是为了看看它是否会有不同的错误)。
这里是评论模型。
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');
}
}
这是用户 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');
}
}
这里是评论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');
}
}
最后,当我调用 $comment->user() 时,它因错误而失败。有谁知道这个错误是从哪里来的?
谢谢。
好吧,发生这个错误是因为我有 '.'代替'->'。我无法弄清楚为什么它总是抛出完全相同的错误,无论我是 $this.belongsTo('App\User');
还是 $this.hasMany('App\User');
甚至 $this.thecakeisalie('App\User');
直到我再次坐在我的许多模型之间盯着文本.然后,你瞧,这是我的另一个愚蠢、微小且很难定位的错误(通常是这样)。
return $this.belongsTo('App\User', 'user_id', 'id'); 到 return $this->belongsTo('App\User');
我认为这是使用 .
访问属性的其他语言的常见错误。在PHP中,应该是->
.
$this.belongsTo('...')
应该改写为
$this->belongsTo('...')