这是我第一次在这里发帖,对不起,如果我输入错误。 所以我有Forums.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Forums extends Model
{
protected $table ='forums';
public $primaryKey='id';
public function section()
{
return $this->belongsTo(Section::class,'id');
}
}
和Section.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Section extends Model
{
public function forums()
{
return $this->hasmany(Forums::class,'section_id');
}
}
我休闲的教程显示使用
$section=Section::find($id)->forums;
我应该获得所有fk等于$ id部分的论坛,但是ir返回空数组。 我试图在终端中使用以下命令输出sql:
$order = App\Section::find(2)->forums()->toSql()
其中2是区段表的ID并得到:
select * from `forums` where `forums`.`section_id` is null and `forums`.`section_id` is not
null"
所以难怪它会返回空数组 但是,如果我尝试获取与论坛相关的部分,则效果很好。
我该如何解决?
将hasmady更改为hasMany并没有解决
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('Forums', function (Blueprint $table) {
$table->increments('id');
$table->string('Name');
$table->string('Description');
$table->integer('topics');
$table->integer('Comments');
$table->integer('Last_Post_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('Forums');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddToUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('Forums', function (Blueprint $table)
{
$table->integer('Section_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
虽然我在数据库中有表,但似乎找不到部分shema,或者是因为shema不在项目文件夹中,因为我没有收到找不到表或col的错误?