You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
With Laravel 5.6.x, thanks to Laravel discovering systemthe ServiceProvider is automatically added to your Providers
Architecture
The data is represented in an array containing two fields.A "current" field that contains the currently used imageand another "other" field that contains all the imagesthat have been used.
In your config folder, OpenProfile.php and config your Profile table and options:
<?phpreturn [/* |-------------------------------------------------------------------------- | USER ID FIELD |-------------------------------------------------------------------------- | this value is the field that contains the user_id | */"user_id_field" =>'user_id',/* |-------------------------------------------------------------------------- | AVATAR FIELD |-------------------------------------------------------------------------- | this value represent the field that contains avatars json | */"avatars_field" =>"avatars",/* |-------------------------------------------------------------------------- | AVATAR FIELD |-------------------------------------------------------------------------- | this value represent the field that contains covers json | */"covers_field" =>"covers",/* |-------------------------------------------------------------------------- | Default Images domain |-------------------------------------------------------------------------- | The default images api domain | */"default_images_domain" =>"https://source.unsplash.com/",/* |-------------------------------------------------------------------------- | Default Avatar Size |-------------------------------------------------------------------------- | The default avatars size | */"default_avatars_size" =>"400x400",/* |-------------------------------------------------------------------------- | Default Cover Size |-------------------------------------------------------------------------- | The default cover size | */"default_cover_size" =>"1000x800", ];
After theses steps, you can callAvatarManager or CoverManager etc....
‼️⚠️ 💥 The Profile Model must be a user's profile not only The Model Class,that means it must have a user_id fields etc...
Eg:
<?phpclass ProfileControllerextends \App\Http\Controllers\Controller {publicfunctionindex(\Illuminate\Http\Request$request) {/*------------------------------------------------------- | Retrieve User and Profile |-------------------------------------------------------- | retrieve the user and with Relationships get his profile | | */$user =App\User::find($request->get('user_id'));$profile =$user->profile();// Then you can get avatarManager Like this$manager =$profile->avatarManager();// return an instance of Avatar$manager->getAvatar();// return the current Avatar// Or get CoverManager$coverManager =$profile->coverManager();$cover =$coverManager->getCover(); }}
Available Manager
Avatar Manager
Return an Instance of Avatar Class
$profile()->avatarManager();
Cover Manager
Return an Instance of Cover Class
$profile()->coverManager();
Available Methods for each Manager
Current
Return a current image for a Manager
$profile->avatarManager()->current();// return a string
Current In Array format
Return an array for current image
$profile->avatarManager()->currentInArray();// return an array
$profile->avatarManager()->all();// return an array
Get By Id
Return an image with a specific id
$profile->avatarManager()->getById("cc5088ab-0a68-4459-890d-f949cfed235e");// return an array
Get By source
Return a collection of all images that have a specific source
$profile->avatarManager()->getBySrc("https://source.unsplash.com/random/400x400");// return an array
Others
Return all images that have been used before for a Manager
$profile->avatarManager()->others();// return an array
Set
Put an image to database: it take the image link (string)
$profile->avatarManager()->set("http://lorempicsum.com/random/100");// return the current avatar
Set by Id
Put an image that has already been used as an avatar, cover, etc.This can be useful if for example when the user wants to changehis avatar he is shown all the images that had chosenit before and he has the choice to put an old image or a new image
/*------------------------------------------------------- | First image |-------------------------------------------------------- | put the first image to database */$profile->avatarManager()->set("http://lorempicsum.com/random/100");// After that, we retrieve the current image id$id =$profile->avatarManager()->currentInArray()['id'];/*------------------------------------------------------- | AND WE PUT ANOTHER IMAGE |-------------------------------------------------------- | */$profile->avatarManager()->set("http://loremimage.com/80");/*------------------------------------------------------- | PUTTING OLD IMAGE AS THE CURRENT AVATAR |-------------------------------------------------------- | After that, the current avatar is the last record. | with $id above, I can now tell him to put the image corresponding | to this identifier as the current avatar. | This can be useful if for example when the user wants to | change his avatar he is shown all the images that | had chosen it before and he has the choice | to put an old image or a new image */$profile->avatarManager()->setById($id);// return the current avatar
Set A Random Image
You can use it to set a default Image when user is registered.
$profile->avatarManager()->setRandom();// return the current avatar
Change or Update
Changes the value of the collection whose id is passed in the parameters by the remplacement value passed.It takes a third parameter which is the field of interest.if no value is specified then if the replacement value is an array,the entire collection will be replaced, else if the replacement value is a string,only the src attribute will be changedotherwise if it is not a string , nor an array, an exception will be thrown
$profile->avatarManager()->change('cc5088ab-0a68-4459-890d-f949cfed235e','https://source.unsplash.com/random/400x400');// change the image src$profile->avatarManager()->change('cc5088ab-0a68-4459-890d-f949cfed235e',now()->addDay(2)->format('Y-m-d'),'set_at');// change set_at value$profile->avatarManager()->change('cc5088ab-0a68-4459-890d-f949cfed235e','yes','archived');// add a new field to collection
All Methods are in MethodsInterface
<?php/*** @author Aboubacar Ouattara <abouba181@gmail.com>* @license MIT*/namespaceOza\UserImagesManager\Interfaces;interface MethodsInterface{/** * Get the current object * * @return mixed */publicfunctioncurrent():string;/** * @return array */publicfunctioncurrentInArray():array;/** * Get all * * @return array */publicfunctionall():array;/** * Get Others * * @return array * */publicfunctionothers():array;/** * Fill All Array and return an instance * * @return MethodsInterface */publicfunctiongetAll():MethodsInterface;/** * @param string $id * @return array|null */publicfunctiongetById(string$id): ?array;/** * @param string $id * @return array|null */publicfunctiongetBySrc(string$id): ?array;/** * set to profile * * @param string $src * @return string|null */publicfunctionset(string$src):string;/** * Set Random image * * @return string */publicfunctionsetRandom():string;/** * Set by an id * * @param string $id * @return mixed */publicfunctionsetById(string$id): ?string;/** * @param string $id * @return bool */publicfunctionremove(string$id):bool;/** * @return bool */publicfunctionremoveAll():bool;/** * @param string $id * @param null|string $field * @param $value * @return bool */publicfunctionchange(string$id, ?string$field=null ,$value) :bool;}
Add a Custom Manager
Just create a class that extends to Manager Class and implements MethodsInterface
<?phpclass MyCustomManagerextends \Oza\UserImagesManager\Managerimplements \Oza\UserImagesManager\Interfaces\MethodsInterface{use \Oza\UserImagesManager\Traits\Methods;private$field;private$defaultSize;/** * My constructor * */publicfunction__construct() {parent::__construct();$this->field =config("profile.YOUR_FIELD_IN_PROFILE_TABLE") ??'YOUR_FIELD_IN_PROFILE_TABLE';$this->defaultSize =config("profile.YOUR_DEFAULT_IMAGE_SIZE") ??'400x400'; }// Add your methods here}
Then add it to UserImagesManager trait or create a new Trait and use it inside your profile model