Giter Site home page Giter Site logo

laravel-unsplash's Introduction

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\Facades\Unsplash::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();

Topics

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

$showTopic = Unsplash::showTopic()
    ->id($topicIdOrSlug)
    ->toJson();

$showTopicPhotos = Unsplash::showTopicPhotos()
    ->id($topicIdOrSlug)
    ->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.

laravel-unsplash's People

Contributors

hopkins385 avatar marksitko avatar oleynikd avatar razisayyed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-unsplash's Issues

Showing Client error if results is empty

If empty results then showing this Client error:
message: "Client error: GET https://api.unsplash.com/photos/random?orientation=landscape&query=xdgdsgdsgdgdgd&count=6 resulted in a 404 Not Found response:\n{"errors":["No photos found."]}\n"

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

Laravel 10 support

Hi,

when trying to install this package into a fresh Laravel 10 project, I get the following errors:

  Problem 1
    - marksitko/laravel-unsplash 1.0.0 requires php ^7.2 -> your php version (8.2.2) does not satisfy that requirement.
    - marksitko/laravel-unsplash 1.1.0 requires php ^7.3 -> your php version (8.2.2) 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.27] but these were not loaded, likely because it conflicts with another require.
    - marksitko/laravel-unsplash[2.0.0, ..., 2.0.1] require illuminate/support ^8.0|^9.0 -> found illuminate/support[v8.0.0, ..., v8.83.27, v9.0.0, ..., v9.52.0] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires marksitko/laravel-unsplash * -> satisfiable by marksitko/laravel-unsplash[1.0.0, 1.1.0, 1.1.1, 1.1.2, 2.0.0, 2.0.1].

Composer require fail to install on laravel 11

I got this error when installing this package in a new laravel project. I think the composer package is not updated on packagist

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - marksitko/laravel-unsplash[2.0.0, ..., 2.0.1] require illuminate/support ^8.0|^9.0 -> found illuminate/support[v8.0.0, ..., v8.83.27, v9.0.0, ..., v9.52.16] but these were not loaded, likely because it conflicts with another require.
    - marksitko/laravel-unsplash[2.1.0, ..., 2.2.0] require illuminate/support ^8.0|^9.0|^10.0 -> found illuminate/support[v8.0.0, ..., v8.83.27, v9.0.0, ..., v9.52.16, v10.0.0, ..., v10.48.12] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires marksitko/laravel-unsplash ^2 -> satisfiable by marksitko/laravel-unsplash[2.0.0, 2.0.1, 2.1.0, 2.2.0].


Installation failed, reverting ./composer.json and ./composer.lock to their original content.

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.

trackPhotoDownload not working?

In my code, I'm calling:

Unsplash::trackPhotoDownload($id);

But in my Unsplash dashboard, the download count is not incrementing. Is this endpoint still working correctly?

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.