您好我是laravel的新手我创建了添加单个帖子的页面但由于某种原因,成功和错误消息根本没有出现无关紧要我成功插入新帖子或提交空表格虽然需要一些字段..我的控制器我正在使用这种方法postCreatePost
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Catgory;
class PostController extends Controller {
public function getBlogIndex() {
return view ('frontend.blog.index');
}
public function getSignlePost($post_id, $end = 'frontend') {
return view ($end, '.blog.single');
}
public function getCreatePost() {
return view('admin.blog.create_post');
}
public function postCreatePost(Request $request) {
$this->validate($request, [
'title' => 'required|max:120|unique:posts',
'author' => 'required|max:80',
'body' => 'required'
]);
$post = new Post();
$post->title = $request['title'];
$post->author = $request['author'];
$post->body = $request['body'];
$post->save();
//Attaching categories
return redirect()->route('admin.index')->with(['success','Post sucessfully created!']);
}
}
这是我的观点
@extends('layouts.admin-master')
@section('styles')
<link rel="stylesheet" href="{{ URL::secure('src/css/form.css') }}" type="text/css" />
@endsection
@section('content')
<div class="container">
@section('styles')
<link rel="stylesheet" href="{{ URL::to('src/css/common.css') }}" type="text/css" />
@append
@if(Session::has('fail'))
<section class="info-box fail">
{{ Session::get('fail') }}
</section>
@endif
{{ var_dump(Session::get('success')) }}
@if(Session::has('success'))
<section class="info-box success">
{{ Session::get('success') }}
</section>
@endif
@if(count($errors) > 0)
<section class="info-box fail">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</section>
@endif
<form action="{{ route('admin.blog.post.create') }}" method="post">
<div class="input-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" {{ $errors->has('title') ? 'claass=has-error' : '' }} />
</div>
<div class="input-group">
<label for="author">Author</label>
<input type="text" name="author" id="author" {{ $errors->has('author') ? 'claass=has-error' : '' }} />
</div>
<div class="input-group">
<label for="category_select">Add Categories</label>
<select name="category_select" id="category_select">
<!-- Foreach loop to output categories -->
<option value="Dummy Category ID">Dummy Category</option>
</select>
<button type="button" class="btn">Add Category</button>
<div class="added-categories">
<ul></ul>
</div>
</div>
<input type="hidden" name="categories" id="categories">
<div class="input-group">
<label for="body">Body</label>
<textarea name="body" id="body" cols="30" rows="10" {{ $errors->has('body') ? 'claass=has-error' : '' }}></textarea>
</div>
<button type="submit" class="btn">Create Post</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
@endsection
@section('scripts')
<script type="text/javascript" src="{{ URL::secure('src/js/posts.js') }}"></script>
@endsection
我在网络中间件中的路线
Route::group(['middleware' => ['web']], function () {
Route::get('/about', function() {
return view('frontend.other.about');
})->name('about');
Route::group([
'prefix' => '/admin'
], function() {
Route::get('/', [
'uses' => 'AdminController@getIndex',
'as' => 'admin.index'
]);
Route::get('/blog/posts/create', [
'uses' => 'PostController@getCreatePost',
'as' => 'admin.blog.create_post'
]);
Route::post('/blog/post/create', [
'uses' => 'PostController@postCreatePost',
'as' => 'admin.blog.post.create'
]);
});
});
和模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function categories() {
return $this->belongsToMany('App\Category', 'posts_categories');
}
}
答案 0 :(得分:1)
你使用了错误的语法。试试这个:
return redirect()->route('admin.index')->with('success','Post sucessfully created!');