paranoid
paranoid

Reputation: 7105

Convert array to Request object in laravel 5

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?

Upvotes: 16

Views: 38794

Answers (5)

jakub_jo
jakub_jo

Reputation: 1634

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);

Upvotes: 1

KShport
KShport

Reputation: 605

Just use

$request = new Illuminate\Http\Request($test_array);

Upvotes: 49

Parvez Rahaman
Parvez Rahaman

Reputation: 4387

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.

Upvotes: 9

Rodrigo Souza
Rodrigo Souza

Reputation: 7332

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);

Upvotes: -2

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

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); 
}

Upvotes: 1

Related Questions