PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 2, 2022

[FIXED] How set uploaded avatar in this.$page.props.user with inertiajs?

 January 02, 2022     fortify, inertiajs, laravel     No comments   

Issue

In Laravel 8 app with inertiajs/inertia-vue 0.7/vuejs2 with fortify (but without jetstream)

I use "spatie/laravel-medialibrary": "^9.9" for avatar uploading like

$loggedUser = auth()->user();
$avatar_file_path = $avatarImageUploadedFile->getPathName();
$loggedUser->addMedia( $avatar_file_path )->toMediaCollection('avatar');

and it works, but in which way can I use avatar in on client part when I use

this.$page.props.user

in vue components properties to check which user is logged and show his info?

Thanks!


Solution

You can do this with the 'Shared data' feature via the HandleInertiaRequests middleware.

For example, to share user info you can do the following:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class HandleInertiaRequests extends Middleware
{
    public function share(Request $request)
    {
        return array_merge(parent::share($request), [
            'user' => function () {
                return Auth::user() ? [
                    'id' => Auth::user()->id,
                    'name' => Auth::user()->name,
                    'email' => Auth::user()->email,  
                    // get path to avatar
                    'avatar' => Storage::url('avatar-of-the-user.jpg'),
                ] : null;
            },
        ]);
    }
}

Client side you can then access the avatar's URL with this.$page.props.user.avatar.



Answered By - Sidney Gijzen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing