Buenas amigos, tengo el siguiente problema en laravel. Quiero crear un registro en una tabla llamada movies que esta relacionada con una tabla category por medio de una llave foranea. Al ir a la vista new.blade.php me muestra siguiente error:
(1/1) RelationNotFoundException
Call to undefined relationship [category] on model [App\Category].
Tabla Movies
public function up()
{
Schema::create('movies', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50)->required();
$table->string('description', 50)->nullable();
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}
Modelo Movie
class Movie extends Model
{
protected $table = 'movies';
protected $primaryKey = 'id';
protected $fillable = ['name', 'description', 'category_id'];
protected $guarded = ['id'];
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
}
Modelo Category
class Category extends Model
{
protected $table = 'categories';
protected $primaryKey = 'id';
protected $fillable = ['name', 'description'];
protected $guarded = ['id'];
public function movies()
{
return $this->hasMany('App\Movie');
}
}
Controlador MovieController@create
public function create()
{
$categoria = Category::with('category')->get();
return \View::make('new', compact('categoria'));
}
Vista crear new.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
{!! Form::open(['route' => 'movie.store', 'method' => 'post', 'novalidate']) !!}
<div class="form-group">
{!! Form::label('full_name', 'Nombre') !!}
{!! Form::text('name', null, ['class' => 'form-control' , 'required' => 'required']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Descripción') !!}
{!! Form::text('description', null, ['class' => 'form-control' , 'required' => 'required']) !!}
</div>
<div class="form-group">
{!! Form::label('category', 'Categoria') !!}
<select name="category_id">
@foreach($categoria as $catego)
<option value="{{$catego->category->id}}">{{$catego->category->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
{!! Form::submit('Enviar', ['class' => 'btn btn-success ' ] ) !!}
</div>
{!! Form::close() !!}
</div>
</div>
{{$categoria}}
</div>
@endsection