简体   繁体   中英

How to get property name by using ID in Lavarel

I make a search function based on the dropdown list and of course it is using ID. When I clicked button Search, it appears the ID number instead I want it to appear the name based on the ID.

Here is my complaint table

id
defect_id
description
report_by
residential_id

residential table

id
name
added_by

Here is my controller

public function search(Request $request)
{
   if ($request->residential_id)
        {
            $residence = $request->residential_id;
            $complaints = DB::table('complaints')->where('residential_id', $residence)->get();
            return view('admins.reports.result', compact('complaints', 'residence'));
        }
}

Here is the index view

<div class="col-md-4">
    <div class="panel panel-default">
        <div class="panel-heading">Complaint by Residence</div>
        <div class="panel-body">
            <form action="/search-report" method="post">
            @csrf
            <label>List of Residence</label>
                <select class="form-control" name="residential_id" >
                   <option value="">-- Choose Residence --</option>
                       @foreach(App\Residential::where('added_by',Auth::user()->id)->get() as $res)
                           <option value="{{$res->id}}">{{$res->name}}</option>
                       @endforeach
                </select>
                <br>
                <button type="submit" class="btn btn-primary">Search</button>
            </form>
        </div>
    </div>
</div>

Here is the result view after click button search

<h3 class="panel-title"><strong>All Complaints on
    @php
    if (isset($residence)) {
       echo $residence;
    }
    @endphp</strong></h3>
</div>
<table class="table table-hover" id="report-table">
   <thead>
      <tr>
         <th>Complaint ID</th>
         <th>Resident</th>
         <th>Description</th>
      </tr>
   </thead>
   @foreach($complaints as $c)
      <tr>
         <td>{{$c->id}}</td>
         <td>{{$c->report_by}}</td>
         <input type="text" class="form-control hidden" name="report_id" value="{{$c->id}}"/>
         <td>{{$c->description}}</td>
      </tr>
   @endforeach
</table>

Above code is my problem. For example, it will show All Complaints on 2 . I need it to be All Complaints on Evoke Residence . How can I do that?

You need to fetch residence name from residential table like

public function search(Request $request)
{
   if ($request->residential_id)
   {
       $residence = DB::table('residential')->where('id', $request->residential_id)->first();
       $complaints = DB::table('complaints')->where('residential_id', $residence->id)->get();
       return view('admins.reports.result', compact('complaints', 'residence'));
   }
}

and in your view you will do

<h3 class="panel-title">
    <strong> All Complaints on {{ $residence->name ?? "" }}</strong>
</h3>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM