【发布时间】:2017-12-21 19:22:00
【问题描述】:
我有一个名为“ical”的字段,其中包含一个 url,但我想做的是运行一个函数,使用该 url 下载文件,但是当字段为空时忽略该行。目前它会为每一行下载一个文件,如果有一个 url,它会填充正确的数据,但是当字段为空时,它会下载一个文件,但当然它是空的,而我只想完全如果为空则忽略该行。
代码如下:
<?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");
}
}
}
【问题讨论】:
-
试试这个
foreach ( $entities as $entityID => $entityIcal ) { if( $entityIcal ){ $response = Curl::to($entityIcal) ->download('public/ical/'.$entityID.'.ics'); $this->info("ICal Retrieved"); } }!!
标签: php laravel curl laravel-5