Laravel belongsToのフォームの書き方

formはlaravel collectiveを使用。make:model ${modelName} -mでDBを作って、データを挿入し、select文に対してControllerのcreateメソッドからデータを渡す

View: create.blade.php

{!! Form::open(['method'=>'POST', 'action'=>'AdminPostsController@store', 'files'=>true]) !!}
        {{ csrf_field()}}
 
        <div class="form-group">
            {!! Form::label('title', 'Title') !!}
            {!! Form::text('title', null, ['class'=>'form-control']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('category_id', 'Category') !!}
            {!! Form::select('category_id', array(''=>'options'), null, ['class'=>'form-control']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('photo_id', 'Photo') !!}
            {!! Form::file('photo_id', null,['class'=>'form-control']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('body', 'Description') !!}
            {!! Form::textarea('body', null, ['class'=>'form-control', 'rows'=>3]) !!}
        </div>

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

$ php artisan make:request PostsCreateRequest

Requests/PostCreateRequest.php

public function rules()
    {
        return [
            'category_id' => 'required',
            'photo_id' => 'required',
            'title' => 'required',
            'body' => 'required'
        ];
    }

AdminPostsController.php

use App\Http\Requests\PostsCreateRequest;
use Illuminate\Support\Facades\Auth;
public function store(PostsCreateRequest $request)
    {
        //
        $input = $request->all();
        $user = Auth::user();
        if($file = $request['photo_id']){
            $name = time(). $file->getClientOriginalName();
            $file->move('images', $name);
            $photo = Photo::create(['file'=>$name]);

            $input['photo_id'] = $photo->id;
        }
        $user->posts()->create($input);
        return redirect('/admin/posts');
    }

View: posts/index.blade.php

<tr>
			  <td>{{$post->id}}</td>
			  <td>{{$post->user->name}}</td>
			  <td>{{$post->category_id}}</td>
			  <td><img height="50" src="{{$post->photo ? '/' . $post->photo->file : 'https://placehold.it/50x50'}}"></td>
			  <td>{{$post->title}}</td>
			  <td>{{$post->body}}</td>
			  <td>{{$post->created_at->diffForHumans()}}</td>
			  <td>{{$post->updated_at->diffForHumans()}}</td>
			</tr>

$ php artisan make:model Category -m

Model: Category.php

class Category extends Model
{
    //
    protected $fillable = ['name'];
}

Post.php

public function category(){
    	return $this->belongsTo('App\Category');
    }

categories migration files

Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });

$ php artisan migrate

insert into categories (name) values (‘PHP’);
insert into categories (name) values (‘Laravel’);
insert into categories (name) values (‘JavaScript’);
insert into categories (name) values (‘Photoshop’);

/posts/index.blade.php

<td>{{$post->category ? $post->category->name : 'Uncategorized'}}</td>

AdminPostsController

use App\Category;
public function create()
    {
        //
        $categories = Category::lists('name', 'id')->all();
        return view('admin.posts.create', compact['categories']);
    }

View: posts/create.blade.php

<div class="form-group">
            {!! Form::label('title', 'Title') !!}
            {!! Form::text('title', null, ['class'=>'form-control']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('category_id', 'Category') !!}
            {!! Form::select('category_id', array(''=>'Choose Options') + $categories, null, ['class'=>'form-control']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('photo_id', 'Photo') !!}
            {!! Form::file('photo_id', null,['class'=>'form-control']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('body', 'Description') !!}
            {!! Form::textarea('body', null, ['class'=>'form-control', 'rows'=>3]) !!}
        </div>

DBにuncategorizedという値を入れずに、view側で$post->category ? $post->category->name : ‘Uncategorized’と書く方法は面白い、isnt it?