【问题标题】:The appearance of the added category in "user services list" page“用户服务列表”页面新增类别的外观
【发布时间】:2019-01-03 14:07:31
【问题描述】:

我刚刚添加了一个类别服务(类别名称:Like),但是当我转到服务列表(在用户面板中)时,更多类别出现在 Like 类别之前。 Please see this video for a better understanding of my problem:

Serviceslist.blade.php 代码(在视图文件夹中):

@extends('user.layouts.master')
@section('site_title', 'Service List')
@section('page_title')
    <i class="fa fa-list-ol"></i> @lang('slide.serviceslist')
@endsection
@section('body')
    <div class="row">
        <div class="col-md-12">
            @foreach($categories as $category)
            <div class="card">
                <div class="card-header bg-dark text-white">
                    @if(!empty($category->category)) {{ $category->category->name }} @endif
                    </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                            <tr>
                                <th>Sevice code</th>
                                <th>Service name</th>
                                <th>Price</th>
                                <th>min</th>
                                <th>max</th>
                            </tr>
                            </thead>
                            <tbody>
                            @php
                            $items = \App\ServicePrice::where('category_id', $category->category_id)->where('user_id', Auth::id())->get();
                            @endphp
                            @foreach($items as $item)
                            @if(!empty($item->service))
                                <tr>
                                    <td> {{$item->service->id}}</td>
                                    <td> {{$item->service->name}}</td>
                                    <td>{{ $item->price }}</td>
                                    <td>{{ $item->service->min }}</td>
                                    <td>{{ $item->service->max }}</td>
                                </tr>
                            @endif
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
                @endforeach
        </div>
    </div>
@endsection

和服务列表控制器:

public function serviceList(){
 $categories = ServicePrice::where('user_id', Auth::id())->distinct()- 
 >get(['category_id']);
    return view('user.serviceList', compact('categories'));
}

【问题讨论】:

  • 假设$categories 包含多个类别,但名称为空。或者也许是一件神奇的事情。
  • 我在phpmyadmin中查看了分类表,只有“Like”分类。

标签: javascript php laravel


【解决方案1】:

您需要使用类别 id 对控制器中的类别进行排序,才能将最后一个类别视为第一个。

public function serviceList(){  
   $categories = ServicePrice::where('user_id', Auth::id())->distinct()->orderBy( "category_id", "DESC" )->get(['category_id']);
   return view('user.serviceList', compact('categories')); 
}

【讨论】:

  • 我做到了,但显示:方法 Illuminate\Database\Eloquent\Collection::orderBy 不存在。
  • order by 在 get() 之前
  • 但是现在,那些额外的类别仍然出现!在您的帮助下,“喜欢”类别排在第一位。
  • 好的,那有什么问题呢?我看不到视频,你也不太明白。你的动作是插入?列表?如果您正在创建类别,则必须发送到其他视图,如果您只想查看我给您解决方案的所有列表,如果您只想显示一个类别,请使用 id 或 put ->fisrt() 并仅使用一个值。,
  • 不是,我只添加了一个分类,分类名称是“Like”,但是在“服务列表”页面中,在这个分类之后,显示的是更多分类!但他们没有任何名称或服务。
猜你喜欢
  • 2013-01-30
  • 2012-05-23
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
  • 1970-01-01
相关资源
最近更新 更多