Php 拉威尔的关系似乎不起作用

Php 拉威尔的关系似乎不起作用,php,laravel,php-5.4,laravel-6.2,Php,Laravel,Php 5.4,Laravel 6.2,我在Laravel中有一个项目和一个项目对象。我想在项目和广告项目之间创建一个1对1的关系 项类如下所示 <?php namespace App; use Illuminate\Database\Eloquent\Model; class Item extends Model { // public function Category(){ return $this->belongsTo(Category::class); }

我在Laravel中有一个项目和一个项目对象。我想在项目和广告项目之间创建一个1对1的关系

类如下所示

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    //
    public function Category(){
        return $this->belongsTo(Category::class);
    }

    public function Currency(){
        return $this->hasOne(Currency::class);
    }

    public function AdvertItem(){
        return $this->hasOne(AdvertItems::class);
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AdvertItems extends Model
{
    protected $guarded = [];

    //
    public function items(){
        return $this->belongsTo(Item::class);
    }
}
 class CreateItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('description');
            $table->unsignedBigInteger('currency_lookup_id');
            $table->unsignedBigInteger('category_id')->index();
            $table->unsignedBigInteger('price');
            $table->string("image_path");
            $table->string('sale_ind');
            $table->Date('eff_from');
            $table->Date('eff_to');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('item');
    }
}
class CreateAdvertItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('advert_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('item_id');
            $table->unsignedBigInteger('customer_id');
            $table->Date('eff_from');
            $table->Date('eff_to');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('advert_items');
    }
}
广告表是这样创建的

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    //
    public function Category(){
        return $this->belongsTo(Category::class);
    }

    public function Currency(){
        return $this->hasOne(Currency::class);
    }

    public function AdvertItem(){
        return $this->hasOne(AdvertItems::class);
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AdvertItems extends Model
{
    protected $guarded = [];

    //
    public function items(){
        return $this->belongsTo(Item::class);
    }
}
 class CreateItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('description');
            $table->unsignedBigInteger('currency_lookup_id');
            $table->unsignedBigInteger('category_id')->index();
            $table->unsignedBigInteger('price');
            $table->string("image_path");
            $table->string('sale_ind');
            $table->Date('eff_from');
            $table->Date('eff_to');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('item');
    }
}
class CreateAdvertItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('advert_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('item_id');
            $table->unsignedBigInteger('customer_id');
            $table->Date('eff_from');
            $table->Date('eff_to');
            $table->timestamps();
        });
    }

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

请帮助。

以下规则将帮助您

  • 始终以小写字母开头关系名称。为类而不是方法节省资本

  • 模型应该是单一的

  • 注意名字的多样性。应该只有一个的事物应该是单数的。因此,在1:1的关系中,两个关系名称都应该是单数

广告项类

    public function item(){
        return $this->belongsTo(Item::class);
    }
然后,如果您有一个项目并且想要一个项目,您应该
加载它

$item->load('advertitem');
或者反过来

$advertItem->load('item');

与您的问题无关,但只是提醒一下,Laravel6.x需要PHP7.2或更高版本。我甚至不知道你是如何在5.4上安装它的,但我强烈建议更新。我可能把PHP5放错了,我实际上使用的是7.3.8