我使用Schema
s创建了以下表:
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
创建了模型,并将其视为一对一的关系(这是正确的吗?)。
我想使用这样的Eloquent查询创建一个具有给定Typ的Auktion:
$thetyp = App\Typ::where("id", "=", 1)->first();
$auktion->typ()->associate($thetyp);
并获取name
的{{1}}的{{1}}:
Typ
目前,Auktion
的模型如下所示:
$auktion->typ->name;
哪个不行。我已经尝试过设置不同的关系,但我只是以不同的错误代码结束,范围从未定义的方法Auktion
到SQL语句尝试更新 public function typ()
{
return $this->hasOne('App\Typ', 'typ');
}
表失败的错误(使用(associate()
时 - 我真的不想这样做。
有人可以为我澄清这个问题并解释我必须使用哪种关系?
EDIT1 :正如评论中所述,我已尝试在typen
模型中使用save()
belongsTo
在调用
时会产生Auktion
public function typ()
{
return $this->belongsTo('App\Typ', 'typ');
}
并在
中调用Undefined property: App\Auktion::$typ
时
$thetyp = App\Typ::where("id", "=", 1)->first();
$auktion->typ()->save($thetyp);
EDIT2 :
我只想到了
$auktion->typ->name;
确实在起作用。 But referring to this answer这应与
相同Trying to get property of non-object
我究竟做错了什么?
EDIT3 :
我尝试使用建议的代码:
echo $auktion->typ()->first()->name;
在我导航到视图后,我运行了我得到的代码:
我今天第二次得到了这个,无处不在
答案 0 :(得分:1)
你的第二次编辑很有意义。您的方法名称为typ
,但它也是列的名称。因此,当您使用$auktion->typ()
时,它实际上正在加载关系。当您使用$auktion->typ
时,它会抓取该列的值。
您需要继续以这种方式使用括号加载关系而不使用括号来获取列的值,或者您可以将列的名称更改为更好的内容,例如typ_id
最终是Laravel所期望的,并且应该让你免于更多类似的麻烦。
答案 1 :(得分:1)
以下是一些代码增强功能:
$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);
问题在于关系是向后定义的。您需要进行拍卖belongTo
输入,并将类型更改为hasMany
拍卖。声明如下:
" A Type有很多拍卖。拍卖会有一种类型"。
这些课程(英文,对不起,我的德语很糟糕:(所以我只是用英语做了)和迁移:
-Auction class:
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();
});
-Type class:
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
对象(或者将其与查询一起插入),这样我们就可以拥有一个Type
行,我们可以将其与Auction
个对象/条目相关联,执行以下操作:
//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
我希望这有帮助!如果您还有其他问题,请与我们联系!
答案 2 :(得分:0)
您可以在Auktion模型中尝试此操作
$thetyp = App\Typ::find(1);
$auktion->typ->save($thetyp);
现在获取
$auktion->typ->name