laravel点eloquent关系
laravel point eloquent releationship
我卡在设计点上了。
我尝试做了一个点函数,需要的函数总结如下
- 根据posts或评论
赚取积分
- 显示从 post 或评论
获得的积分
- 获取积分的路径必须提前定义好(例如,只有写字才能获得积分)
我总结上面必要的函数得到的关系如下
post,评论...等等 -> point -> pointType
点类型模型
class PointType extends Model
{
use HasFactory;
protected $table = "point_types";
protected $primaryKey = "id";
protected $guarded = [];
public function point()
{
return $this->hasMany('App\Models\Point', 'id', 'pointTypeID');
}
}
点模型
class Point extends Model
{
use HasFactory;
protected $table = "points";
protected $primaryKey = "id";
protected $guarded = [];
public function pointable()
{
return $this->morphTo('pointable', 'pointable_type', 'pointable_id');
}
public function pointType()
{
return $this->belongsTo('App\Models\PointType','pointTypeID', 'id');
}
}
点Table
id|pointable_type |pointable_id|pointTypeID|memberID|created_at |updated_at |
--+---------------+------------+-----------+--------+-------------------+-------------------+
1|App\Models\Post| 12| 1| 1|2021-06-13 13:22:51|2021-06-13 13:22:51|
点类型Table
id|type|point|
--+----+-----+
1|write_post | 5|
2|write_comment| 1|
完成以上操作后
我添加点如下
我只是想展示
我很难创建该功能。
赚取积分
$post = Post::find($id);
$post->points()->create([
'memberID' => auth()->id(),
'pointTypeID' => $pointType->id
]);
显示点
$userInfo = User::find($user->id);
// dd($user->id);
$points = array();
// $points['totalPoint'] = $userInfo->points->pointable->sum('point');
// $points['totalPoint'] = $userInfo->points->pointType->sum('point');
$points = (object)$points;
我问你一个问题。
我要实现的功能点关系设计对吗?
如何获取积分以及如何修改以上代码才能正常显示积分?
我认为你打算做的是:
$userInfo = User::with('points.pointType')->find($user->id);
$totalPoints = $userInfo->points->sum(function ($point) {
return $point->pointType->point;
});
这里的想法是根据类型对所有点求和,而不是对类型本身求和。
我卡在设计点上了。
我尝试做了一个点函数,需要的函数总结如下
- 根据posts或评论 赚取积分
- 显示从 post 或评论 获得的积分
- 获取积分的路径必须提前定义好(例如,只有写字才能获得积分)
我总结上面必要的函数得到的关系如下 post,评论...等等 -> point -> pointType
点类型模型
class PointType extends Model
{
use HasFactory;
protected $table = "point_types";
protected $primaryKey = "id";
protected $guarded = [];
public function point()
{
return $this->hasMany('App\Models\Point', 'id', 'pointTypeID');
}
}
点模型
class Point extends Model
{
use HasFactory;
protected $table = "points";
protected $primaryKey = "id";
protected $guarded = [];
public function pointable()
{
return $this->morphTo('pointable', 'pointable_type', 'pointable_id');
}
public function pointType()
{
return $this->belongsTo('App\Models\PointType','pointTypeID', 'id');
}
}
点Table
id|pointable_type |pointable_id|pointTypeID|memberID|created_at |updated_at |
--+---------------+------------+-----------+--------+-------------------+-------------------+
1|App\Models\Post| 12| 1| 1|2021-06-13 13:22:51|2021-06-13 13:22:51|
点类型Table
id|type|point|
--+----+-----+
1|write_post | 5|
2|write_comment| 1|
完成以上操作后 我添加点如下 我只是想展示 我很难创建该功能。
赚取积分
$post = Post::find($id);
$post->points()->create([
'memberID' => auth()->id(),
'pointTypeID' => $pointType->id
]);
显示点
$userInfo = User::find($user->id);
// dd($user->id);
$points = array();
// $points['totalPoint'] = $userInfo->points->pointable->sum('point');
// $points['totalPoint'] = $userInfo->points->pointType->sum('point');
$points = (object)$points;
我问你一个问题。
我要实现的功能点关系设计对吗?
如何获取积分以及如何修改以上代码才能正常显示积分?
我认为你打算做的是:
$userInfo = User::with('points.pointType')->find($user->id);
$totalPoints = $userInfo->points->sum(function ($point) {
return $point->pointType->point;
});
这里的想法是根据类型对所有点求和,而不是对类型本身求和。