Laravel - 如何检查数据是否存在?

How to check if data exist, if it not exist then redirect to different page.

It keep saying data is exist even it does not exist.

In the model file, I have:

public static function getPackage($id)
{
    return Packages::where('id', '=', $id);
}

In the controller:

protected function checkPackage($id) 
{
    if (!Packages::getPackage($id)->get()) {
        Redirect::to('/')
    } 
}

Model

public static function getPackage($id)
{
$something = Packages::where('id', '=', $id)->get();
return $something;
}

in controller

protected function checkPackage($id) 
{
$variable = getPackage();
if (!$variable ) 
{
    Redirect::to('/')
} 
}

You can use isEmpty() on the collection.

$package = Package::getPackage($id);

if ($package->isEmpty(){
    // Do stuff.
}

With Eloquent perhaps you could use something like:

if(Packages::where('id', '=', $id)->count() > 0) {
    //do something
} else {
    // redirect
}