本文整理汇总了PHP中app\models\Language::firstOrCreate方法的典型用法代码示例。如果您正苦于以下问题:PHP Language::firstOrCreate方法的具体用法?PHP Language::firstOrCreate怎么用?PHP Language::firstOrCreate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Language
的用法示例。
在下文中一共展示了Language::firstOrCreate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create
/**
* Add a dvd to the database
* If you're curious where a million db requests are coming from, it's here.
* @param array $input
* @return bool
*/
public function create(array $input)
{
$dvd = $this->dvdInfo->firstOrCreate(['title' => $input['title'], 'description' => $input['description'], 'length' => $input['length'], 'cover_image' => $input['cover_image']]);
$price = Price::firstOrCreate(['price_whole' => $input['price_whole'], 'price_cents' => $input['price_cents'], 'late_fee_whole' => $input['late_fee_whole'], 'late_fee_cents' => $input['late_fee_cents'], 'points' => $input['points']]);
$input['price_id'] = $price->id;
// Insert producers
for ($i = 0; $i < sizeof($input['producer_name']); $i++) {
$producer = Producer::firstOrCreate(['name' => $input['producer_name'][$i]]);
$dvd->producers()->attach($producer);
}
// Insert genres
for ($i = 0; $i < sizeof($input['genre']); $i++) {
$genre = Genre::firstOrCreate(['genre' => $input['genre'][$i]]);
$dvd->genres()->attach($genre);
}
// Insert actors
for ($i = 0; $i < sizeof($input['actor_name']); $i++) {
$actor = Actor::firstOrCreate(['name' => $input['actor_name'][$i]]);
$dvd->actors()->attach($actor, ['character_name' => $input['character_name'][$i]]);
}
// Add new dvd stock
$newDvds = [];
for ($i = 0; $i < $input['stock']; $i++) {
array_push($newDvds, new DVD($input));
}
$dvd->dvds()->saveMany($newDvds);
foreach ($newDvds as $newDvd) {
// Insert languages
for ($i = 0; $i < sizeof($input['language_name']); $i++) {
$language = Language::firstOrCreate(['language' => $input['language_name'][$i]]);
$newDvd->languages()->attach($language);
}
// Insert subtitles
for ($i = 0; $i < sizeof($input['subtitle_name']); $i++) {
$subtitle = Language::firstOrCreate(['language' => $input['subtitle_name'][$i]]);
$newDvd->subtitles()->attach($subtitle);
}
}
}