本文整理匯總了PHP中app\Book::firstOrCreate方法的典型用法代碼示例。如果您正苦於以下問題:PHP Book::firstOrCreate方法的具體用法?PHP Book::firstOrCreate怎麽用?PHP Book::firstOrCreate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類app\Book
的用法示例。
在下文中一共展示了Book::firstOrCreate方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: store
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(BookRequest $request)
{
$titles = $request->input('titles');
// $bookclubs = $request->get('bookclubs');
// $bookIds = [];
$status_id = \App\BookStatus::availableId();
if (count($titles)) {
foreach ($titles as $title) {
$book = \App\Book::firstOrCreate(['title' => ucfirst($title)]);
auth()->user()->books()->detach($book->id);
auth()->user()->books()->attach($book->id, ['status_id' => $status_id]);
//send mail to user for added books and adding details
//$bookIds[] = $bookId => ['status_id' => $status_id];
// foreach ($bookclubs as $bookclub) {
// $book->bookclubs()->detach($bookclub);
// $book->bookclubs()->attach($bookclub,['owner_id' => auth()->user()->id]);
// }
}
} else {
flash()->error('Please enter atleast one Book Title');
return \Redirect::back();
}
flash('Book/Books added to your library.');
return \Redirect::back();
/***
// $book = \App\Book::where('title','=',$request->input('title'))->first();
// if($book)
// {
// flash('This book already exits add it to your library.');
// return \Redirect::route('books.show',$book->id);
// }
// $request['author_id'] = auth()->user()->id;
// Convert comma separated string into array of author names
// $authors = explode(',',$request->get('author'));
//going over author names build up array of author ids by getting author ids and creating author if does not exist
// $authorIds = [];
// foreach ($authors as $author) {
// $authorIds[] = App\Author::firstOrCreate(['name' => ucfirst($author)])->id;
// }
// $request['author_id'] = App\Author::firstOrCreate(['name' => ucfirst(\Input::get('author'))])->id;
// $request['publisher_id'] = App\Publisher::firstOrCreate(['name' => ucfirst(\Input::get('publisher'))])->id;
// $request['category_id'] = App\Category::firstOrCreate(['name' => ucfirst(\Input::get('category'))])->id;
// $request['language_id'] = App\Language::firstOrCreate(['name' => ucfirst(\Input::get('language'))])->id;
//create book
// $book = auth()->user()->books()->create($request->all(),['status_id' => $status_id]);
//atached book to authors
// $book->authors()->attach($authorIds);
//attach book to book clubs
// $book->bookclubs()->attach($request->input('bookclubs'),['status_id' => $status_id]);
// flash('Book added.');
// return \Redirect::back();
***/
}
示例2: update
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id, Request $request)
{
$bookclub = \App\BookClub::findOrFail($id);
$request['is_closed'] = $request->input('is_closed', 0);
$bookclub->update($request->all());
$titles = $request->input('books');
$status_id = \App\BookStatus::availableId();
if ($titles) {
foreach ($titles as $title) {
$book = \App\Book::firstOrCreate(['title' => ucfirst($title)]);
auth()->user()->books()->detach($book->id);
auth()->user()->books()->attach($book->id, ['status_id' => $status_id]);
\DB::table('book_book_club')->where('book_club_id', $bookclub->id)->where('book_id', $book->id)->where('owner_id', auth()->user()->id)->delete();
$bookclub->books()->attach($book->id, ['status_id' => $status_id, 'owner_id' => auth()->user()->id]);
}
}
if ($request->hasFile('image')) {
$this->setBookclubImage($bookclub, $request);
}
flash('Book Club updated.');
return \Redirect::back();
}