check if id exists in table laravel code example

Example 1: laravel check record exists

$user = User::where('email', '=', Input::get('email'))->first();
if ($user === null) {
   // user doesn't exist
}

Example 2: laravel seeder check if table has data

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UserSeeder extends Seeder
{
  public function run()
  {
    // check if table is empty before seeding data
    if(DB::table('users')->count() == 0) {
      DB::table('users')->insert([
        [
          'first_name' => 'John',
          'last_name' => 'Doe'
        ]
      ]);
    }
  }
}

Tags:

Php Example