【问题标题】:search Undefined variable in laravel在laravel中搜索未定义的变量
【发布时间】:2019-10-25 07:33:23
【问题描述】:

我想写一个搜索功能,但是当我点击搜索按钮时出现错误(未定义变量:请求)..

这是我的表格

<div class="container">
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
          <div class="card-header">
            <form action="/member_search" methode="get">
              <div class="input-group">
                <input type="search"  name="searchinput" class="form-control">
                <span class="input-group-prepend">
                <button type="submit" class="btn btn-primary">search</button>
              </div>
            </form>
           </div>

            <div class="card-body">
              <table class="table">

                <thead>
                  <tr>
                    <th scope="col">status</th>
                    <th scope="col">type</th>
                    <th scope="col">date</th>
                    <th scope="col"> request number</th>
                  </tr>
                </thead>
                <tbody>
                    @foreach ($requests as $request)
                    <tr>
                    <th scope="col">{{$request->status->name}}</th>
                    <th scope="col">{{$request->type->name}}</th>
                    <th scope="row">{{$request->created_at}}</th>
                    <th scope="row">{{$request->uniid}}</th>
                    </tr>
                @endforeach
                </tbody>
               </table>
               </div>
             </div>
            </div>
         </div>
       </div>

这是我在控制器中的函数,我收到错误未定义变量:使用此函数的请求

  public function searchmember(Request $request)
{
$searchinput= $request->get('searchinput');
$posts= DB::table('students')->where ( 'uniid', 'LIKE', '%' . $searchinput . '%' )->paginate(1);
return view('Committee.Request.index', ['posts' => $posts]);

}

这是我的路线,我尝试将其作为 (get) 但它不起作用

Route::any('/member_search','RequestController@searchmember');

【问题讨论】:

  • 你有2个return语句,函数会在第一个return处停止,第二个return不能到达。
  • $requests 应该是什么?你不应该循环$posts吗?
  • 我删除了第二个 return .. 你的意思是我应该在哪里使用 $posts?
  • 你检查了吗?

标签: php laravel


【解决方案1】:

你应该在循环中使用变量传递给视图的变量:

@foreach ($posts as $post)
    <tr>
        <th scope="col">{{$post->status->name}}</th>
        <th scope="col">{{$post->type->name}}</th>
        <th scope="row">{{$post->created_at}}</th>
        <th scope="row">{{$post->uniid}}</th>
    </tr>
@endforeach

【讨论】:

  • 我改用函数而不是 public function searchmember() { $search = Input::get ( 'search' ); $requests= DB::table('sickleaves')-&gt;where ( 'uniid', 'LIKE', '%' . $search . '%' )-&gt;paginate(1); return view('Committee.Request.index', ['requests' =&gt; $requests]); 的形式,现在的错误是 Undefined property: stdClass::$status
  • 好的,你想用$requests就好了。那么在您的循环中,请您尝试&lt;th scope="col"&gt;{{$request-&gt;status[0]-&gt;name}}&lt;/th&gt;
  • 如果我写 {{$request->status[0]->name}} 显示错误 Trying to get property 'name' of non-object
  • 嗯,好吧,然后这样写 - {{$request-&gt;status[0]-&gt;name or ''}}
  • 错误尝试获取非对象的属性“名称”
猜你喜欢
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 2015-05-15
  • 2016-10-23
  • 2020-11-04
  • 2018-12-12
  • 1970-01-01
  • 2015-02-26
相关资源
最近更新 更多