Example of FirstOrCreate in aschmelyun/laravel-job-board

File app/Http/Controllers/ListingController.php (link to Github)
use App\Models\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class ListingController extends Controller
{
    //
    public function store(Request $request)
    {
            //
            foreach(explode(',', $request->tags) as $requestTag) {
                $tag = Tag::firstOrCreate([
                    'slug' => Str::slug(trim($requestTag))
                ], [
                    'name' => ucwords(trim($requestTag))
                ]);

                $tag->listings()->attach($listing->id);
            }
            //
    }
}

Additional resources on firstOrCreate: