M
M
Mass1veDit2022-03-09 13:50:06
Laravel
Mass1veDit, 2022-03-09 13:50:06

How to pass variable from 1 function to another in controller?

I can't pass a variable from 1 controller to another,

there is a function to save the user

public function store(UserRequest $request, User $user)
{
$user->fill($request->only($user->getFillable()));
$user['balance'] =  100;
$user['avatar'] = Storage::put('avatar',$user['avatar']);
$user->password = Hash::make($request->password);
$user->save();
 
Notification::send($user,new WelcomeNotification);
 
$this->content = view('Admin::User.verify')
    ->with([
        'title'=>$this->title,
    ])
    ->render();
return $this->renderOutPut();
}


Next comes a redirect to the blade page, where the user enters the verification code using the phone

<div class="form-group row">
    <label for="verification_code"
           class="col-md-4 col-form-label text-md-right">{{ __('Phone Number') }}</label>
    <div class="col-md-6">
 
        <input id="code" type="tel"
               class="form-control @error('verification_code') is-invalid @enderror"
               name="verification_code" value="{{ old('verification_code') }}" required>
        @error('verification_code')
        <span class="invalid-feedback" role="alert">
            <strong>{{$message }}</strong>
        </span>
        @enderror
    </div>
</div>

After in method

public function verify(Request $request)
{
Хочу сделать проверку,если код из бд совпадает с введенным,редиректить,если нет то сообщение об ошибке
if($request->verification_code == $user->code){
 
}
}

But the fact is that I can't get the value from the database of the newly created user into the verify method, the
user is saved in the database with the code,
$user->save();
with dd you can display the value of $user->code in the store function,
I tried to save the variable globally, but nothing comes in the public function verify. Because the user has already been created and it is not possible to display it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2022-03-17
@dlnsk

You must remember that when the verify method is run, it is a completely new run of your application, unrelated to the previous one when the store method was executed. In this regard, remembering anything in memory is useless. Every launch of a Laravel application (and any other server-side script) is a clean slate.
In this regard, if you want to work with some information (for example, a user), you need to get it from somewhere. Thus, if you need a user, then you need the user id and in verify you need to get this user from the database by id. Well, then compare what you want.
This means that you must either pass the id through the form along with the verification_code, or, if it is in the route, simply use the Route Model Binding (as is done in the store). In short, you need to understand where $user came from in the store, and then it will become easier for you, but you can understand this only by reading the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question