php Laravel -无法将图片加载到数据库

jm2pwxwz  于 2022-12-25  发布在  PHP
关注(0)|答案(1)|浏览(109)

我为用户上传了一张个人资料图片,并试图复制代码,为用户在链接上的自定义图标做同样的事情。当上传图片的名称需要在数据库中更新时,问题就出现了。
这是我从UserController复制的代码:

public function update_avatar(Request $request){

        if($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->fit(300, 300)->save( public_path('/uploads/avatars/' . $filename ) );

            $user = Auth::user();
            $user->avatar = $filename;
            $user->save();
        }

        return redirect()->back()
            ->with(['success' => 'Profile picture updated!']);

    }

下面是我正在尝试的代码,但在LinkController中失败:

public function custom_icon(Request $request){

        if($request->hasFile('icon')){
            $icon = $request->file('icon');
            $filename = time() . '.' . $icon->getClientOriginalExtension();
            Image::make($icon)->fit(300, 300)->save( public_path('/img/icons/' . $filename ) );

            $icon->icon = $filename;
            $icon->save();
        }

        return redirect()->back()
            ->with(['success' => 'Custom icon uploaded!']);

    }

这是我的web.php的一部分

Route::group(['middleware' => 'auth', 'prefix' => 'dashboard', ], function() {

    Route::get('/links', [LinkController::class, 'index']);
    Route::get('/links/new', [LinkController::class, 'create'])->middleware('verified');
    Route::post('/links/new', [LinkController::class, 'store']);
    Route::get('/links/{link}', [LinkController::class, 'edit']);
    Route::post('/links/{link}', [LinkController::class, 'update']);
    Route::delete('/links/{link}', [LinkController::class, 'destroy']);
    Route::post('/links/{link}', [LinkController::class, 'custom_icon']);

    Route::get('/qrcode', [UserController::class, 'qrcode']);

    Route::get('/settings', [UserController::class, 'settings']);
    Route::get('/settings/edit', [UserController::class, 'edit']);
    Route::get('/settings/profile', [UserController::class, 'profile']);
    Route::get('/settings/qr', [UserController::class, 'qr']);
    Route::post('/settings/edit', [UserController::class, 'update']);
    Route::post('/settings/profile', [UserController::class, 'update_avatar']);
    Route::post('/settings/qr', [UserController::class, 'qr']);

    Route::get('/settings/vcard', [UserController::class, 'vcard']);
    Route::get('/settings/vcard/name', [UserController::class, 'vcard_name']);
    Route::get('/settings/vcard/contact', [UserController::class, 'vcard_contact']);
    Route::get('/settings/vcard/info', [UserController::class, 'vcard_info']);

    Route::get('/settings/help', [UserController::class, 'help']);
    Route::get('/settings/help/devices', [UserController::class, 'devices']);
    Route::get('/settings/help/account', [UserController::class, 'account']);
    Route::get('/settings/help/terms', [UserController::class, 'terms']);

    Route::get('/settings/help/contact', [ContactController::class, 'contact']);
    Route::post('/settings/help/contact', [ContactController::class, 'send_email']);

});

任何帮助都将不胜感激。

bjp0bcyl

bjp0bcyl1#

您可以直接更新用户表,以便通过直接使用雄辩软件更新用户表

user::where('id',Auth::id())->update(['avatar'=>$filename])

相关问题