Success and error message does'nt show - php

Hello i am new to laravel i created page to added single post but for some reason success and errors messages doesn't appear at all not matter i successfully insert a new post or submit empty form although some fields are required .. in my controller i am using this method 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!']);
}
}
This is my view
#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
My routes inside web middleware
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'
]);
});
});
And the model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function categories() {
return $this->belongsToMany('App\Category', 'posts_categories');
}
}

You're using wrong syntax. Try this one:
return redirect()->route('admin.index')->with('success','Post sucessfully created!');

Related

Data not pulling through to form - laravel

I am trying to pull data from a form and as per my screenshot below the data will not display, but it appears to be functioning to a certain extent as there are two users which exist in my instructor variable and there are two options to select from, can anyone help?
create.blade.php
#extends('layouts.app')
#section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Course</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
#csrf
<div class="form-group">
#if (Auth::user()->isAdmin())
{!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
{!! Form::select('Instructor[]', $Instructor, Input::get('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
#if($errors->has('Instructor'))
<p class="help-block">
{{ $errors->first('Instructor') }}
</p>
#endif
</div>
<div class="form-group">
<label class="required" for="name">Course Title</label>
<input class="form-control {{ $errors->has('title') ? 'is-invalid' : '' }}" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
#if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
#endif
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</div>
#endif
</form>
</div>
</div>
#endsection
CoursesController
protected function create()
{
{
$Instructor = \App\User::whereHas('role', function ($q) { $q->where('role_id', 2); } )->get()->pluck('title', 'id');
// $courses = Course::all()->pluck('title');
return view('admin.courses.create', compact('Instructor'));
}
}
User.php
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsToMany(Role::class, 'role_user');
}
public function isAdmin()
{
return $this->role()->where('role_id', 1)->first();
}
public function roles(){
return $this->belongsToMany('App\Role');
}
Thanks for any help.
The Laravel Collective Form::select requires the values to be in a [ key => value ] array pair. You are passing it a result collection.
You need to translate the result of $Instructor = \App\User::whereHas('role', function ($q) { $q->where('role_id', 2); } )->get()->pluck('title', 'id'); to an array that is (I assume) [ id => title, id => title ]...
So, before you pass it to your view something like this:
$flatInstructors = []
$Instructor->each(function($item, $key) {
$flatInstructors[$item->id] = $item->title;
});
return view('admin.courses.create', compact('flatInstructors'));
And use $flatInstructors in your Form::select generation.

Laravel - Undefined variable: Instructor

I am trying to pass users with a specific role_id through a form on a create.blade.php see below;
#extends('layouts.app')
#section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Course</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
#csrf
<div class="panel-body">
#if (Auth::user()->isAdmin())
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
{!! Form::select('Instructor[]', $Instructor, old('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
<p class="help-block"></p>
#if($errors->has('Instructor'))
<p class="help-block">
{{ $errors->first('Instructor') }}
</p>
#endif
</div>
</div>
#endif
<div class="form-group">
<label class="required" for="name">Course Title</label>
<input class="form-control {{ $errors->has('title') ? 'is-invalid' : '' }}" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
#if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
#endif
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</form>
</div>
</div>
#endsection
User.php
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsToMany(Role::class, 'role_user');
}
public function isAdmin()
{
return $this->role()->where('role_id', 1)->first();
}
public function roles(){
return $this->belongsToMany('App\Role');
}
CoursesController.php
My create function:
protected function create()
{
{
$Instructor = \App\User::whereHas('role', function ($q) { $q->where('role_id', 2); } )->get()->pluck('title', 'id');
// $courses = Course::all()->pluck('title');
return view('admin.courses.create', compact('courses'));
}
}
The following error is being returned:
Undefined variable: Instructor (View:
C:\xampp\htdocs\test\resources\views\admin\courses\create.blade.php)
I am not sure where I have gone wrong. Thanks for any help.
Referring to my latest comment:
In the controller, you missing pass Instructor to your view:
return view('admin.courses.create', compact('courses', 'Instructor'));

Call to a member function comments() on null

Basically, I want to solve this error
Call to a member function comments() on null
I am trying to make a system on my site whereby users can post,view post comment on posts and reply.
When a user comments on http://127.0.0.1:8000/post/show/6 for example,it is meant to go to http://127.0.0.1:8000/comment/store insert the comments into the database and then show the comments
BUT
what currently happens is that after typing the comment in http://127.0.0.1:8000/post/show/6 it directs to http://127.0.0.1:8000/comment/store it show this error on laravel'S PrettyPageHandler:
Call to a member function comments() on null
I have no clue what I'm doing wrong.
Please help
These are my code:
PostController.php
<?php
// PostController.php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
use Stevebauman\Location\Facades\Location;
class PostController extends Controller
{
protected $fillable = [
'Uploader',
];
public function __construct()
{
return $this->middleware('auth');
}
public function create()
{
return view('post');
}
public function store(Request $request)
{
{
$post = new Post;
$post->title = $request->get('title');
$post->type = $request->get('type');
$post->description = $request->get('description');
$post->body = $request->get('body');
$post->UniqueId = str_random(16);
$post->Uploader = Auth::user()->name;
$post->Language = 'en';
$post->Location=Location::get()->countryCode;
$post->views = 0;
$post->Applauds = 0;
$post->Boos = 0;
$post->Tags = "hey";
if ($request->get('agegroup')) {
$post->agegroup = $request->get('agegroup');
} else {
$post->agegroup ='undefined';
}
$post->AllowComments = 'true';
$post->CommentsBg = 'default';
$post->Visibility = 'globally public';
$post->others = 'embeddable';
$post->save();
return redirect('posts');
}
}
public function index()
{
$posts = Post::all();
return view('index', compact('posts'));
}
public function show($id)
{
$post = Post::find($id);
return view('show', compact('post'));
}
}
CommentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comment;
use App\Post;
class CommentController extends Controller
{
public function store(Request $request)
{
$comment = new Comment;
$comment->body = $request->get('comment_body');
$comment->user()->associate($request->user());
$post = Post::find($request->get('post_id'));
$post->comments()->save($comment);
return back();
}
}
Web.php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('logout', '\App\Http\Controllers\Auth\LoginController#logout');
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin', 'AdminController#index')->name('admin');
Route::get('/upload', 'UploadController#index')->name('upload');
Route::get('/post/create', 'PostController#create')->name('post.create');
Route::post('/post/store', 'PostController#store')->name('post.store');
Route::get('/posts', 'PostController#index')->name('posts');
Route::get('/post/show/{id}', 'PostController#show')->name('post.show');
Route::post('/comment/store', 'CommentController#store')->name('comment.add');
Route::post('/reply/store', 'CommentController#replyStore')->name('reply.add');
Route::match(['get', 'post'], 'imageupload', 'ImageController#Image');
Route::delete('delimage/{filename}', 'ImageController#Image');
post.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Post</div>
<div class="card-body">
<form method="post" action="{{ route('post.store') }}">
<div class="form-group">
#csrf
<label class="label">Post Title: </label>
<input type="text" name="title" class="form-control" required/>
</div>
<label class="label">Post Type </label>
<input type="text" name="type" class="form-control" required/>
<label class="label">Tags </label>
<input type="text" name="tags" class="form-control" required/>
<label class="label">Age-group(optional) </label>
<input type="text" name="agegroup" class="form-control" required/>
<div class="form-group">
<label class="label">Post Description </label>
<textarea name="description" rows="5" cols="20" class="form-control" required></textarea>
</div>
<div class="form-group">
<label class="label">Post Body: </label>
<textarea name="body" rows="10" cols="30" class="form-control" required></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
show.blade.php
<!-- show.blade.php -->
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-body">
<p><b>{{ $post->title }}</b></p>
<p>
{{ $post->body }}
</p>
<hr />
<h4>Display Comments</h4>
#foreach($post->comments as $comment)
<div class="display-comment">
<strong>{{ $comment->user->name }}</strong>
<p>{{ $comment->body }}</p>
</div>
#endforeach
<hr />
<h4>Add comment</h4>
<form method="post" action="{{ route('comment.add') }}">
#csrf
<div class="form-group">
<input type="text" name="comment_body" class="form-control" />
<input type="hidden" name="post_id" value="{{ $post->id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-warning" value="Add Comment" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<table class="table table-striped">
<thead>
<th>ID</th>
<th>Title</th>
<th>Action</th>
</thead>
<tbody>
#foreach($posts as $post)
<tr>
<td>{{ $post->Id }}</td>
<td>{{ $post->title }}</td>
<td>
Show Post
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
I guess the problem is in this part of the CommentController.php code:
$post = Post::find($request->get('post_id'));
$post->comments()->save($comment);
Your assumption is that in the first line Post::find() returns a new Post object. Clearly it doesn't. Why? I can't tell, but probably because the ID doesn't exist?
You could check for this by doing:
$post = Post::find($request->get('post_id'));
if (!is_object($post)) echo "Yeah, I really have a problem here...";
$post->comments()->save($comment);
hi this is because you're not retrieving the inputs correctly ..
class CommentController extends Controller
{
public function store(Request $request)
{
$comment = new Comment;
$comment->body = $request->comment_body;
$comment->user()->associate($request->user());
$post = Post::find($request->post_id);
$post->comments()->save($comment);
return back();
}
}
instead of using $request->get('field_name') just use $request->field_name
EDIT NOTES:
your form method is post .. you might as well use $request->post('field_name') if that function exists .. there are lot of ways to retrieve inputs but as written in my answer i used $request->field_name ..
and why you're getting that error is because the Post::find(null/undefined) returns null ..
You have this error because when you do:
$post = Post::find($request->get('post_id'));
You find nothing, so $post is null. Thus you should find out why. Try to debug and see what $request->get('post_id') contains. Maybe it contains the wrong post id or maybe it has nothing inside of it.
If that the case I think the answer would be to do:
$post = Post::find($request->input('post_id'));
Regards
I got it.
I HAD TO CHANGE post_id to post_Id` and vice-versa as in:
CommentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comment;
use App\Post;
class CommentController extends Controller
{
public function store(Request $request)
{
$comment = new Comment;
$comment->body = $request->comment_body;
$comment->user()->associate($request->user());
$post = Post::find($request->post_id);
$post->comments()->save($comment);
return back();
}
}
And in
show.blade.php
<!-- show.blade.php -->
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-body">
<p><b>{{ $post->title }}</b></p>
<p>
{{ $post->body }}
</p>
<hr />
<h4>Display Comments</h4>
#foreach($post->comments as $comment)
<div class="display-comment">
<strong>{{ $comment->user->name }}</strong>
<p>{{ $comment->body }}</p>
</div>
#endforeach
<hr />
<h4>Add comment</h4>
<form method="post" action="{{ route('comment.add') }}">
#csrf
<div class="form-group">
<input type="text" name="comment_body" class="form-control" />
<input type="hidde" name="post_id" value="{{ $post->Id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-warning" value="Add Comment" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection

Solve "Missing required parameters for [Route: post.show] [URI: post/show/{id}]."

Basically, I want to solve this error
Missing required parameters for [Route: post.show] [URI: post/show/{id}]. (View: C:\xampp2\htdocs\galaxall\resources\views\index.blade.php)
I am trying to make a system on my site whereby users can post,view post comment on posts and reply.
When a usertypes the details of his post in http://localhost:8000/post/create,it is supposed to accept the values,insert it into the database and redirect to http://localhost:8000/posts/show and show a the list of posts ever 'posted' so that the user can view a post can comment and reply to comments about that post.
BUT
what currently happens is that after typing the details for the to-be post in
http://localhost:8000/post/create,it inserts into the db and then shows this error on laravel 'customised' error page:
Missing required parameters for [Route: post.show] [URI: post/show/{id}]. (View: C:\xampp2\htdocs\galaxall\resources\views\index.blade.php)
I have no clue what I'm doing wrong.
Please help
These are my code:
PostController.php
<?php
// PostController.php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use Auth;
use Stevebauman\Location\Facades\Location;
class PostController extends Controller
{
protected $fillable = [
'Uploader',
];
public function __construct()
{
return $this->middleware('auth');
}
public function create()
{
return view('post');
}
public function store(Request $request)
{
{
$post = new Post;
$post->title = $request->get('title');
$post->type = $request->get('type');
$post->description = $request->get('description');
$post->body = $request->get('body');
$post->UniqueId = str_random(16);
$post->Uploader = Auth::user()->name;
$post->Language = 'en';
$post->Location=Location::get()->countryCode;
$post->views = 0;
$post->Applauds = 0;
$post->Boos = 0;
$post->Tags = "hey";
if ($request->get('agegroup')) {
$post->agegroup = $request->get('agegroup');
} else {
$post->agegroup ='undefined';
}
$post->AllowComments = 'true';
$post->CommentsBg = 'default';
$post->Visibility = 'globally public';
$post->others = 'embeddable';
$post->save();
return redirect('posts');
}
}
public function index()
{
$posts = Post::all();
return view('index', compact('posts'));
}
public function show($id)
{
$post = Post::find($id);
return view('show', compact('post'));
}
}
CommentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Comment;
use App\Post;
class CommentController extends Controller
{
public function store(Request $request)
{
$comment = new Comment;
$comment->body = $request->get('comment_body');
$comment->user()->associate($request->user());
$post = Post::find($request->get('post_id'));
$post->comments()->save($comment);
return back();
}
}
Web.php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('logout', '\App\Http\Controllers\Auth\LoginController#logout');
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin', 'AdminController#index')->name('admin');
Route::get('/upload', 'UploadController#index')->name('upload');
Route::get('/post/create', 'PostController#create')->name('post.create');
Route::post('/post/store', 'PostController#store')->name('post.store');
Route::get('/posts', 'PostController#index')->name('posts');
Route::get('/post/show/{id}', 'PostController#show')->name('post.show');
Route::post('/comment/store', 'CommentController#store')->name('comment.add');
Route::post('/reply/store', 'CommentController#replyStore')->name('reply.add');
Route::match(['get', 'post'], 'imageupload', 'ImageController#Image');
Route::delete('delimage/{filename}', 'ImageController#Image');
post.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Post</div>
<div class="card-body">
<form method="post" action="{{ route('post.store') }}">
<div class="form-group">
#csrf
<label class="label">Post Title: </label>
<input type="text" name="title" class="form-control" required/>
</div>
<label class="label">Post Type </label>
<input type="text" name="type" class="form-control" required/>
<label class="label">Tags </label>
<input type="text" name="tags" class="form-control" required/>
<label class="label">Age-group(optional) </label>
<input type="text" name="agegroup" class="form-control" required/>
<div class="form-group">
<label class="label">Post Description </label>
<textarea name="description" rows="5" cols="20" class="form-control" required></textarea>
</div>
<div class="form-group">
<label class="label">Post Body: </label>
<textarea name="body" rows="10" cols="30" class="form-control" required></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
show.blade.php
<!-- show.blade.php -->
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-body">
<p><b>{{ $post->title }}</b></p>
<p>
{{ $post->body }}
</p>
<hr />
<h4>Display Comments</h4>
#foreach($post->comments as $comment)
<div class="display-comment">
<strong>{{ $comment->user->name }}</strong>
<p>{{ $comment->body }}</p>
</div>
#endforeach
<hr />
<h4>Add comment</h4>
<form method="post" action="{{ route('comment.add') }}">
#csrf
<div class="form-group">
<input type="text" name="comment_body" class="form-control" />
<input type="hidden" name="post_id" value="{{ $post->id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-warning" value="Add Comment" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<table class="table table-striped">
<thead>
<th>ID</th>
<th>Title</th>
<th>Action</th>
</thead>
<tbody>
#foreach($posts as $post)
<tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>
Show Post
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
The second parameter of the route function should be an array of parameter name to value pairs, eg
<a href="{{ route('post.show', ['id' => $post]) }}" class="btn btn-primary">
Show Post
</a>
See https://laravel.com/docs/5.8/urls#urls-for-named-routes

Laravel Post Controller Method and PostController not Working

My Laravel blog project the get method of postcontroller is working but post method is not working. I redirect The Post Method. But Accordind to my my code it should return to my Admin page.My Controller Code is
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use App\Category;
class PostController extends Controller
{
public function getBlogIndex() {
return view('frontend.blog.index');
}
public function getSinglePost($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();
return redirect()->route('admin.index')->with(['success' => 'Post Successfully Created']);
}
}
My Routes file
<?php
Route::group(['middleware' => ['web']], function () {
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::get('/blog/post/create', [
'uses' => 'PostController#postCreatePost',
'as' => 'admin.blog.post.create'
]);
});
});
My form is
#extends('layouts.admin-master')
#section('styles')
{!! Html::style('src/css/form.css') !!}
#endsection
#section('content')
<div class="container">
#include('includes.info-box')
<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') ? 'class=has-error' : '' }} value="{{ Request::old('title') }}">
</div>
<div class="input-group">
<label for="author">Author</label>
<input type="text" name="author" id="author" {{ $errors->has('author') ? 'class=has-error' : '' }} value="{{ Request::old('author') }}">
</div>
<div class="input-group">
<label for="category_select">Add Category</label>
<select name="category_select" id="category_select">
<option value="Dummy Category ID">Dummy Category</option>
</select>
<button type="button" class="btn">Add Category</button>
<div class="added-categories">
<ul></ul>
</div>
<input type="hidden" name="categories" id="categories">
</div>
<div class="input-group">
<label for="body">Body</label>
<textarea name="body" id="body" rows="12" {{ $errors->has('body') ? 'class=has-error' : '' }} >{{ Request::old('body') }}</textarea>
</div>
<button type="submit" class="btn">Create Post</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
#endsection
#section('scripts')
{!! Html::script('src/js/posts.js') !!}
#endsection
When I submit My post I shows this
I dont find where the problem is. Plz Help Me
This is an extremely common error and a good one to look out for. Whenever you see:
MethodNotAllowedHttpException in RouteCollection.php
The very first thing you should check is your routes file to make sure you have Route::get or Route::post correctly based on what you are trying to do.
Your issue is that your form sends the data as POST, but your route is GET.
<form action="{{ route('admin.blog.post.create') }}" method="post">
and
Route::get('/blog/post/create', [
'uses' => 'PostController#postCreatePost',
'as' => 'admin.blog.post.create'
]);
Change that to Route::post for it to function correctly.

Categories