M
M
Maxim2020-11-30 16:03:19
Laravel
Maxim, 2020-11-30 16:03:19

How to save data from another controller in Laravel?

when adding an ad in the store(Request $request) method,

I check for authorization

if(!Auth::check()){
        Cookie::queue('ads_request',json_encode($request->input()), time() + 3600 * 24 * 30 * 365);
        return redirect()->route('login');
      }


if the user is not authorized, then I add the form data to the cookie and send the user to the login page

when authorized, I try to call the store method of the AdsController controller

$ads = new AdsController;
$ads->store(json_decode(Cookie::get('ads_request')));


but throws an error

Type error: Argument 1 passed to App\Http\Controllers\AdsController::store() must be an instance of Illuminate\Http\Request, instance of stdClass given, called in /home/vagrant/projects/Laravel/mosdogs/app/Http/Controllers/Auth/LoginController.php on line 83


tell me where I went wrong or how to do it right

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sarvarov, 2020-12-01
@megakor

Never initialize controller classes, they are not for that.
You need to make a separate class - service and use its method in both controllers. Well, or at least a trait.
Calling controller methods is bad in most cases and should indicate that something is wrong in your application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question