dpcyx08288 2018-06-08 19:54
浏览 120
已采纳

未定义属性:Illuminate \ Database \ MySqlConnection :: $ name

Hi i'm trying to make a small system filtering in laravel using the following function in my controller to filter users by their roles or their names here's my current code:

profilecontroller.php:

public function membrevis()
{
    $filter = isset($_GET['filter']) ? $_GET['filter'] : null;
    $users = DB::table('users')
        ->join('user_role', 'users.id', '=', 'user_role.user_id')
        ->join('roles', 'user_role.role_id', '=', 'roles.id')
        ->where('users.valid','=',0)
        ->select('users.*','roles.description');
   if ($filter != null) {
        $users->where('users.name','like','%'.$filter.'%')
            ->orWhere('roles.description',' like','%'.$filter.'%');
   }
   $users->get();

   return view('membre2',['users'=> $users]); 
}

My view has an input form where you can type the name or the role of the members you are trying to filter :

membre2.blade.php:

<form action="/profilecontroller/membrevis" method="get">
    <input type="text" name="filter" >
    <button type="submit">filter</button>
</form> 

@foreach($users as $users) 

   <h4 class="media-heading">{{ $users->name }}</h4>

@endforeach

The error i'm getting is Undefined property: Illuminate\Database\MySqlConnection::$name

I have no idea why i'm getting this error i'm obviously passing $users to my view with this line of command:

return view('membre2',['users'=> $users]);

Any help would be appreciated! thank you

  • 写回答

1条回答 默认 最新

  • dongsheng8158 2018-06-08 20:06
    关注

    when using $users->get() it returns Collection instance, you must be assign $users = $users->get(); Corrected code is

        public function membrevis()
        {
            $filter = isset($_GET['filter']) ? $_GET['filter'] : null;
            // also can use this structure $filter = isset($_GET['filter']) ?? null;
            $users = DB::table('users')
                ->join('user_role', 'users.id', '=', 'user_role.user_id')
                ->join('roles', 'user_role.role_id', '=', 'roles.id')
                ->where('users.valid','=',0)
                ->select('users.*','roles.description');
            if ($filter != null) {
                $users->where('users.name','like','%'.$filter.'%')
                    ->orWhere('roles.description',' like','%'.$filter.'%');
            }
            $users = $users->get();
    
            return view('membre2',['users'=> $users]); 
        }
    

    also in blade fix it

    @foreach($users as $user) 
    
       <h4 class="media-heading">{{ $user->name }}</h4>
    
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于温度改变石墨烯介电性能(关键词-介电常数)
  • ¥150 HDMI分路器LT86102 的输出在890MHz频点处EMC超标8DB
  • ¥15 druid(相关搜索:数据库|防火墙)
  • ¥15 大一python作业
  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥60 如何鉴定微信小程序数据被篡改过
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥20 手机截图相片分辨率降低一半