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, まだ基礎の基礎
毎度のことだが、ボリュームの見積もりが甘すぎた