Laravel

Eloquent ORMまとめ(Laravel5)

More than 1 year has passed since last update.

https://readouble.com/laravel/5.3/ja/eloquent.html


「新しいデータを挿入」する場合


  • new

  • プロパティのセット

  • save

save時は、updated_atとcreated_atフィールドが自動的に更新される。

$tag = new Tag();

$tag->name = 'PHP';
$tag->save();


サンプルから該当箇所を抜粋


public function store(PostRequest $request) {
$post = new Post();
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect('/')->with('flash_message', 'Post Added!');
}


public function store(Request $request, $postId) {
$comment = new Comment(['body' => $request->body]);
$post = Post::findOrFail($postId);
$post->comments()->save($comment);

return redirect()->action('PostsController@show', $post->id);
}


public function store(Request $request, $postId)
{
$comment = new Comment();
$comment->message = $request->message;
   $comment->post_id = $postId;
$comment->user_id = Auth::user()->id;
$comment->save();
return redirect()->route('posts.show', $postId);
}


public function store(Request $request)
{
$post = new Post();
$post->message = $request->message;
$post->user_id = Auth::user()->id;
if($request->hasFile('file')) {
$file = $request->file('file');
$images_path = public_path()."/images";
$newfilename = time().$file->getClientOriginalName();
$file->move($images_path, $newfilename);
$post->image = $newfilename;
} else {
$post->image = "no image";
}
$post->save();
return redirect()->route('posts.index');
}


public function store(Request $request)
{
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$file = $request->file('file');
$images_path = public_path()."/images";
$newfilename = time().$file->getClientOriginalName();
$file->move($images_path, $newfilename);
$user->image = $newfilename;
$user->save();
Auth::login($user);
return redirect()->route('posts.index');
}


データ更新する場合


  • データ取得

  • save


$tag = Tag::find(1);
$tag->name = 'PHP';
$tag->save();


updateメソッドを使ってもよい

$tag = Tag::find(1)->update(['name' => 'PHP']);


サンプルから該当箇所を抜粋

public function index() {

$posts = Post::latest('created_at')->get();
return view('posts.index')->with('posts', $posts);
}

public function show($id) {

$post = Post::findOrFail($id);
return view('posts.show')->with('post', $post);
}

public function edit($id) {

$post = Post::findOrFail($id);
return view('posts.edit')->with('post', $post);
}

public function update(PostRequest $request, $id) {

$post = Post::findOrFail($id);
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect('/')->with('flash_message', 'Post Updated!');
}

public function index(Request $request)

{
if ($request->has('word'))
{
$word = $request->word;
$posts = Post::where('message', 'LIKE', '%'.$word.'%')
->orderBy('created_at', 'desc')->get();
} else {
$posts = Post::orderBy('created_at', 'desc')->get();
}
return view('posts.index')->with('posts', $posts);
}

public function show($id)
{
$post = Post::find($id);
return view('posts.show')->with('post', $post);
}

public function edit($id)

{
$user = User::find($id);
return view('users.edit')->with('user', $user);
}

public function update(Request $request, $id)

{
$user = User::find($id);
if($request->has('name')) {
$user->name = $request->name;
}
if($request->has('email')) {
$user->email = $request->email;
}
if($request->has('password')) {
$user->password = bcrypt($request->password);
}
if($request->hasFile('file')) {
$old_image_file = public_path()."/images/".$user->image;
File::delete($old_image_file);
$file = $request->file('file');
$images_path = public_path()."/images";
$newfilename = time().$file->getClientOriginalName();
$file->move($images_path, $newfilename);
$user->image = $newfilename;
}
$user->save();
return redirect()->route('posts.index');
}


複数データの更新


  • updateメソッドを使う

Book::where('publish_date', '<=', Carbon::now()->subYears(10))

->update(['title' => DB::raw("concat('*',title)")]);


データの削除

$author = Author::find(1);

$author -> delete();

サンプルから抜粋

public function destroy($id) {

$post = Post::findOrFail($id);
$post->delete();
return redirect('/')->with('flash_message', 'Post Deleted!');
}

public function destroy($postId, $commentId) {

$post = Post::findOrFail($postId);
$post->comments()->findOrFail($commentId)->delete();
return redirect()->action('PostsController@show', $post->id);
}

public function destroy($id)

{
$post = Post::find($id);
$post->delete();
return redirect()->route('posts.index');
}


絞りこんでdelete

Author::where('id', '<', 20)->delete();


主キーを指定してdestroyする


  • 複数の場合は配列で指定

Author::destroy(1);

Author::destroy([1, 3, 5]);
Author::destroy(1, 3, 5); //この書き方でもOK


削除フラグを立てて削除したことにする(ソフトデリート)


  • deleted_atフィールドを利用して、このフィールドの値がnullでなければ、データが削除されたものとして扱える。

https://readouble.com/laravel/5.dev/ja/eloquent.html


  • 削除フラグの機能を利用した場合、getメソッドでデータ取得しても、取得できない