當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Language::firstOrCreate方法代碼示例

本文整理匯總了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);
         }
     }
 }
開發者ID:RichardMarbach,項目名稱:mkdir,代碼行數:45,代碼來源:DVDRepository.php


注:本文中的app\models\Language::firstOrCreate方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。