我有一个列表功能,允许用户将特定文章添加到个性化列表中。如果我将多于一篇文章添加到同一列表中。该列表在下拉菜单中重复。
示例:
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/