You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 10:无订单时显示用户名而非用户ID的解决方法

用户订单搜索功能问题修复

问题说明

  • 功能场景:通过下拉框选择用户,搜索并展示该用户的订单列表。
  • 异常情况:当用户无订单时,页面标题处显示的是用户ID,而非用户名。

相关代码文件

report_by_user.blade.php

@extends('admin.admin_dashboard')
@section('admin')

<div class="page-content">
    <!--breadcrumb-->
    <div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
        <div class="breadcrumb-title pe-3">Ecommerce Report</div>
        <div class="ps-3"> 
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0 p-0">
                    <li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a>
                    </li>
                    <li class="breadcrumb-item active" aria-current="page">Ecommerce Report</li>
                </ol>
            </nav>
        </div>
          
    </div>
    <!--end breadcrumb-->
     
    <hr/>
     

<div class="row row-cols-1 row-cols-md-1 row-cols-lg-3 row-cols-xl-3">
     

<form method="post" action="{{ route('search-by-user')}}">
    @csrf
    <div class="col">
        <div class="card">
             
            <div class="card-body">
                <h5 class="card-title">Search By User</h5>
                

                <label class="form-label">Select User:</label>
                <select name="user" class="form-select mb-3" aria-label="Default select example">
                    <option selected="">Open this select User</option>
                    @foreach($users as $user)
                        <option value="{{ $user->id }}">{{ $user->name }}</option>
                    @endforeach
                </select>


                <br>
                <input type="submit" class="btn btn-rounded btn-primary" value="Search">
            </div>
             
             
        </div>
    </div>
</form>



</div> 

     
</div>

@endsection

report_by_user_show.blade.php

@extends('admin.admin_dashboard')
@section('admin')

<div class="page-content">
    <!--breadcrumb-->
    <div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
        <div class="breadcrumb-title pe-3">All Order By User Report</div>
        <div class="ps-3"> 
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0 p-0">
                    <li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a>
                    </li>
                    <li class="breadcrumb-item active" aria-current="page">Order By User Report</li>
                </ol>
            </nav>
        </div>
        <div class="ms-auto">
            <div class="btn-group">
                
            </div>
        </div>
    </div>
    <!--end breadcrumb-->
    <h3> Search By User Name  : {{ $userName }} </h3>
    <hr/>
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table id="example" class="table table-striped table-bordered" style="width:100%">
                    <thead>
                        <tr>
                            <th>Sl</th>
                            <th>Date </th>
                            <th>Invoice </th>
                            <th>Amount </th>
                            <th>Payment </th>
                            <th>State </th>
                            <th>Action</th> 
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($orders as $key => $item)      
                        <tr>
                            <td> {{ $key+1 }} </td>
                            <td>{{ $item->order_date }}</td>
                            <td>{{ $item->invoice_no }}</td>
                            <td>AED {{ $item->amount }}</td>
                            <td>{{ $item->payment_method }}</td>
                            <td> <span class="badge rounded-pill bg-success"> {{ $item->status }}</span></td> 
                            
                            <td>
                                <a href="{{ route('admin.order.details',$item->id) }}" class="btn btn-info" title="Details"><i class="fa fa-eye"></i> </a>

                                <a href="{{ route('admin.invoice.download',$item->id) }}" class="btn btn-danger" title="Invoice Pdf"><i class="fa fa-download"></i> </a>
                            

                            </td> 
                        </tr>
                        @endforeach
                        
                        
                    </tbody>
                    <tfoot>
                        <tr>
                            <th>Sl</th>
                            <th>Date </th>
                            <th>Invoice </th>
                            <th>Amount </th>
                            <th>Payment </th>
                            <th>State </th>
                            <th>Action</th> 
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
    

    
</div>

@endsection

ReportController.php

class ReportController extends Controller {
 
    public function OrderByUser(){
        $users = User::where('role','user')->latest()->get();
        return view('backend.report.report_by_user',compact('users'));
    }// End Method 

    public function SearchByUser(Request $request){
        $users = $request->user;
        $orders = Order::where('user_id',$users)->latest()->get();
        return view('backend.report.report_by_user_show',compact('orders','users'));
    }// End Method 
}

Order.php

class Order extends Model
{
    use HasFactory;
    protected $guarded = [];
 
    public function user(){
        return $this->belongsTo(User::class,'user_id','id');
    }
} 

问题原因

  1. 控制器SearchByUser方法中,仅将用户ID($request->user)赋值给$users变量并传递给视图,没有获取完整的用户模型。
  2. 视图中使用{{ $userName }}显示用户名,但控制器并未传递该变量;当有订单时可通过订单关联的user关系间接获取名称,但无订单时无关联数据,导致页面直接显示用户ID。

修复方案

方案一:规范获取用户模型(推荐)

修改ReportController.php的SearchByUser方法,获取完整用户模型并传递给视图:

public function SearchByUser(Request $request){
    $userId = $request->user;
    // 获取用户模型,若用户不存在则抛出404
    $user = User::findOrFail($userId);
    $orders = Order::where('user_id',$userId)->latest()->get();
    // 传递user模型而非ID
    return view('backend.report.report_by_user_show',compact('orders','user'));
}

然后修改report_by_user_show.blade.php中的标题代码:

<h3> Search By User Name  : {{ $user->name }} </h3>

方案二:优化无订单提示(可选)

在视图中添加订单为空时的提示,提升用户体验:

<h3> Search By User Name  : {{ $user->name }} </h3>
<hr/>
@if($orders->isEmpty())
    <div class="alert alert-warning">该用户暂无任何订单记录</div>
@else
    <div class="card">
        <!-- 原有表格代码 -->
    </div>
@endif

内容的提问来源于stack exchange,提问作者Muhammad Yaseen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 18:24:55