简体   繁体   English

如何在 this.$page.props.user 中使用惯性js设置上传的头像?

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

In Laravel 8 app with inertiajs/inertia-vue 0.7/vuejs2 with fortify (but without jetstream)在 Laravel 8 应用程序中,带有惯性js/惯性-vue 0.7/vuejs2,带有强化(但没有喷射流)

I use "spatie/laravel-medialibrary": "^9.9" for avatar uploading like我使用 "spatie/laravel-medialibrary": "^9.9" 来上传头像

$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?在 vue 组件属性中检查哪个用户已登录并显示他的信息?

Thanks!谢谢!

You can do this with the 'Shared data' feature via the HandleInertiaRequests middleware .您可以通过HandleInertiaRequests中间件使用“共享数据”功能来做到这一点。

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 .在客户端,您可以使用this.$page.props.user.avatar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM