如果目标在一个 table 中,活动在另一个中,如何显示实现不同目标的进展
How to show progress towards achieving different goals, if goals are in one table and activities in another
我正在与 Laravel 5.7 合作。在我的应用程序中,用户填写一个链接到目标 table 的表单来定义他们的目标(要完成的活动数量,按类型 activity,在开始日期和结束日期之间)。用户可以有任意多的目标。
另一个表格用于按日期和 activity 的类型记录每个已完成 activity(链接到活动 table)的详细信息。两个 table 都包含链接的唯一 user_ids。
对于每个唯一目标(目标 id),我需要找到所有符合该目标的已完成活动,并且重要的是,计算针对每个目标完成的活动总数。
目标是有一个 table 来详细说明每个目标,并在同一个 table 中显示针对每个独特目标的进度。
请大家帮忙,因为我已经坚持了很长时间而且我只是一个初学者!
我已通过唯一 user_id 内部加入目标和活动 table 并设置 where 子句以将已完成的活动与目标条件相匹配。
我只设法获得所有目标的已完成活动总量($progress_against_goal)。我需要给出每个独特目标完成的总活动数。
来自活动table:
Schema::create('activities', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->date('date');
$table->string('activity');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');});
来自目标 table:
Schema::create('goals', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('target_number_of_activities');
$table->string('activity');
$table->date('goal_start');
$table->date('goal_end');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); });
来自控制器:
public function index()
{
$query = DB::table('activities')
->join('goals', 'activities.user_id', 'goals.user_id')
->select('activities.*', 'goals.*')
->where('goals.user_id', auth()->id())
->whereRaw('activities.date >= goals.goal_start')
->whereRaw('activities.date <= goals.goal_end')
->whereRaw('activities.activity = goals.activity')
->get();
$progress_against_goal = $query->count();
$goals = Goal::where('user_id', auth()->id())->get();
}
来自 Blade 视图:
@foreach ($goals as $goal)
<tr><td>{{$goal->target_number_of_activities}}</td>
<td>{{$goal->activity}}</td>
<td>{{$goal->goal_start)</td>
<td>{{$goal->goal_end)</td>
<td>{{ $progress_against_goal }}</td></tr>
@endforeach
我只设法获得所有目标的已完成活动总量($progress_against_goal)。我需要给出每个独特目标完成的总活动数。
您需要声明 Goal
模型和 Activities
模型之间的关系。
class Goal extends Model
{
public function activities()
{
$query = Activities::where('date', '>=', $this->goal_start)->where('date', '<', $this->goal_end)->where('activity', '=', $this->activity);
return $this->newHasMany($query, $this, 'user_id', 'user_id');
}
public function getProgressAttribute()
{
return $this->activities()->count();
}
}
控制器
public function index()
{
$goals = Goal::where('user_id', auth()->id())->get();
}
blade
@foreach ($goals as $goal)
<tr><td>{{$goal->target_number_of_activities}}</td>
<td>{{$goal->activity}}</td>
<td>{{$goal->goal_start)</td>
<td>{{$goal->goal_end)</td>
<td>{{$goal->progress}}</td></tr>
@endforeach
免责声明: 此关系仅适用于模型实例 Goal
,它不会在查询构建器中工作喜欢
Goal::with('activities')->get();
//or
Goal::withCount('activities')->get();
我正在与 Laravel 5.7 合作。在我的应用程序中,用户填写一个链接到目标 table 的表单来定义他们的目标(要完成的活动数量,按类型 activity,在开始日期和结束日期之间)。用户可以有任意多的目标。
另一个表格用于按日期和 activity 的类型记录每个已完成 activity(链接到活动 table)的详细信息。两个 table 都包含链接的唯一 user_ids。
对于每个唯一目标(目标 id),我需要找到所有符合该目标的已完成活动,并且重要的是,计算针对每个目标完成的活动总数。
目标是有一个 table 来详细说明每个目标,并在同一个 table 中显示针对每个独特目标的进度。
请大家帮忙,因为我已经坚持了很长时间而且我只是一个初学者!
我已通过唯一 user_id 内部加入目标和活动 table 并设置 where 子句以将已完成的活动与目标条件相匹配。
我只设法获得所有目标的已完成活动总量($progress_against_goal)。我需要给出每个独特目标完成的总活动数。
来自活动table:
Schema::create('activities', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->date('date');
$table->string('activity');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');});
来自目标 table:
Schema::create('goals', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('target_number_of_activities');
$table->string('activity');
$table->date('goal_start');
$table->date('goal_end');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); });
来自控制器:
public function index()
{
$query = DB::table('activities')
->join('goals', 'activities.user_id', 'goals.user_id')
->select('activities.*', 'goals.*')
->where('goals.user_id', auth()->id())
->whereRaw('activities.date >= goals.goal_start')
->whereRaw('activities.date <= goals.goal_end')
->whereRaw('activities.activity = goals.activity')
->get();
$progress_against_goal = $query->count();
$goals = Goal::where('user_id', auth()->id())->get();
}
来自 Blade 视图:
@foreach ($goals as $goal)
<tr><td>{{$goal->target_number_of_activities}}</td>
<td>{{$goal->activity}}</td>
<td>{{$goal->goal_start)</td>
<td>{{$goal->goal_end)</td>
<td>{{ $progress_against_goal }}</td></tr>
@endforeach
我只设法获得所有目标的已完成活动总量($progress_against_goal)。我需要给出每个独特目标完成的总活动数。
您需要声明 Goal
模型和 Activities
模型之间的关系。
class Goal extends Model
{
public function activities()
{
$query = Activities::where('date', '>=', $this->goal_start)->where('date', '<', $this->goal_end)->where('activity', '=', $this->activity);
return $this->newHasMany($query, $this, 'user_id', 'user_id');
}
public function getProgressAttribute()
{
return $this->activities()->count();
}
}
控制器
public function index()
{
$goals = Goal::where('user_id', auth()->id())->get();
}
blade
@foreach ($goals as $goal)
<tr><td>{{$goal->target_number_of_activities}}</td>
<td>{{$goal->activity}}</td>
<td>{{$goal->goal_start)</td>
<td>{{$goal->goal_end)</td>
<td>{{$goal->progress}}</td></tr>
@endforeach
免责声明: 此关系仅适用于模型实例 Goal
,它不会在查询构建器中工作喜欢
Goal::with('activities')->get();
//or
Goal::withCount('activities')->get();