I’m trying to send a set of form inputs from a controller into an API endpoint but when I check the variables in the API, it all shows null value.. what is wrong with my code?
CulinaryController.php
public function update(Request $request, Culinary $culinary)
{
$client = new Client();
$token = Session('token');
try {
$multipart = [];
foreach ($request->except('cover_photo') as $key => $value) {
$multipart[] = [
'name' => $key,
'contents' => $value,
];
}
if ($request->hasFile('cover_photo')) {
$coverPhoto = $request->file('cover_photo');
$multipart[] = [
'name' => 'cover_photo',
'contents' => fopen($coverPhoto->getRealPath(), 'r'),
'filename' => $coverPhoto->getClientOriginalName(),
];
}
dd($multipart);
$url = 'http://localhost/api/v1/culinary/'.$culinary->id;
$response = $client->put($url, [
'multipart' => $multipart,
'headers' => [
'Authorization' => 'Bearer '.$token,
'Accept' => 'application/json',
]
]);
} catch (Exception $e) {
return redirect()->back()->withErrors(['errors' => $e->getMessage()])->withInput();
}
}
api/v1/CulinaryController.php
public function update(Request $request, Culinary $culinary)
{
return response()->json([
'status' => 'test',
'request' => $request->all(),
'foo' => request('title'),
'file' => $request->file('cover_photo'),
'culinary' => $culinary,
]);
}
routes/api.php
Route::apiResource('culinary', CulinaryController::class);
when I uncomment the dd($multipart)
in the controller, it contains 7 sets of input below so I’m sure the form wasn’t empty when submitted.
array:9 [▼ // appHttpControllersAdminCulinaryController.php:198
0 => array:2 [▼
"name" => "_token"
"contents" => "TwpS0e7OJH1jhhR1dr8RBpCiYKW4vhVyuk3CRZWC"
]
1 => array:2 [▼
"name" => "_method"
"contents" => "PUT"
]
2 => array:2 [▼
"name" => "title"
"contents" => "Et in quia ut et illo harum."
]
3 => array:2 [▼
"name" => "old_cover_photo"
"contents" => "https://via.placeholder.com/640x480.png/0022bb?text=distinctio"
]
4 => array:2 [▼
"name" => "country_id"
"contents" => "188"
]
5 => array:2 [▼
"name" => "category_name"
"contents" => "vegetarian"
]
6 => array:2 [▼
"name" => "prologue"
"contents" => "Adipisci iure nihil sit voluptatem rerum voluptatibus. Qui aperiam et magnam corrupti quia eius reprehenderit. Sunt non aut dolores. Dolorem nesciunt atque ipsa
▶
"
]
7 => array:2 [▼
"name" => "body"
"contents" => "Autem iure qui magni excepturi hic. Laudantium adipisci saepe odio quod cupiditate. Blanditiis occaecati sint at sint incidunt optio illum nihil."
]
8 => array:2 [▼
"name" => "opening_hours"
"contents" => "Qui et magnam labore a ea corrupti."
]
]
2
Answers
I studied your question . you can use a below package for a solution your problem :
In php we have multipart/form-data in POST method request but you have use PUT method.i have a suggest for you. please test
$request->all()
at the beginning of your function (update) and check type of ‘cover-photo’ parameter. if you recieve array type its ok but you should use a package for config multipart/form-data on your PUT Request.good luck
May be update content-type and try