16

I have store method in user controller like this

public function store(Request $request)
{
    User::create($request->all()); 

}

and in another controller I want use this method

 public function test(Request $request)
{
  .....
   app('\App\Http\Controllers\UserController')->store($test_array);
  ...
}

and $test_array is:

{"name":"test","email":"[email protected]"}

and finally show me error this

Argument 1 passed to App\Http\Controllers\UserController::store() must be an instance of Illuminate\Http\Request,

How can I convert array to Request object?

2
  • How do you get $request array before app('\App\Http\Controllers\UserController')->store($request);? Commented May 2, 2016 at 5:28
  • Why is $request an argument to test in the first place? You would be better off using setUp or a data provider Commented Apr 20, 2018 at 8:37

5 Answers 5

50

Just use

$request = new Illuminate\Http\Request($test_array);
Sign up to request clarification or add additional context in comments.

Comments

9

use $request->merge($someArray)

You can try this way..

Your second controller's test method.

public function test(Request $request)
{
  $another_array = [];
  $another_array['eg'] = $test_array;
  $request->merge($another_array);

  .....
   app('\App\Http\Controllers\UserController')->store($request->get('eg'));
  ...
}

Your$test_arraymust be of the type array

Hope this will solve your problem.

Comments

1

I don't think you'll be able to use store() this way.

If you're calling store() action from other places, you could place persisting logic into a model:

class User extends Model

    public function storeData($data)
    {
        $createdUser = User::create($data);
        return $createdUser;
    }

And then call this from store() action and test().

If not just, just change store() to:

public function store($data)
{
    User::create($data); 
}

4 Comments

Thanks @Alexey but I can't add method in model because before that I added any method in controller
I think you didn't understand what I mean. You can put DB related logic inside a model class. Then you'll be able to call it from any controller and any action you want (from test() and from store()). That's the right use of MVC pattern.
I have validation and another thing in store method of user controller and I if I use in model I write this in test controller User::create($request);
That's why Laravel allows you to create custom Request class for validation: laravel.com/docs/5.0/validation#form-request-validation
1

How can I convert array to Request object?

If you really want to do it this way, which I wouldn't, here you're:

Something like this should do the trick -- I did not test it:

use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\ParameterBag;
use Illuminate\Http\Request as IlluminateRequest;


$symfonyRequest = SymfonyRequest::createFromGlobals();

$symfonyRequest->query = new ParameterBag([
    'foo' => 'bar',
]);

$request = IlluminateRequest::createFromBase($symfonyRequest);

Comments

-2

Why don't you use redirect using flashed session data? As the Response - Redirecting With Flashed Session Data documentation says:

Redirecting to a new URL and flashing data to the session are typically done at the same time. So, for convenience, you may create a RedirectResponse instance and flash data to the session in a single method chain. This is particularly convenient for storing status messages after an action:

Route::post('user/profile', function () {
    // Update the user's profile...

    return redirect('dashboard')->with('status', 'Profile updated!');
});

Of course, after the user is redirected to a new page, you may retrieve and display the flashed message from the session. For example, using Blade syntax:

@if (session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
@endif

So in your case you would store return redirect('dashboard')->with('user', $userModel);

1 Comment

You don't understand my problem, I want to use controller method in other place to REST API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.