Home > Software engineering >  Laravel: How to change return value of controllermethod in vendor/laravel/jetstream without editing
Laravel: How to change return value of controllermethod in vendor/laravel/jetstream without editing

Time:01-01

I want to edit what a controllermethod returns, but don't want to edit the vendor file directly because it can undo my changes obviously. I'm not sure how to make these edits happen though:

I want to change this file: vendor/laravel/jetstream/src/Http/Controllers/Inertia/UserProfileController.php

From this:

public function show(Request $request)
{
    $this->validateTwoFactorAuthenticationState($request);

    return Jetstream::inertia()->render($request, 'Profile/Show', [
        'confirmsTwoFactorAuthentication' => Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm'),
        'sessions' => $this->sessions($request)->all(),
    ]);
}

To this:

public function show(Request $request)
{
    $this->validateTwoFactorAuthenticationState($request);

    return Jetstream::inertia()->render($request, 'Profile/Show', [
        'confirmsTwoFactorAuthentication' => Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm'),
        'sessions' => $this->sessions($request)->all(),
        'video' => $request->user()->video <-- added this line
    ]);
}

CodePudding user response:

create a new controller that extends the one you need to override. then use your controller instead

<?php

namespace App\myController\Path;

use namespace\of\original\controller;

class CustomUserProfileController extends UserProfileController
{
    public function show(Request $request)
    {
        $this->validateTwoFactorAuthenticationState($request);

        return Jetstream::inertia()->render($request, 'Profile/Show', [
            'confirmsTwoFactorAuthentication' => Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm'),
            'sessions' => $this->sessions($request)->all(),
            'video' => $request->user()->video <-- added this line
        ]);
    }
}

now you can use your own controller in any place you want

  • Related