如何使用 laravel 和 Cloudinary 上传多张图片?

发布于 2025-01-16 03:57:48 字数 2203 浏览 3 评论 0原文

实际上我正在做一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2025-01-23 03:57:49
foreach ($request->file('images') as $imagefile) {
  $uploadedFileUrl = Cloudinary::upload($imagefile->getRealPath())->getSecurePath();
}
foreach ($request->file('images') as $imagefile) {
  $uploadedFileUrl = Cloudinary::upload($imagefile->getRealPath())->getSecurePath();
}
瞳孔里扚悲伤 2025-01-23 03:57:49

Cloudinary::upload() 允许您一次处理单个资源(请参阅 上传 API)。对于每次上传,都会返回响应 JSON,其中包含新上传资产的所有信息(请参阅示例响应 JSON )。如果您希望捕获每个资产的结果,您可以在 foreach 中收集它们,例如:

$unit->unit_pictures = $request->file('unit_pictures'); 

$allUploadApiReponse = array();

foreach ( $unit->unit_pictures=$request->file('unit_pictures') as $imagefile) 
{ 
    $uploadedFileUrl = Cloudinary::upload($imagefile->getRealPath(), [ 'folder' => 'Units' ] )->getSecurePath(); 
    array_push($allUploadApiReponse, $uploadedFileUrl);
} 

// To check content
print_r($allUploadApiReponse);

$unit->save();`

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 the foreach, for example:

$unit->unit_pictures = $request->file('unit_pictures'); 

$allUploadApiReponse = array();

foreach ( $unit->unit_pictures=$request->file('unit_pictures') as $imagefile) 
{ 
    $uploadedFileUrl = Cloudinary::upload($imagefile->getRealPath(), [ 'folder' => 'Units' ] )->getSecurePath(); 
    array_push($allUploadApiReponse, $uploadedFileUrl);
} 

// To check content
print_r($allUploadApiReponse);

$unit->save();`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文