laravel eloquent get() ? code example

Example 1: show user only those product which he hasn't buyed laravel eloquest

$user_id = auth()->user()->id;

Product::where('status', 'active')
  ->whereNotIn('id', function($query) use ($user_id) {
    $query->select('product_id')->from(new OrderProduct->getTable())
      ->where('user_id', $user_id)->where('status', 'delivered')
		->pluck('product_id')->toArray();
  });

Example 2: laravel firstorcreate

// Retrieve flight by name, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);

// Retrieve flight by name, or create it with the name, delayed, and arrival_time attributes...
$flight = App\Flight::firstOrCreate(
    ['name' => 'Flight 10'],
    ['delayed' => 1, 'arrival_time' => '11:30']
);

// Retrieve by name, or instantiate...
$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);

// Retrieve by name, or instantiate with the name, delayed, and arrival_time attributes...
$flight = App\Flight::firstOrNew(
    ['name' => 'Flight 10'],
    ['delayed' => 1, 'arrival_time' => '11:30']
);