php - Laravel:foreach 语句中的重复条目

标签 php mysql laravel

我有一个列表功能,允许用户将特定文章添加到个性化列表中。如果我将多于一篇文章添加到同一列表中。该列表在下拉菜单中重复。

示例:

enter image description here

The more articles i add to the specific list. so it duplicates.

数据库表

list          | id | person_id  | name      | description
              | 1    15         | Test List | null

data_list_ref | id | list_id   | data_uid
              | 1  | 1         | 9b888e1e9e
              | 2  | 1         | jZ-mAtgh

查询

$lists = DB::table('list')
  ->leftJoin('data_ref_list', 'data_ref_list.list_id', '=', 'list.id')
  ->select('list.id', 'list.name', 'data_ref_list.data_uid')
  ->where('person_id', Auth::user()->person_id)
  ->get();

Blade 实现

$record->key is the current 'article' being viewed in the browser

@foreach($lists as $list)
  @if($list->data_uid === $record->key)  
     // show the checked list 
     // user's cant add the same article twice to a specific list
  @else
     // show the user's other lists
  @endif
@endforeach

最佳答案

尝试使用 Eloquent:关系。

创建 2 个模型 https://laravel.com/docs/5.3/eloquent#defining-models 一种用于“列表”,一种用于 data_list_ref。

在模型“list”中创建与“data_list_ref”的关系 https://laravel.com/docs/5.3/eloquent-relationships#defining-relationships

像这样将模型包含在 Controller 中

use App\List;

现在,如果您想从“List”和“data_list_ref”获取数据,您可以这样调用它

$list = List::where('person_id', Auth::user()->person_id)->get();

这是 Laravel 获取数据的方式。

关于php - Laravel:foreach 语句中的重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39769008/

相关文章:

php - MySQL查询,多列求和

mysql - CHAR(n) 如何以二进制表示其数据?

mysql - mysql中的逗号分隔值分为多行

mysql "paste"两个结果在一起(并排)

php - 试图在 laravel 5.2 中获取非对象的属性

php - 无法解析 [Illuminate\Mail\TransportManager] 的 NULL 驱动程序。用于流明框架

php - jQuery AJAX,PHP 通过 JSON 返回(登录表单)

java - 即使我指定它获取所有行,我的 Web 服务也不会从数据库返回所有行

php - 执行或 Shell_exec : variables don't pass from php to the script

php - 如何在甜蜜警报中使用日期时间选择器?