【问题标题】:Laravel: In multiple links display data only with certain column valuesLaravel:在多个链接中仅显示具有某些列值的数据
【发布时间】:2018-02-20 21:58:00
【问题描述】:

使用复选框,我在表中输入值为 1 的数据。现在,例如,John 有 25 行,值为 1,Michael 15,Maria 10 等...现在,我创建了一个带有链接的引导列表(我将放入附件)并且我希望当我单击 Michael 时向我显示只有 Michael 值为 1 的行的表,对于 John、Maria 等也是如此。

bootstrap list image

我在那里有更多数据,例如,一些关系,所以当我使用来自纽约的用户登录时,只显示 town_id 为 NY 的名称,这很有效。名字 John、Michael、Maria 没有 ID,所以我无法关联它们,它只是带有变量的列。

路线如下所示:

Route::resource('/statistic', 'StatisticController');

控制器:

public function index()
{
    $activists = Activist::where('town_id', Auth::user()->town_id)->get();
    return view('statistic.index', compact('activists'));
}

public function show()
{
    $activists = Activist::where('town_id', Auth::user()->town_id)->paginate(10);
    return view('statistic.show', compact('activists'));
}

index.blade.php:

<ul class="list-group">
  <a href="{{ url('statistic/show') }}" class="list-group-item"><span class="badge">{{$activists->where('work_on_computer', 1) ? $activists->where('john', 1)->count() : '0'}}</span> John</a>
  <a href="{{ url('statistic/show') }}" class="list-group-item"><span class="badge">{{$activists->where('sticking_posters', 1) ? $activists->where('michael', 1)->count() : '0'}}</span> Michael</a>
  <a href="{{ url('statistic/show') }}" class="list-group-item"><span class="badge">{{$activists->where('sharing_flyers', 1) ? $activists->where('maria', 1)->count() : '0'}}</span> Maria</a>
</ul>

现在,当我单击链接时,它会显示整个表格,但我不知道如何仅过滤 John、Michael 或 Maria。对不起,如果我感到困惑。

【问题讨论】:

  • 在url中传入user(john) id,然后根据user id获取数据。
  • John 没有 ID,John 只是列名。我仅提及用户 ID 作为示例。
  • 你如何在没有关系的情况下对数据库中的记录(哪些数据与哪个用户相关)进行分类?
  • 不,我与用户和城镇有关系,这行得通。那是 $activists 变量。也许我把你和提到混淆了。我与 John、Michael、Maria 等列中的 1 值没有关系......因此我不知道如何过滤行。
  • @NenadM,你的路线是什么? statistic/show,应将 (john) 作为参数。即statistic/show/{slug},是吗?

标签: php mysql twitter-bootstrap laravel


【解决方案1】:

我猜你对资源路线感到困惑。它是这样工作的 -

Route::resource('statistic', 'StatisticController');

Actions Handled By Resource Controller

Verb         URI                    Action         Route Name
GET          /statistic             index          statistic.index
GET          /statistic/create      create         statistic.create
POST         /statistic             store          statistic.store
GET          /statistic/{Id}        show           statistic.show
GET          /statistic/{Id}/edit   edit           statistic.edit
PUT/PATCH    /statistic/{Id}        update         statistic.update
DELETE       /statistic/{Id}        destroy        statistic.destroy

希望这将帮助您弄清楚您的进一步行动。

【讨论】:

  • 我很困惑。当我必须链接到某些页面时,我知道路由以及如何检查路由名称,但我不知道如何处理参数,并且有人告诉我 params 是解决这个问题所需要的。如果您可以帮助我提供更多详细信息或发布具有类似解决方案的链接,那会很好...?
  • 像这样传递参数 - {{ URL::route('statistic.show', $id) }}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
相关资源
最近更新 更多