我刚来拉拉维尔。如何查找是否存在记录?

$user = User::where('email', '=', Input::get('email'));

我能做什么来查看$user是否有记录?


当前回答

if (User::where('email', Input::get('email'))->exists()) {
    // exists
}

其他回答

如果你想在数据库中插入一条记录,而相同邮件的记录不存在,那么你可以这样做:

$user = User::updateOrCreate(
    ['email' => Input::get('email')],
    ['first_name' => 'Test', 'last_name' => 'Test']
);

updateOrCreate方法的第一个参数列出了唯一标识关联表中的记录的列,而第二个参数由要插入或更新的值组成。

你可以在这里查看文档:Laravel upserts doc

if($user->isEmpty()){
    // has no records
}

Eloquent使用集合。 参见以下链接:https://laravel.com/docs/5.4/eloquent-collections

要知道是否有记录是很简单的

$user = User::where('email', '=', Input::get('email'))->get();
if(count($user) > 0)
{
echo "There is data";
}
else
echo "No data";

Laravel 6或顶部:写表名,然后给出where子句条件,例如where('id', $request->id)

 public function store(Request $request)
    {

        $target = DB:: table('categories')
                ->where('title', $request->name)
                ->get()->first();
        if ($target === null) { // do what ever you need to do
            $cat = new Category();
            $cat->title = $request->input('name');
            $cat->parent_id = $request->input('parent_id');
            $cat->user_id=auth()->user()->id;
            $cat->save();
            return redirect(route('cats.app'))->with('success', 'App created successfully.');

        }else{ // match found 
            return redirect(route('cats.app'))->with('error', 'App already exists.');
        }

    }
$email = User::find($request->email);
If($email->count()>0)
<h1>Email exist, please make new email address</h1>
endif