Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

learner2016's avatar

How do I Pass firstname input and lastname input to a View Composer?

Blade View

{!! Form::open(['url' => route('sendData'),'method'=>'post']) !!}

{!! Form::label('firstname','First Name') !!} {!! Form::input('firstname', 'firstname') !!}

{!! Form::label('lastname','Last Name') !!} {!! Form::input('lastname', 'lastname', null) !!}

{!! Form::submit('Submit') !!}

{!! Form::close() !!}

0 likes
7 replies
learner2016's avatar

I don't know if it involves view composer or not, but I need to pass inputs from a view to another view after authentication of a user. My problem is that whenever the input goes from view A to login or registration to view B, somehow the input does not get passed from view A to view B. I get an error of undefined variable in view B. But without authentication everything works fine. So, I was looking for a way to share my input to all views to avoid the issues I'm having due to authentication. Thank you.

learner2016's avatar

I figured out how to share my inputs from a view to all views using the Laravel share method. All I did was to post my form inputs from my blade view. And then I passed those inputs into the AppServiceprovider boot method ( app\providers\AppServiceProvider). Below is my code that I used in the appserviceprovider.

 public function boot(Request $request)
    {
        Schema::defaultStringLength(191);
    
        $wordcount = $request['wordcount'];
        $label = $request['label'];
        $currency = $request['currency'];
        $symbol = $request['symbol'];
        $result = $request['result'];
        $tax = $request['tax'];
        $total = $request['total'];
        $state = $request['symbol'];
        $country = $request['selCountry'];
        $amount = $request['currency'];
        
        View::share('wordcount', $wordcount);
        View::share('label', $label);
        View::share('currency', $currency);
        View::share('symbol', $symbol);
        View::share('result', $result);
        View::share('tax', $tax);
        View::share('total', $total);
        View::share('state', $state);
        View::share('country', $country);
        View::share('amount', $amount);
    } 

The final step is going to any view and using the variables (e.g. $wordcount).

I hope this may help someone in a similar situation.

Snapey's avatar

Easy enough but ... why on earth???

learner2016's avatar

I have several views that I use this variables on. This saves me from repeating the codes in controllers. Besides, it solves my problem of having undefined variables after a user is forced to login or register, to access a protected view. Please, you are welcomed to point me to another way to resolve my issues. However, this solves all my problems. Thanks anyway, even though you never gave me direct solutions, each time you responded to my question, I end up finding ways to resolve my own issues. Thanks again for responding.

Snapey's avatar

What I don't understand is why you are getting this data out of the request object? How did it get there?

jlrdw's avatar

If you want to display something else in a view, why not just put it in a division with ajax. Or even another section.

Please or to participate in this conversation.