如何从Laravel中的表中搜索值

时间:2015-02-10 10:28:08

标签: php validation laravel

我正在尝试从数据库表中搜索值并使用laravel将结果显示到网站。我的controller.php代码在这里

else
      { 
       try
       {
          $pname = Input::get('pname');
          $parents = ForumParent::where('pname', $pname)->first();           
         if(empty($parents))
          {
            throw new  \Exception("Parent not found");}           
            return Redirect::route('view',$parents->paddress);     
          }
       catch (Exception $e)
          { return "not value";        
             //abort(404);
          } 
        }
      }
public function view($paddress)
{  
   $parents=ForumParent::find($paddress);
   $users=User::all();
   return View::make('search.viewsearch')
   ->with('parents',$parents)
   ->with('users',$users);
}

1 个答案:

答案 0 :(得分:0)

改变这个:

$parents = ForumParent::where('pname', $pname)->first();

到此:

$parents = ForumParent::where('pname', '=', $pname)->first();

$parents = ForumParent::where('pname','LIKE', '%' . $pname . '%')->first();

看,如果有帮助。