只有当字段不为空时,才能下载Laravel

时间:2017-07-17 09:56:12

标签: php laravel curl laravel-5

我有一个名为' ical'它包含一个url,但我想要做的是运行一个函数使用url下载文件,但是当field为空时忽略该行。它的作用是它为每一行下载一个文件,如果有一个网址,它填入的数据是正确的,但是当字段为空时它下载了一个文件,但当然它是空的,而是我如果是空的话,只想完全忽略该行。

以下是代码:

<?php

namespace App\Console\Commands;

use App\Entity;
use Illuminate\Console\Command;
use Ixudra\Curl\Facades\Curl;

class DownloadIcal extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ical:download';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Download all ICAL files from ical field in businesses database';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $entities = Entity::pluck('ical', 'id');
        foreach ( $entities as $entityID => $entityIcal ) {
            $response = Curl::to($entityIcal)
                ->download('public/ical/'.$entityID.'.ics');
                $this->info("ICal Retrieved");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您所要做的就是在循环中使用if并检查值是否为空或为空。

$entities = Entity::pluck('ical', 'id');

foreach ( $entities as $entityID => $entityIcal ) 
{
    if ( !is_null($entityIcal) and !empty($entityIcal))
    {
        $response = Curl::to($entityIcal)
        ->download('public/ical/'.$entityID.'.ics');
        $this->info("ICal Retrieved");
    }
}