Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Lavarel中使用ID获取属性名_Php_Laravel - Fatal编程技术网

Php 如何在Lavarel中使用ID获取属性名

Php 如何在Lavarel中使用ID获取属性名,php,laravel,Php,Laravel,我根据下拉列表创建了一个搜索功能,当然它使用的是ID。当我单击按钮搜索时,它会显示ID号,而不是我希望它显示基于ID的名称 这是我的投诉表 id defect_id description report_by residential_id 居住表 id name added_by 这是我的控制器 public function search(Request $request) { if ($request->residential_id) {

我根据下拉列表创建了一个搜索功能,当然它使用的是ID。当我单击按钮搜索时,它会显示ID号,而不是我希望它显示基于ID的名称

这是我的投诉表

id
defect_id
description
report_by
residential_id
居住表

id
name
added_by
这是我的控制器

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'));
        }
}
这是索引视图

<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>

住宅投诉
@csrf
居住清单
--选择住所--
@foreach(App\Residential::where('added_by',Auth::user()->id)->get()作为$res)
{{$res->name}
@endforeach

搜寻
这是单击按钮搜索后的结果视图

<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>
所有关于
@php
如果(isset($居住)){
埃科公寓;
}
@endphp
投诉编号
居民
描述
@foreach(投诉为$c)
{{$c->id}
{{$c->报告人}
{{$c->description}
@endforeach

上面的代码是我的问题。例如,它将显示2上的所有投诉。我需要它所有关于Evoke Residence的投诉。我该怎么做?

您需要从
住宅
表中获取住宅名称,如

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'));
   }
}
在你看来,你会做到的

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

关于{{{$residence->name???”}的所有投诉