本文整理汇总了PHP中Illuminate\Database\Eloquent\Collection::first方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::first方法的具体用法?PHP Collection::first怎么用?PHP Collection::first使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Collection
的用法示例。
在下文中一共展示了Collection::first方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUnitsCollection
/**
* @param $imei
* @param Collection|null $traza
* @return array
*/
private function setUnitsCollection($imei, Collection $traza = null, Collection $modeloInfo = null)
{
$result = [];
if (!$traza == null) {
return $result = ['imei' => $imei, 'sl' => $traza->first()->Codigo, 'fechaHora' => $traza->first()->FechaHora, 'carrier' => $this->findInCollection($modeloInfo, 'Campo', 'CARRIER')->Valor, 'salesModel' => $this->findInCollection($modeloInfo, 'Campo', 'SALESMODEL')->Valor, 'partNumber' => $this->findInCollection($modeloInfo, 'Campo', 'PART_NUMBER')->Valor];
} else {
return $result = ['imei' => $imei, 'sl' => 'Sin resultados', 'fechaHora' => '-', 'carrier' => '-', 'salesModel' => '-', 'partNumber' => '-'];
}
}
示例2: upload
public function upload(PhotoUploadRequest $request)
{
$user = Auth::user();
$destinationPath = 'uploads/';
if ($request->hasFile('files')) {
$photos = new Collection();
$i = 0;
foreach ($request->file('files') as $file) {
$extension = $file->getClientOriginalExtension();
$new_filename = time() . uniqid() . str_random(rand(5, 15)) . '.' . $extension;
$file->move($destinationPath, $new_filename);
$photos[$i] = new Photo();
$photos[$i]->content_type = $file->getClientMimeType();
$photos[$i]->disk_name = $new_filename;
$photos[$i]->file_name = $file->getClientOriginalName();
$photos[$i]->path = $destinationPath . $new_filename;
$photos[$i]->file_size = $file->getClientSize();
$photos[$i]->user_id = $user->id;
$photos[$i]->save();
$i++;
}
return $photos->first();
}
return response()->json(['status' => 'error']);
}
示例3: formatEloquentCollection
/**
* Format an Eloquent collection.
*
* @param \Illuminate\Database\Eloquent\Collection $collection
* @return string
*/
public function formatEloquentCollection($collection)
{
if ($collection->isEmpty()) {
return $this->encode([]);
}
$key = str_plural($collection->first()->getTable());
return $this->encode([$key => $collection->toArray()]);
}
示例4: shouldTryAgain
/**
* shouldTryAgain.
*
* @author Casper Rasmussen <cr@nodes.dk>
* @return bool
*/
private function shouldTryAgain()
{
if ($this->failedCarbons->count() < $this->maxNetworkRetries) {
return true;
}
/** @var Carbon $carbon */
$carbon = $this->failedCarbons->first();
if ($carbon->diffInSeconds(Carbon::now()) >= $this->retryNetworkAfterSec) {
return true;
}
return false;
}
示例5: formatEloquentCollection
/**
* Format an Eloquent collection.
*
* @param \Illuminate\Database\Eloquent\Collection $collection
*
* @return string
*/
public function formatEloquentCollection($collection)
{
if ($collection->isEmpty()) {
return $this->encode([]);
}
$model = $collection->first();
$key = str_plural($model->getTable());
if (!$model::$snakeAttributes) {
$key = camel_case($key);
}
return $this->encode([$key => $collection->toArray()]);
}
示例6: match
public function match(array $models, Collection $results, $relation)
{
$match = $this->matchPHP;
foreach ($models as $model) {
$value = $results->first(function ($result) use($match, $model) {
return $match($model, $result);
});
if ($value) {
$model->setRelation($relation, $value);
}
}
return $models;
}
示例7: generateArrayData
/**
* @param \Illuminate\Database\Eloquent\Collection|static[] $collection
*/
public function generateArrayData($collection)
{
$modelObject = $collection->first();
$arrayData = array();
foreach ($collection as $item) {
$row = array();
foreach ($modelObject->getListAttributes() as $index => $value) {
$row[] = $item->{$index};
}
$pk = $modelObject->getPrimaryKey();
$href = route('admin_edit', ['model' => $this->model->getUrlName(), 'id' => "{$pk}:{$item->{$pk}}"]);
$row[] = "<a href='{$href}'>Editar</a>";
$arrayData[] = $row;
}
return $arrayData;
}
示例8: extractModelAndKeys
/**
* Extract the model instance and model keys from the given parameters.
*
* @param string|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection $model
* @param array|null $keys
* @return array
*/
public static function extractModelAndKeys($model, array $keys = null)
{
if (is_null($keys)) {
if ($model instanceof Model) {
return [$model, [$model->getKey()]];
}
if ($model instanceof Collection) {
return [$model->first(), $model->modelKeys()];
}
} else {
if (is_string($model)) {
$model = new $model();
}
return [$model, $keys];
}
}
示例9: getMeta
/**
* Gets meta data
*
* @param string $key
* @param null|mixed $default
* @param bool $raw
* @return Collection
*/
public function getMeta($key, $default = null, $raw = false)
{
$meta = $this->meta()->where('key', $key)->get();
if ($raw) {
$collection = $meta;
} else {
$collection = new Collection();
foreach ($meta as $m) {
$collection->put($m->id, $m->value);
}
}
if (0 == $collection->count()) {
return $default;
}
return $collection->count() <= 1 ? $collection->first() : $collection;
}
示例10: showProspects
/**
* Display one or more prospects.
*
* @note We're using the 'table()' function which expects an array
* of arrays (i.e. rows of data items/fields)
*
* @param Collection $prospects
* @param bool $forceList
* @param int $maxLen
*/
protected function showProspects($prospects, $forceList = false, $maxLen = 76)
{
if (empty($prospects) || $prospects->isEmpty()) {
$this->info('No prospect records to display');
return;
}
if ($prospects->count() > 1 || $forceList) {
$rows = $prospects->toArray();
$this->table(array_keys($rows[0]), $rows);
} else {
$prospect = $prospects->first();
$data = $prospect->toArray();
$rows = [];
foreach ($data as $k => $v) {
$rows[] = [$k, is_string($v) ? str_limit($v, $maxLen) : $v];
}
$this->table(['field', 'value'], $rows);
}
$this->info('');
// Print a blank line
}
示例11: nenaplaceno
/**
* Odgovor servera u slučaju pokušaja rezerviranja kada postoje nenaplaćene rezervacije.
* @param \Illuminate\Database\Eloquent\Collection $nenaplaceneRezervacije Nenaplaćene rezervacije
* @return Response
*/
private function nenaplaceno($nenaplaceneRezervacije)
{
Session::flash(self::DANGER_MESSAGE_KEY, 'Nije dozvoljeno praviti rezervaiju dok ne naplatite sve odrađene.<br/>' . 'Primjerice rezervacija ' . $nenaplaceneRezervacije->first()->link() . ' nije naplaćena.');
return Redirect::route('home');
}
示例12: testFirstReturnsFirstItemInCollection
public function testFirstReturnsFirstItemInCollection()
{
$c = new Collection(array('foo', 'bar'));
$this->assertEquals('foo', $c->first());
}
示例13: findProductItem
/**
* Find the item entry of a specific product key in a collection of items.
*
* @param \Illuminate\Database\Eloquent\Collection $items
* @param mixed $key The product's primary key.
*
* @return \REDACTED\Wastage\ItemModel|null
*/
protected function findProductItem(Collection $items, $key)
{
if ($key instanceof ItemModel) {
$key = $key->getKey();
}
return $items->first(function ($id, $item) use($key) {
return $item->product_id == $key;
});
}
示例14: first
public function first()
{
return $this->data->first();
}
示例15: updateUserHashtags
/**
* Updates the current user's searched hashtags with the specified hashtag
* $terms and returns the number of hashtags updated or -1 on failure.
*
* @param Collection $hashtags hashtag collection to update
* @param array|string $terms hashtag terms to update with
* @return integer
*/
protected function updateUserHashtags(Collection $hashtags, array $terms)
{
$num_updates = 0;
foreach ($terms as $id => $term) {
// remove whitespace, convert to lowercase
$term = strtolower(preg_replace('/\\s+/', '', $term));
if (!empty(trim($term))) {
// verify hashtag term does not already exists
$hashtag = $hashtags->first(function ($key, $value) use($id, $term) {
return $value->id != $id && $value->term == $term;
});
if (isset($hashtag)) {
// indicate error has occured
$num_updates = -1;
\Session::put('error_hashtag_edit_id', $id);
\Session::flash('flash_message', 'Hashtag \'' . $term . '\' already exists.');
break;
}
// update hashtag term if not the same
$hashtag = $hashtags->find($id);
if (isset($hashtag) && $hashtag->term != $term) {
$hashtag->term = $term;
$hashtag->save();
$num_updates++;
}
}
}
return $num_updates;
}