在我的应用程序的发布功能中,帖子存在于包括流派,类别和帖子的三阶段层次结构中。类型有很多类别,类别有很多帖子。当我尝试访问我的帖子/创建视图时,我收到一条错误消息,指出Class'类型'无法找到。以下是相关模型:
类型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Category;
class Genre extends Model
{
protected $fillable = array('name');
public function category() {
return $this->hasMany('Category');
}
}
类别:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Genre;
use App\Post;
class Category extends Model
{
protected $fillable = array('name', 'genre_id');
public function post() {
return $this->hasMany('Post');
}
public function genre() {
return $this->belongsTo('Genre', 'genre_id');
}
}
发表:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Category;
class Post extends Model
{
protected $fillable = array('title', 'slug', 'category_id', 'body');
public function category() {
return $this->belongsTo('Category', 'category_id');
}
}
这里是PostController中的use部分和create函数:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Category;
use App\Genre;
class PostController extends Controller
{
public function create()
{
$categories = Category::with('genre')->get();
return view('posts/create')->withCategories($categories);
}
}
创建视图本身:
@extends('layouts/main')
@section('stylesheet')
<link href="/css/postcreate.css" rel=stylesheet>
@endsection
@section('content')
<div class="content">
<h4 class="font-display postcreate_title">CREATE POST</h4>
<form method="POST" action="{{ route('posts.store') }}" class="postcreate_form">
{{ csrf_field() }}
<div class="postcreate_form-group">
<label for="title" class="postcreate_form-label">Title:</label>
<input id="title" name="title" class="postcreate_form-input" required maxlength="255">
</div>
<div class="postcreate_form-group">
<label for="category_id" class="postcreate_form-label">Category:</label>
<select id="category_id" name="category_id" class="font-body postcreate_form-category" required>
<optgroup label="Fiction">
@foreach($categories as $category)
@if($category->genre()->name == 'Fiction')
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endif
@endforeach
</optgroup>
<optgroup label="Poetry">
@foreach($categories as $category)
@if($category->genre()->name == 'Poetry')
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endif
@endforeach
</optgroup>
<optgroup label="Essay">
@foreach($categories as $category)
@if($category->genre()->name == 'Essay')
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endif
@endforeach
</optgroup>
<optgroup label="Journal">
@foreach($categories as $category)
@if($category->genre()->name == 'Journal')
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endif
@endforeach
</optgroup>
</select>
</div>
<div>
<label for="slug" class="postcreate_form-label">Slug:</label>
<input id="slug" name="slug" class="font-body postcreate_form-input" required minlength="5" maxlength="255">
</div>
<div>
<label for="body" class="postcreate_form-label">Body:</label>
<textarea rows="30" id="body" name="body" class="font-body postcreate_form-input">Enter text here.</textarea>
</div>
<input class="xlight" type="submit" value="Publish">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
@endsection
有人建议我在Laracast主席上删除“#genre”中的括号。在if语句中,但这没有任何效果。同样,建议使用composer dump-autoload
和php artisan cache:clear
php artisan config:cache
,但没有效果。