繁体   English   中英

在Laravel 5.3中处理雄辩的关系

[英]Dealing with Eloquent relationships in Laravel 5.3

我使用Schema创建了以下表:

  Schema::create('typen', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name')->unique();
  });

 Schema::create('auktionen', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('typ')->unsigned();

    $table->foreign('typ')->references('id')->on('typen');
  });

typen仅在包含固定值的情况下创建一次:

Typen
id| name
1 | Typ 1
2 | Typ 2
...

这些值是恒定的,在应用程序生命周期内不会再添加任何值。 因此,我创建的每个Auktion都应与一个Typ相关联。 我为Auktion创建了模型, Auktion其视为一对一的关系(对吗?)。

我想使用这样的口才查询来创建具有给定类型的Auktion:

  $thetyp = App\Typ::where("id", "=", 1)->first();
  $auktion->typ()->associate($thetyp);

并取name中的Typ我的Auktion

$auktion->typ->name;

目前,我的模型在Auktion看起来像这样:

  public function typ()
  {
    return $this->hasOne('App\Typ', 'typ');
  }

这是行不通的。 我已经尝试设置不同的关系,但是我遇到的错误代码范围很广,从未定义的方法(associate()到一个错误,在该错误中,SQL语句尝试更新我的typen表失败(使用save() ,我真的不想这样做) )。

有人可以为我澄清这个问题并解释我必须使用哪种关系吗?

EDIT1 :如评论中所述,我已经尝试在Auktion模型中使用belongsTo

  public function typ()
  {
    return $this->belongsTo('App\Typ', 'typ');
  }

导致Undefined property: App\\Auktion::$typ调用时Undefined property: App\\Auktion::$typ

 $thetyp = App\Typ::where("id", "=", 1)->first();
 $auktion->typ()->save($thetyp);

并在调用$auktion->typ->name;

Trying to get property of non-object

编辑2

我只是觉得

echo $auktion->typ()->first()->name;

确实在工作。 但是参考这个答案应该与

echo $auktion->typ->name;

我到底在做什么错?

编辑3

我尝试使用建议的代码:

  $thetyp = App\Typ::find($typ);
  $auktion->typ->save($thetyp);

在导航到该视图之后,我运行了代码:

在此处输入图片说明

我今天第二次拿到这个

您的第二次编辑很有意义。 您的方法名称是typ但这也是列的名称。 因此,当您使用$auktion->typ()它实际上是在加载关系。 当您使用$auktion->typ它将获取该列的值。

您需要继续使用圆括号来加载关系,而不需要使用圆括号来获取列的值,或者将列的名称更改为更好的名称(例如typ_id ,这最终是Laravel期望的)并可以避免以后遇到更多类似的麻烦。

这是一些代码增强功能:

 $thetyp = App\Typ::where("id", "=", 1)->first();
 $auktion->typ()->save($thetyp);

至:

//Will return null if Model is not found
$thetyp = App\Typ::find(1);
//You actually have to retrieve the relationship, because calling typ()
//will only retrieve the Relationship query
$auktion->typ()->get()->save($thetyp);

问题在于关系是向后定义的。 您需要将Auction设置为belongTo类型,并将Type更改为hasMany Auctions。 该语句将显示为:

“一种类型有很多拍卖。一种拍卖有一种类型”。

这是与迁移有关的课程(英语,对不起,我的德语不好:(所以我只是用英语做了):

-拍卖类:

class Auction extends Model
{
    protected $table = 'auction';

    public function type()
    {
        return $this->belongsTo('App\Type');
    }
}

-拍卖迁移:

Schema::create('auction', function(Blueprint $table){

$table->increments('id');
$table->integer('type_id')->references('id')->on('type')->nullable();
$table->string('title')->nullable();
$table->string('description')->nullable();

$table->timestamps();

});

-类型类:

class Type extends Model
{
    protected $table = 'type';

    public function auction()
    {
        return $this->hasMany('App\Auction');
    }
}

-类型迁移:

 Schema::create('type', function(Blueprint $table){

      $table->increments('id');
      $table->string('name');

      $table->timestamps();
 });

首先,您可以创建一个Type对象(或在查询中插入它),以便我们可以拥有一个与Auction对象/条目相关的Type行,并执行以下操作:

//Example of Type obj creation

$type = new Type();
$type->name = 'Type #1';
//Don't forget to save
$type->save();

//OR find/retrieve a Type obj

//Where $id is the id of the Type obj, else the obj will be null
$type = Type::find($id);

$auction = new Auction();

$auction->title = 'Title #1';
$auction->description = 'Test description';

$auction->type_id = $type->id;

//Don't forget to save
$auction->save();

现在,在代码的稍后部分,每当您使用Auction对象并且想要检索关联的类型(如果有)时,都可以使用:

$type = $auction->type()->get();

这将返回Type的实例,您将能够像下面这样检索属性名称:

$type->name

我希望这有帮助! 让我知道您是否还有其他问题!

您可以在Auktion模型中尝试

$thetyp = App\Typ::find(1);
$auktion->typ->save($thetyp);

现在获取

$auktion->typ->name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM