Powerful Unsplash package for Laravel

Overview

Powerful Unsplash package for Laravel

Provides a fluent api to use the Unsplash within Larvel applications. Use public actions or store images directly in your storage and persists all copyright informations automatically with the databse connector.

Latest Version on Packagist Build Status Quality Score Total Downloads

Install

$ composer require marksitko/laravel-unsplash

Laravel-Unsplash comes with package discovery and Laravel will register the service provider and facade automatically. Just in case you wanna add it manually, you should provide it in config/app.php

Service provider

'providers' => [
    //...
    MarkSitko\LaravelUnsplash\UnsplashServiceProvider::class,
];

Facade

'aliases' => [
    //...
    'Unsplash' => MarkSitko\LaravelUnsplash\UnsplashFacade::class,
];

Next you should publish the configuration.

$ php artisan vendor:publish --tag=config
Optional

If you wanna use Laravel-Unsplash with the database connector you have to publish the migration files.

$ php artisan vendor:publish --tag=migrations

It creates 2 migrations. One to store additional informations about stored image and one morph table to use it with the HasUnsplashables trait.

Configuration

You have to provide a unsplash api access key in your .env file. Read how to generate a Unsplash API key

UNSPLASH_ACCESS_KEY=YOUR_GENERATED_API_KEY_FROM_UNSPLASH

Optional configurations:

# default is false
UNSPLASH_STORE_IN_DATABASE=BOOLEAN

# default is local
UNSPLASH_STORAGE_DISK=YOUR_STORAGE_DISC

Basic Usage

Take a look at the full Unsplash API documentation https://unsplash.com/documentation

Random Photos

// Returns the http response body.
$twoRandomPhotosOfSomePeoples = Unsplash::randomPhoto()
    ->orientation('portrait')
    ->term('people')
    ->count(2)
    ->toJson();

// Store the image in on your provided disc
$theNameFromTheStoredPhoto = Unsplash::randomPhoto()
    ->orientation('landscape')
    ->term('music')
    ->randomPhoto()
    ->store();
];

Photos

$photos = Unsplash::photos()->toJson();
$photo = Unsplash::photo($id)->toJson();
$photosStatistics = Unsplash::photosStatistics($id)->toJson();
$trackPhotoDownload = Unsplash::trackPhotoDownload($id)->toJson();

Users

$user = Unsplash::user($username)->toJson();
$userPortfolio = Unsplash::userPortfolio($username)->toJson();
$userPhotos = Unsplash::userPhotos($username)->toJson();
$userLikes = Unsplash::userLikes($username)->toJson();
$userCollections = Unsplash::userCollections($username)->toJson();
$userStatistics = Unsplash::userStatistics($username)->toJson();

Search

$search = Unsplash::search()
    ->term('buildings')
    ->color('black_and_white')
    ->orientation('squarish')
    ->toJson();

$searchCollections = Unsplash::searchCollections()
    ->query('events')
    ->page($pageNumber)
    ->toJson();

$searchUsers = Unsplash::searchUsers()
    ->query('search_term')
    ->toJson();

Collections

$collectionsList = Unsplash::collectionsList()
    ->page($pageNumber)
    ->perPage($itemsPerPage)
    ->toJson();

$featuredCollection = Unsplash::featuredCollection()
    ->page($pageNumber)
    ->perPage($itemsPerPage)
    ->toJson();

$showCollection = Unsplash::showCollection()
    ->id($collectionId)
    ->toJson();

$showCollectionPhotos = Unsplash::showCollectionPhotos()
    ->id($collectionId)
    ->toJson();

$showCollectionRelatedCollections = Unsplash::showCollectionRelatedCollections()
    ->id($collectionId)
    ->toJson();

Stats

$totalStats = Unsplash::totalStats()->toJson();
$monthlyStats = Unsplash::monthlyStats()->toJson();

Usage with Database

If you wanna persist automaticly some informations (i.e. Copyrights) about you stored images you have to run the published migrations. In case you dont have ran the optional command, we start at the beginning:

$ php artisan vendor:publish --tag=migrations
$ php artisan migrate

When migration is successfull, you have to adjust the .env

UNSPLASH_STORE_IN_DATABASE=true

Now when you execute store() on the Unsplash client, the image is stored in your provided disc and informations like

  • the unsplash photo id
  • the stored image name
  • author name
  • author link

However, these informations are all required to use a unsplash photo on your website.

Example with Unsplash Client

// Returns the created unsplash asset record
$databaseRecord = Unsplash::randomPhoto()->store();

You are now also able to use the build in UnsplashAsset Model Example with UnsplashAsset Model

// Returns the created unsplash asset record
$databaseRecord = UnsplashAsset::api()->randomPhoto()->store();

// Get an stored unsplash asset
$unsplashAsset = UnsplashAsset::find($id);

You can also use the HasUnsplashables Trait on any model.

Example with HasUnsplashables Trait on the User Model

use Illuminate\Foundation\Auth\User as Authenticatable;
use MarkSitko\LaravelUnsplash\Traits\HasUnsplashables;

class User extends Authenticatable
{
    use HasUnsplashables;

    // ...
}

Now you are able to use it like:

// store the unsplash asset in a morphToMany relation
$unsplashAsset = Unsplash::randomPhoto()->store();
User::unsplash()->save($unsplashAsset);

// retrive all related unsplash assets
User::find($userId)->unsplash();

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

You might also like...
A Laravel chat package. You can use this package to create a chat/messaging Laravel application.
A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

Chat Create a Chat application for your multiple Models Table of Contents Click to expand Introduction Installation Usage Adding the ability to partic

This package provides extended support for our spatie/enum package in Laravel.
This package provides extended support for our spatie/enum package in Laravel.

Laravel support for spatie/enum This package provides extended support for our spatie/enum package in Laravel. Installation You can install the packag

Testbench Component is the de-facto package that has been designed to help you write tests for your Laravel package

Laravel Testing Helper for Packages Development Testbench Component is the de-facto package that has been designed to help you write tests for your La

🥳🔐 This package is a Laravel package that checks if an email address is a spammer
🥳🔐 This package is a Laravel package that checks if an email address is a spammer

This package is a Laravel package that checks if an email address is a spammer. It verifies your signups and form submissions to confirm that they are legitimate.

GeoLocation-Package - This package helps you to know the current language of the user, the country from which he is browsing, the currency of his country, and also whether he is using it vpn
GeoLocation-Package - This package helps you to know the current language of the user, the country from which he is browsing, the currency of his country, and also whether he is using it vpn

GeoLocation in PHP (API) 😍 😍 😍 This package helps you to know a lot of information about the current user by his ip address 😍 😍 😍 This package h

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic! concepts like , Hijri Dates & Arabic strings and so on ..

Comments
  • Facade is not working on Laravel 8

    Facade is not working on Laravel 8

    Hi @marksitko, thanks for this package.

    I am trying to use but I am getting non-static method MarkSitko\\LaravelUnsplash\\Unsplash::search() should not be called statically

    If I use \Unsplash::, it throws this error: Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given, called in /var/www/kapa99/vendor/laravel/framework/src/Illuminate/Http/Response.php on line 72

    I am using in an API endpoint. If I try it on tinker it works but no in the controller.

    Is there any configuration I am missing to get this package working? Or what is the correct import to use the library?

    Regards

    opened by LTroya 8
  • Unsplash->store returns a String instead of the Model when storing in database

    Unsplash->store returns a String instead of the Model when storing in database

    PHP v8.1

    When using the following code from the Readme:

    // Returns the created unsplash asset record
    $databaseRecord = UnsplashAsset::api()->randomPhoto()->store();
    

    The $databaseRecord object will be a string representation of the model (via toString()) rather than the model itself. I believe this is because Unsplash->store is defined as returning a string, rather than either a string or a Model instance.

    opened by kjlloydie 3
  • Support for Laravel 9

    Support for Laravel 9

    Hi. I ran into the following errors while trying to upgrade to Laravel 9

    Your requirements could not be resolved to an installable set of packages.
    
      Problem 1
        - marksitko/laravel-unsplash 1.1.0 requires php ^7.3 -> your php version (8.0.11) does not satisfy that requirement.
        - marksitko/laravel-unsplash[1.1.1, ..., 1.1.2] require illuminate/support ^6.0|^7.0|^8.0 -> found illuminate/support[v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.3] but it conflicts with another require.
        - Root composer.json requires marksitko/laravel-unsplash ^1.1 -> satisfiable by marksitko/laravel-unsplash[1.1.0, 1.1.1, 1.1.2].
    
    
    Installation failed, reverting ./composer.json and ./composer.lock to their original content.
    

    I'd like to help make this package compatible with laravel 9 but am not entirely familiar with the dependency chain here.

    opened by pkid169 2
  • Remove incorrect PHPDoc @return tags, so IDEs can provide autocompletion

    Remove incorrect PHPDoc @return tags, so IDEs can provide autocompletion

    In PhpStorm, autocompletion of methods is currently broken for this package. For example, Unsplash::search()->term('test')->toJson() stops all autocompletion after the search() method.

    PhpStorm does not recognise the chained term() or toJson() methods because the traits all set self-referential @return docblock tags:

         /**
          * Search photos
          * Get a single page of photo results for a query.
          * @link https://unsplash.com/documentation#search-photos
          *
          * @return MarkSitko\LaravelUnsplash\Endpoints\Search
          */
         public function search()
         {
            $this->apiCall = [
                'endpoint' => 'search/photos',
            ];
            return $this;
        }
    

    Because of this @return tag, PhpStorm assumes return $this; is returning another instance of the Search trait — when it's actually returning the original instance of \MarkSitko\LaravelUnsplash\Unsplash (via inheritance of the UnsplashAPI trait).

    Removing these @return tags lets PhpStorm parse the class inheritance correctly, and provide full autocompletion.

    Thanks for a great package!

    opened by JackWH 2
Releases(2.0.0)
  • 2.0.0(Mar 8, 2022)

    • Add Laravel 9 Support
    • Remove Support for Laravel 6 and 7
    • Move Facade in his own Directory/Namespace

    Full Changelog: https://github.com/marksitko/laravel-unsplash/compare/1.1.1...2.0.0

    Source code(tar.gz)
    Source code(zip)
Owner
Full-Stack Web Developer 🚀
null
A powerful form builder, for Laravel and other frameworks (stand-alone too)

Former A Laravelish way to create and format forms Former outputs form elements in HTML compatible with your favorite CSS framework (Bootstrap and Fou

null 1.3k Dec 22, 2022
Provides a powerful error response system for Laravel

Laravel Exceptions Laravel Exceptions was created by, and is maintained by Graham Campbell, and provides a powerful error response system for both dev

Graham Campbell 571 Jan 31, 2022
🍪 Powerful wrapper for improved cookie integration on Laravel

?? Powerful wrapper for improved cookie integration on Laravel

Attla 1 Jul 16, 2022
The fastest way to make a powerful JSON:API compatible Rest API with Laravel.

The first fully customizable Laravel JSON:API builder. "CRUD" and protect your resources with 0 (zero) extra line of code. Installation You can instal

BinarCode 288 Aug 8, 2022
🔔 Flasher is a powerful and flexible flash notification system for PHP, Laravel, Symfony

A powerful and flexible flash notifications system for PHP, Laravel, Symfony ?? PHP Flasher helps you to add flash notifications to your PHP projects.

PHP Flasher 158 Jan 4, 2023
Laravel is accessible, powerful, and provides tools required for large, robust applications.

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be trul

Web & Mobile | eCommerce | Full-Stack Developer 4 Nov 24, 2022
Extend Kirby’s templates with a powerful layout system

Kirby Layouts plugin This plugin extends Kirby’s templates with a powerful layout system. Installation Download Download and copy this repository to /

Kirby 3 39 Dec 28, 2022
Blade is a simple, yet powerful templating engine provided for the Slim Framework

slim-blade Blade is the default template engine of Laravel. The main advantage of Blade is template inheritance whilst using plain PHP. This package a

Kevin Darren 32 May 24, 2021
HydePHP - Elegant and Powerful Static App Builder

HydePHP - Elegant and Powerful Static App Builder Make static websites, blogs, and documentation pages with the tools you already know and love. About

HydePHP 31 Dec 29, 2022
Jetstrap is a lightweight laravel 8 package that focuses on the VIEW side of Jetstream / Breeze package installed in your Laravel application

A Laravel 8 package to easily switch TailwindCSS resources generated by Laravel Jetstream and Breeze to Bootstrap 4.

null 686 Dec 28, 2022