laravel 詳細ページ

routingから
/posts/{id} とする。

Route::get('/', 'PostsController@index');
Route::get('/posts/{id}', 'PostsController@show');

postController.php

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

show.Blade.php

<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<title>{{ $posts->title }}</title>
	<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
	<div class="container">
		<h1>{{ $posts->title }}</h1>
		<p>{!! nl2br(e($posts->body)) !!}</p>
	</div>
</body>
</html>

implicit binding
なんかまた新しいの出てきた

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

ぎょえーーーーーーーーーー

laravelのcss

public/css フォルダの中
style.css

body {
	font-family: Verdana, sans-serif;
	font-size: 14px;
}

.container {
		width: 400px;
		margin: 20px auto;	
}

h1 {
	font-size: 16px;
	padding-bottom: 10px;
	margin-bottom: 15px;
	border-bottom: 1px solid #ddd;
}

ul > li {
		margin-bottom: 5px;
}

ふむふむ

bladeの@forelseという書き方

コメントアウトは{{– –}}
@forelse, @emptyで出し分け出来る。

<ul>
			{{-- 
			@foreach ($posts as $post)
			<li><a href="">{{ $post->title }}</a></li>
			@endforeach
			--}}

			@forelse ($posts as $post)
			<li><a href="">{{ $post->title }}</a></li>
			@empty
			<li>No posts yet</li>
			@endforelse
		</ul>
class PostsController extends Controller
{
    //
    public function index(){
    	// $posts = Post::orderBy('created_at', 'desc')->get();
    	// $posts = Post::latest()->get();
    	$posts = [];
    	// dd($posts->toArray()); // dump die
    	// return view('posts.index', ['posts' => $posts]);
    	return view('posts.index')->with('posts', $posts);
    }

}

ぎゃー

Laravel controllerを編集

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class PostsController extends Controller
{
    //
    public function index(){
    	$posts = Post::all();
    	dd($posts->toArray()); // dump die
    	return view('posts.index');
    }

}

うわー どうしよ、これ。困った。。

orderBy, latest()でfetchできる。

class PostsController extends Controller
{
    //
    public function index(){
    	// $posts = Post::orderBy('created_at', 'desc')->get();
    	$posts = Post::latest()->get();
    	dd($posts->toArray()); // dump die
    	return view('posts.index');
    }

}
class PostsController extends Controller
{
    //
    public function index(){
    	// $posts = Post::orderBy('created_at', 'desc')->get();
    	$posts = Post::latest()->get();
    	// dd($posts->toArray()); // dump die
    	// return view('posts.index', ['posts' => $posts]);
    	return view('posts.index')->with('posts', $posts);
    }

}
<body>
	<div class="container">
		<h1>Blog Posts</h1>
		<ul>
			@foreach ($posts as $post)
			<li><a href="">{{ $post->title }}</a></li>
			@endforeach
		</ul>
	</div>
</body>

Laravelでviewをつくっていく

resources/views の中にpostsフォルダを作成し、その中にindex.blade.phpファイルを作成する。

PostsController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller
{
    //
    public function index(){
    	return view('posts.index');
    }

}

html

<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<title>Blog Posts</title>
</head>
<body>
	<div class="container">
		<h1>Blog Posts</h1>
		<ul>
			<li><a href="">title</a></li>
			<li><a href="">title</a></li>
			<li><a href="">title</a></li>
		</ul>
	</div>
</body>
</html>

[vagrant@localhost myblog]$ php artisan serve –host 192.168.35.10 –port 8000
Laravel development server started:
[Sun Sep 9 12:24:50 2018] 192.168.35.1:57370 [200]: /favicon.ico

なんだこれ

Laravel routing, controller

/routes/web.php

Route::get('/', 'PostsController@index');

php artisanでPostControllerをつくる
[vagrant@localhost myblog]$ php artisan make:controller PostController
Controller created successfully.

あ、PostsControllerでした。
[vagrant@localhost myblog]$ php artisan make:controller PostsController
Controller created successfully.

app/Httpの中に作られます。

ipアドレスを調べる。コマンドはip a
[vagrant@localhost myblog]$ ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:a9:1b:8f brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
inet6 fe80::a00:27ff:fea9:1b8f/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:cb:a5:1b brd ff:ff:ff:ff:ff:ff
inet 192.168.35.10/24 brd 192.168.35.255 scope global eth1
inet6 fe80::a00:27ff:fecb:a51b/64 scope link
valid_lft forever preferred_lft forever

tinker 抽出、更新、削除

クラスを呼んでいる書き方だが、独特ですな。
[vagrant@localhost myblog]$ php artisan tinker
Psy Shell v0.9.8 (PHP 7.1.21 — cli) by Justin Hileman
>>> App\Post::find(3)->toArray();
=> [
“id” => 3,
“title” => “title 3”,
“body” => “body 3”,
“created_at” => “2018-09-08 23:32:01”,
“updated_at” => “2018-09-08 23:32:01”,
]

>>> App\Post::where(‘id’, ‘>’, 1)->orderBy(‘created_at’,’desc’)->get()->toArray();
=> [
[
“id” => 3,
“title” => “title 3”,
“body” => “body 3”,
“created_at” => “2018-09-08 23:32:01”,
“updated_at” => “2018-09-08 23:32:01”,
],
[
“id” => 2,
“title” => “title 2”,
“body” => “body 2”,
“created_at” => “2018-09-08 23:31:24”,
“updated_at” => “2018-09-08 23:31:24”,
],
]

update
>>> $post = App\Post::find(3);
=> App\Post {#2893
id: “3”,
title: “title 3”,
body: “body 3”,
created_at: “2018-09-08 23:32:01”,
updated_at: “2018-09-08 23:32:01”,
}
>>> $post->title = ‘title 3 updated’;
=> “title 3 updated”
>>> $post->save();
=> true

delete


>>> $post = App\Post::find(3);
=> App\Post {#2908
     id: "3",
     title: "title 3 updated",
     body: "body 3",
     created_at: "2018-09-08 23:32:01",
     updated_at: "2018-09-09 00:18:20",
   }
>>> $post->delete();
=> true
>>> App\Post::all()->toArray();
=> [
     [
       "id" => 1,
       "title" => "title 1",
       "body" => "body 1",
       "created_at" => "2018-09-08 23:21:01",
       "updated_at" => "2018-09-08 23:21:01",
     ],
     [
       "id" => 2,
       "title" => "title 2",
       "body" => "body 2",
       "created_at" => "2018-09-08 23:31:24",
       "updated_at" => "2018-09-08 23:31:24",
     ],
   ]

laravel thinker

[vagrant@localhost myblog]$ php artisan tinker
Psy Shell v0.9.8 (PHP 7.1.21 — cli) by Justin Hileman
>>> $post = new App\Post();
=> App\Post {#2900}
>>> $post->title = 'title 1';
=> "title 1"
>>> $post->body = 'body 1';
=> "body 1"
>>> $post->save();
=> true
>>> App\Post::all();
=> Illuminate\Database\Eloquent\Collection {#2908
     all: [
       App\Post {#2909
         id: "1",
         title: "title 1",
         body: "body 1",
         created_at: "2018-09-08 23:21:01",
         updated_at: "2018-09-08 23:21:01",
       },
     ],
   }
>>> App\Post::all()->toArray();
=> [
     [
       "id" => 1,
       "title" => "title 1",
       "body" => "body 1",
       "created_at" => "2018-09-08 23:21:01",
       "updated_at" => "2018-09-08 23:21:01",
     ],
   ]

sqliteで見てみます。

[vagrant@localhost myblog]$ sqlite3 database/database.sqlite
SQLite version 3.6.20
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from posts;
1|title 1|body 1|2018-09-08 23:21:01|2018-09-08 23:21:01

appの中のpost.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //
    protected $fillable = ['title', 'body'];
}

[vagrant@localhost myblog]$ php artisan tinker
Psy Shell v0.9.8 (PHP 7.1.21 — cli) by Justin Hileman
>>> App\Post::create([‘title’=>’title 2’, ‘body’=>’body 2’]);
=> App\Post {#2898
title: “title 2”,
body: “body 2”,
updated_at: “2018-09-08 23:31:24”,
created_at: “2018-09-08 23:31:24”,
id: 2,
}
>>> App\Post::create([‘title’=>’title 3’, ‘body’=>’body 3’]);
=> App\Post {#2906
title: “title 3”,
body: “body 3”,
updated_at: “2018-09-08 23:32:01”,
created_at: “2018-09-08 23:32:01”,
id: 3,
}
>>> App\Post::all()->toArray();
=> [
[
“id” => 1,
“title” => “title 1”,
“body” => “body 1”,
“created_at” => “2018-09-08 23:21:01”,
“updated_at” => “2018-09-08 23:21:01”,
],
[
“id” => 2,
“title” => “title 2”,
“body” => “body 2”,
“created_at” => “2018-09-08 23:31:24”,
“updated_at” => “2018-09-08 23:31:24”,
],
[
“id” => 3,
“title” => “title 3”,
“body” => “body 3”,
“created_at” => “2018-09-08 23:32:01”,
“updated_at” => “2018-09-08 23:32:01”,
],
]

laravel modelを作ろう

php artisan make:mode hoge opiton と打つようです。
[vagrant@localhost myblog]$ php artisan make:model Post –migration
Model created successfully.
Created Migration: 2018_09_08_222520_create_posts_table

なんじゃこりゃー 全く覚えてないぞ。。
migration fileが作られる。
root -> database -> migration

migration file
upがこのmigrationで行いたい処理、downが巻き戻したい時

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

timestampはcreated at and updated atを管理する。