laravel collectiveを使ってみよう

パッケージというと、centOSのrpmや、npmのnode moduleのようなイメージでしょうか?
「laravel illuminate/html」で検索します

github:https://github.com/illuminate/html

https://laravelcollective.com/docs/5.2/html
なんじゃこりゃーー

composer.json
requireにインストールするパッケージ名を追加します。

"require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.2.*",
        "laravelcollective/html":"^5.2.0"
    },

$ php composer.phar update

./config/app.php
provider

'providers' => [
    // ...
    Collective\Html\HtmlServiceProvider::class,
    // ...
  ],

aliases

'aliases' => [
 // ...
 'Form' => Collective\Html\FormFacade::class,
 'Html' => Collective\Html\HtmlFacade::class,
 // ...
],

あ、aliasesは普段使用しているclass名です
ここで引っ張て来てるんですね

view: create.blade.php
{!! Form::open() !!}と記載する

<h1>Create Post</h1>
	<!-- <form method="post" action="/posts"> -->
	{!! Form::open() !!}
		{{ csrf_field()}}
		<input type="text" name="title" placeholder="Enter tilte">
		<input type="submit" name="submit">
	</form>

methodを記入

{!! Form::open(['method'=>'POST', 'action'=>'PostsController@store']) !!}
		{{ csrf_field()}}
		<input type="text" name="title" placeholder="Enter tilte">
		<input type="submit" name="submit">
	{!! Form::close() !!}

更に、text form、submit buttonもcollectiveを使用

{!! Form::open(['method'=>'POST', 'action'=>'PostsController@store']) !!}
		{{ csrf_field()}}

		<div class="form-group">
			{!! Form::label('title', 'Title') !!}
			{!! Form::text('title', null, ['class'=>'form-controll']) !!}
		</div>

		<div class="form-group">
			{{ Form::submit('Create Post', ['class'=>'btn btn-primary'])}}

		</div>
	{!! Form::close() !!}

edit & delete

@section('content')

	<h1>Edit Posts</h1>
	{!! Form::model($post, ['method'=>'PATCH', 'action'=>['PostsController@update', $post->id]]) !!}
		{{ csrf_field()}}

		<div class="form-group">
		{!! Form::label('title', 'Title') !!}
		{!! Form::text('title', null, ['class'=>'form-controll']) !!}

		{!! Form::submit('Update Post', ['class'=>'btn btn-info']) !!}
		</div>
	{!! Form::close() !!}

	{!! Form::open(['method'=>'DELETE', 'action'=>['PostsController@destroy', $post->id]]) !!}
		{{ csrf_field()}}

		<div class="form-group">
		{!! Form::submit('DELETE', ['class'=>'btn btn-danger']) !!}
		</div>
	{!! Form::close() !!}

@endsection

HTMLのformとは異なるので、使いこなすには練習・復習が必要です。

Laravel Froms 1

Route::resource(‘/posts’, ‘PostsController’);

/posts/create.blade.php

@extends('layouts.app')


@section('content')

	<form method="post" action="/posts">
                {{ csrf_field()}}
		<input type="text" name="title" placeholder="Enter tilte">
		<input type="submit" name="submit">
	</form>

@endsection

@section('footer')
@endsection

PostsController

 public function create()
    {
        return view('posts.create');
    }

route
※index, store, create, show, update, destory, editにアクセスできるようになる

Route::resource('/posts', 'PostsController');

PostsController

public function create()
    {
        return view('posts.create');
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        // return $request->all();

        Post::create($request->all());
    }

same behavior
return $request->get(‘title’);
return $request->title;

return $request->all();
{“_token”:”ANAHFf7WGgKgaEDvvpA7482QF8cAAZH1ifaBFjHa”,”title”:”sakura”,”submit”:”\u9001\u4fe1″}

よく見る書き方

$post = new Post;
        $post->title = $request->title;
        $post->save();

controller

public function index()
    {
        $posts = Post::all();
        return view("posts.index", compact('posts'));
    }

view

@section('content')
	<ul>
		@foreach($posts as $post)
		<li>{{$post->title}}</li>
		
		@endforeach
	</ul>
@endsection

controller: show

public function show($id)
    {
        $post = Post::findOrFail($id);
        return view('posts.show', compact('post'));
    }

view index.blade.php

<li><a href="{{ route('posts.show', $post->id) }}">{{$post->title}}</a></li>

controller: edit

public function edit($id)
    {
        //
        $post = Post::findOrFail($id);
        return view('posts.edit', compact('post'));

    }

view: edit

<h1>Edit Posts</h1>
	<form method="post" action="/posts/{{$post->id}}">
		{{ csrf_field()}}
		<input type="hidden" name="_method" value="PUT">
		<input type="text" name="title" placeholder="Enter tilte" value="{{$post->title}}">
		<input type="submit" name="submit">
	</form>

controller: update

public function update(Request $request, $id)
    {
        //
        $post = Post::findOrFail($id);

        $post->update($request->all());

        return redirect('posts');
    }

view: show

<h1><a href="{{route('posts.edit', $post->id)}}">{{$post->title}}</a></h1>

controller: delete

public function destroy($id)
    {
        $post = Post::findOrFail($id);
        $post->delete();
        return redirect('/posts');
    }

viiew:delete

<h1>Edit Posts</h1>
	<form method="post" action="/posts/{{$post->id}}">
		{{ csrf_field()}}
		<input type="hidden" name="_method" value="PUT">
		<input type="text" name="title" placeholder="Enter tilte" value="{{$post->title}}">
		<input type="submit" name="submit" value="UPDATE">
	</form>

	<form method="post" action="/posts/{{$post->id}}">
		{{ csrf_field()}}
		<input type="hidden" name="_method" value="DELETE">
		<input type="submit" value="DELETE">
	</form>

OK, まだ基礎の基礎
毎度のことだが、ボリュームの見積もりが甘すぎた