Eloquent ORMまとめ(Laravel5)

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


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

- new
- プロパティのセット
- save

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

例


```php
$tag = new Tag();
$tag->name = 'PHP';
$tag->save();
```

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

```php

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!');
}
```

```php

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);
}
```

```php

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);
}
```

```php

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');
}
```

```php

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


```php

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

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

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

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

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

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

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

```php
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!');
}
```

```php
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);
}
```

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

```php
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)")]);
```

#データの削除

```php
$author = Author::find(1);
$author -> delete();
```

サンプルから抜粋

```php
public function destroy($id) {
  $post = Post::findOrFail($id);
  $post->delete();
  return redirect('/')->with('flash_message', 'Post Deleted!');
}
```

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

```php
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メソッドでデータ取得しても、取得できない