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?
Just use
$request = new Illuminate\Http\Request($test_array);
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);
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_array
must be of the type array
Hope this will solve your problem.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With