如何使用 laravel 和 Cloudinary 上传多张图片?
实际上我正在做一个 Laravel 和 ReactJs 项目。我想用 laravel 和 Cloudinary 上传多张图片(相册),但我没有找到正确的解决方案。
这是我的数据库迁移文件:
public function up()
{
Schema::create('unites', function (Blueprint $table) {
$table->increments('unit_id');
$table->unsignedInteger('building_id');
$table->foreign('building_id')->references('building_id')->on('buildings');
$table->unsignedInteger('floor_id');
$table->foreign('floor_id')->references('floor_id')->on('floors');
$table->string('unit_name',200);
$table->tinyinteger('unit_status');
$table->integer('unit_roomnumber');
$table->string('unit_type',5);
$table->string('unit_pictures');
$table->date('unit_added_date')->format("yyyy-MM-dd");
$table->timestamps();
});
}
这是我在控制器中的功能:
else {
$unit = new Unite();
$unit ->unit_name = $request->input('unit_name');
$unit->building_id = $request->input('building_id');
$unit->floor_id = $request->input('building_id');
$unit->unit_type = $request->input('unit_type');
$unit->unit_status = $request->input('unit_status');
$unit->unit_roomnumber = $request->input('unit_roomnumber');
$unit->unit_added_date = $request->input('unit_added_date');
$images = $request->file('unit_pictures');
$uploaded = [];
foreach ($images as $image) {
error_log('for statement fires.');
Cloudinary::upload($image, null, [
'folder' => '/units',
// 'discard_original_filename' => true,
])->getSecurePath();
return array($uploaded);
}
$unit->unit_pictures=$request->file('unit_pictures');
$unit->save();
return response()->json([
'status' => 200,
]);
如果你们中的任何人可以帮助我,我将非常感激
Actually I'm on a laravel and reactJs project. I want to upload multiple pictures (album) with laravel and Cloudinary but I didn't find the right solution.
This my db migration file:
public function up()
{
Schema::create('unites', function (Blueprint $table) {
$table->increments('unit_id');
$table->unsignedInteger('building_id');
$table->foreign('building_id')->references('building_id')->on('buildings');
$table->unsignedInteger('floor_id');
$table->foreign('floor_id')->references('floor_id')->on('floors');
$table->string('unit_name',200);
$table->tinyinteger('unit_status');
$table->integer('unit_roomnumber');
$table->string('unit_type',5);
$table->string('unit_pictures');
$table->date('unit_added_date')->format("yyyy-MM-dd");
$table->timestamps();
});
}
An this My function in the controller :
else {
$unit = new Unite();
$unit ->unit_name = $request->input('unit_name');
$unit->building_id = $request->input('building_id');
$unit->floor_id = $request->input('building_id');
$unit->unit_type = $request->input('unit_type');
$unit->unit_status = $request->input('unit_status');
$unit->unit_roomnumber = $request->input('unit_roomnumber');
$unit->unit_added_date = $request->input('unit_added_date');
$images = $request->file('unit_pictures');
$uploaded = [];
foreach ($images as $image) {
error_log('for statement fires.');
Cloudinary::upload($image, null, [
'folder' => '/units',
// 'discard_original_filename' => true,
])->getSecurePath();
return array($uploaded);
}
$unit->unit_pictures=$request->file('unit_pictures');
$unit->save();
return response()->json([
'status' => 200,
]);
I would be very Thankful if anyone of you can help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Cloudinary::upload()
允许您一次处理单个资源(请参阅 上传 API)。对于每次上传,都会返回响应 JSON,其中包含新上传资产的所有信息(请参阅示例响应 JSON )。如果您希望捕获每个资产的结果,您可以在foreach
中收集它们,例如:The
Cloudinary::upload()
allows you to process a single asset at one time (see Upload API). For every upload, the response JSON will be returned containing all the information for the newly uploaded asset (see sample response JSON). If you wish to capture the results of every asset, you could collect them within theforeach
, for example: