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を管理する。

laravel app.php

<?php

return &#91;

    /*
    |--------------------------------------------------------------------------
    | Application Name
    |--------------------------------------------------------------------------
    |
    | This value is the name of your application. This value is used when the
    | framework needs to place the application's name in a notification or
    | any other location as required by the application or its packages.
    |
    */

    'name' => env('APP_NAME', 'Laravel'),

    /*
    |--------------------------------------------------------------------------
    | Application Environment
    |--------------------------------------------------------------------------
    |
    | This value determines the "environment" your application is currently
    | running in. This may determine how you prefer to configure various
    | services the application utilizes. Set this in your ".env" file.
    |
    */

    'env' => env('APP_ENV', 'production'),

    /*
    |--------------------------------------------------------------------------
    | Application Debug Mode
    |--------------------------------------------------------------------------
    |
    | When your application is in debug mode, detailed error messages with
    | stack traces will be shown on every error that occurs within your
    | application. If disabled, a simple generic error page is shown.
    |
    */

    'debug' => env('APP_DEBUG', false),

    /*
    |--------------------------------------------------------------------------
    | Application URL
    |--------------------------------------------------------------------------
    |
    | This URL is used by the console to properly generate URLs when using
    | the Artisan command line tool. You should set this to the root of
    | your application so that it is used when running Artisan tasks.
    |
    */

    'url' => env('APP_URL', 'http://localhost'),

    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'Asia/Tokyo',

    /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'ja',

    /*
    |--------------------------------------------------------------------------
    | Application Fallback Locale
    |--------------------------------------------------------------------------
    |
    | The fallback locale determines the locale to use when the current one
    | is not available. You may change the value to correspond to any of
    | the language folders that are provided through your application.
    |
    */

    'fallback_locale' => 'en',

    /*
    |--------------------------------------------------------------------------
    | Encryption Key
    |--------------------------------------------------------------------------
    |
    | This key is used by the Illuminate encrypter service and should be set
    | to a random, 32 character string, otherwise these encrypted strings
    | will not be safe. Please do this before deploying an application!
    |
    */

    'key' => env('APP_KEY'),

    'cipher' => 'AES-256-CBC',

    /*
    |--------------------------------------------------------------------------
    | Autoloaded Service Providers
    |--------------------------------------------------------------------------
    |
    | The service providers listed here will be automatically loaded on the
    | request to your application. Feel free to add your own services to
    | this array to grant expanded functionality to your applications.
    |
    */

    'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

    ],

    /*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don't hinder performance.
    |
    */

    'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        'Blade' => Illuminate\Support\Facades\Blade::class,
        'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
        'Bus' => Illuminate\Support\Facades\Bus::class,
        'Cache' => Illuminate\Support\Facades\Cache::class,
        'Config' => Illuminate\Support\Facades\Config::class,
        'Cookie' => Illuminate\Support\Facades\Cookie::class,
        'Crypt' => Illuminate\Support\Facades\Crypt::class,
        'DB' => Illuminate\Support\Facades\DB::class,
        'Eloquent' => Illuminate\Database\Eloquent\Model::class,
        'Event' => Illuminate\Support\Facades\Event::class,
        'File' => Illuminate\Support\Facades\File::class,
        'Gate' => Illuminate\Support\Facades\Gate::class,
        'Hash' => Illuminate\Support\Facades\Hash::class,
        'Lang' => Illuminate\Support\Facades\Lang::class,
        'Log' => Illuminate\Support\Facades\Log::class,
        'Mail' => Illuminate\Support\Facades\Mail::class,
        'Notification' => Illuminate\Support\Facades\Notification::class,
        'Password' => Illuminate\Support\Facades\Password::class,
        'Queue' => Illuminate\Support\Facades\Queue::class,
        'Redirect' => Illuminate\Support\Facades\Redirect::class,
        'Redis' => Illuminate\Support\Facades\Redis::class,
        'Request' => Illuminate\Support\Facades\Request::class,
        'Response' => Illuminate\Support\Facades\Response::class,
        'Route' => Illuminate\Support\Facades\Route::class,
        'Schema' => Illuminate\Support\Facades\Schema::class,
        'Session' => Illuminate\Support\Facades\Session::class,
        'Storage' => Illuminate\Support\Facades\Storage::class,
        'URL' => Illuminate\Support\Facades\URL::class,
        'Validator' => Illuminate\Support\Facades\Validator::class,
        'View' => Illuminate\Support\Facades\View::class,

    ],

];