Php 处理Laravel 5.3中的雄辩关系
我使用Php 处理Laravel 5.3中的雄辩关系,php,laravel,eloquent,Php,Laravel,Eloquent,我使用Schemas创建了以下表格: Schema::create('typen', function (Blueprint $table) { $table->increments('id'); $table->string('name')->unique(); }); Schema::create('auktionen', function (Blueprint $table) { $table->increments('id');
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
创建了模型,并将其视为一对一的关系(这是正确的吗?)
我想使用如下雄辩的查询创建一个具有给定类型的Auktion:
$thetyp = App\Typ::where("id", "=", 1)->first();
$auktion->typ()->associate($thetyp);
并获取我的Auktion
的Typ
的name
:
$auktion->typ->name;
public function typ()
{
return $this->hasOne('App\Typ', 'typ');
}
目前,我的模型在Auktion
中看起来是这样的:
$auktion->typ->name;
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');
}
这将导致调用时
未定义属性:App\Auktion::$typ
$thetyp = App\Typ::where("id", "=", 1)->first();
$auktion->typ()->save($thetyp);
在中调用$auktion->typ->name;
时
Trying to get property of non-object
EDIT2:
我只是想
echo $auktion->typ()->first()->name;
确实有效。这应该与
echo $auktion->typ->name;
我到底做错了什么
EDIT3:
我尝试使用建议的代码:
$thetyp = App\Typ::find($typ);
$auktion->typ->save($thetyp);
在我导航到视图ehere并运行代码后,我得到以下信息:
我今天第二次得到这个,所以你可以在你的Auktion模型中试试
$thetyp = App\Typ::find(1);
$auktion->typ->save($thetyp);
现在取回
$auktion->typ->name
您可以在Auktion模型中尝试此功能
$thetyp = App\Typ::find(1);
$auktion->typ->save($thetyp);
现在取回
$auktion->typ->name
您的第二次编辑很有意义。您的方法名是
typ
,但这也是列的名称。因此,当您使用$auktion->typ()
时,它实际上是在加载关系。当您使用$auktion->typ
时,它会获取该列的值
您需要继续以这种方式工作,使用括号加载关系,而不使用括号获取列的值,或者您可以将列的名称更改为更好的名称,例如
typ\u id
,这正是Laravel所期望的,应该可以让您避免以后遇到更多类似的麻烦。 您的第二次编辑很有意义。您的方法名是typ
,但这也是列的名称。因此,当您使用$auktion->typ()
时,它实际上是在加载关系。当您使用$auktion->typ
时,它会获取该列的值
您需要继续以这种方式工作,使用括号加载关系,而不使用括号获取列的值,或者您可以将列的名称更改为更好的名称,例如
typ\u id
,这正是Laravel所期望的,应该可以让您避免以后遇到更多类似的麻烦。 下面是一些代码增强:
$thetyp = App\Typ::where("id", "=", 1)->first();
$auktion->typ()->save($thetyp);
致:
问题是关系的定义是向后的。您需要将拍卖设置为类型,并将类型更改为hasMany
拍卖。声明如下:
“一种类型有许多拍卖。一种拍卖有一种类型”
以下是关于迁移的课程(英语,对不起,我的德语不好:(所以我只是用英语做的):
-拍卖类别:
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();
});
Schema::create('type', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
-类型类别:
class Type extends Model
{
protected $table = 'type';
public function auction()
{
return $this->hasMany('App\Auction');
}
}
-类型迁移:
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();
});
Schema::create('type', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
首先,您可以创建一个类型
对象(或将其与查询一起插入),这样我们就可以有一个类型
行,该行可以与拍卖
对象/条目相关联,并执行以下操作:
//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();
现在,在代码的后面部分,每当您使用拍卖
对象并希望检索关联类型(如果有)时,您可以使用:
$type=$auction->type()->get();
它将返回类型的实例
,您将能够像这样检索属性名称:
$type->name
我希望这有帮助!如果您还有任何问题,请告诉我!以下是一些代码增强:
$thetyp = App\Typ::where("id", "=", 1)->first();
$auktion->typ()->save($thetyp);
致:
问题是关系的定义是向后的。您需要将拍卖设置为类型,并将类型更改为hasMany
拍卖。声明如下:
“一种类型有许多拍卖。一种拍卖有一种类型”
以下是关于迁移的课程(英语,对不起,我的德语不好:(所以我只是用英语做的):
-拍卖类别:
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();
});
Schema::create('type', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
-类型类别:
class Type extends Model
{
protected $table = 'type';
public function auction()
{
return $this->hasMany('App\Auction');
}
}
-类型迁移:
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();
});
Schema::create('type', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
首先,您可以创建一个类型
对象(或将其与查询一起插入),这样我们就可以有一个类型
行,该行可以与拍卖
对象/条目相关联,并执行以下操作:
//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();
现在,在代码的后面部分,每当您使用拍卖
对象并希望检索关联类型(如果有)时,您可以使用:
$type=$auction->type()->get();
它将返回类型的实例
,您将能够像这样检索属性名称:
$type->name
我希望这有帮助!如果您还有任何问题,请告诉我!删除括号。尝试$auktion->typ->save($thetyp)未定义的属性:App\Auktion::$typ
您的总体目标是什么?在两个对象之间建立关系?看起来您正试图通过其“定义的方法”带来拍卖的类型。我说得对吗?没错,目前我只是在创建Auktion时分配typ
的ide> 。此外,当我访问$auktion->typ
时,我只会检索