I have created an employee management system in Laravel and I want to add a user. Admin can edit, add, and delete the user. Edit and delete are fine but the problem with the additional user, the form it's not getting submitted to add the user gives me error!
this is create.blade.php file
#extends ('layouts.master')
#section('title')
Add Employee | Admin
#endsection
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="card-header">
<h3>Add Employee.</h3>
</div>
<div class="card-body">
<div class="col-md-6">
<form action="/create" method="POST">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="form-group">
<label>Name</label>
<input type="text" name="name" value="{{ $users->name }}" class="form-control">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" name="lastname" value="{{ $users->lastname }}" class="form-control">
</div>
<div class="form-group">
<label>Phone</label>
<input type="text" name="phone" value="{{ $users->phone }}" class="form-control">
</div>
<div class="form-group">
<label>Email address</label>
<input class="form-control" type="email" value="{{ $users->email }}"name="email" id="email"> </div>
<div class="form-group">
<label>Job Title</label>
<input type="text" name="jobtitle" value="{{ $users->jobtitle }}" class="form-control">
</div>
<div class="form-group">
<label>Department</label>
<input type="text" name="department" value="{{ $users->department }}" class="form-control">
</div>
<button type="submit" class="btn btn-success"> Save </button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
#endsection
dashboardcontroller.php
<?php
namespace App\Http\Controllers\Admin;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller
{
public function registered()
{
$users = User::all();
return view('admin.register')->with('users', $users);
}
public function registeredit(Request $request, $id)
{
$users = User::findOrFail($id);
return view('admin.register-edit')->with('users',$users);
}
public function registerupdate(Request $request, $id)
{
$users = User::find($id);
$users->name = $request->input('name');
$users->lastname = $request->input('lastname');
$users->phone = $request->input('phone');
$users->email = $request->input('email');
$users->jobtitle = $request->input('jobtitle');
$users->department = $request->input('department');
$users->update();
return redirect('/role-register')->with('status','Your Data is Updated');
}
public function registerdelete($id)
{
$users = User::findOrFail($id);
$users->delete();
return redirect('/role-register')->with('status','Your Data is Deleted');
}
public function index()
{
$users=DB::table('users')->paginate(10);
//$users = User::all();
return view('admin.dashboard',['users' => $users]);
}
public function department()
{
$users = User::all()->unique('department');
return view('admin.department')->with('users', $users);
}
public function create()
{
$users = new User();
//$user->save();
return view('admin.create')->with('users', $users);
}
}
web.php
<?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('/home', 'HomeController#index')->name('home');
Route::group(['middleware' => ['auth','admin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('/role-register','Admin\DashboardController#registered');
Route::get('/role-edit/{id}', 'Admin\DashboardController#registeredit');
Route::put('/role-register-update/{id}', 'Admin\DashboardController#registerupdate');
Route::delete('/role-delete/{id}', 'Admin\DashboardController#registerdelete');
Route::get('/dashboard', "Admin\DashboardController#index");
Route::get('/department', 'Admin\DashboardController#department');
Route::get('/create', 'Admin\DashboardController#create');
});
It gives me this error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD. I have searched for this error but I'm not getting any results. Thanks in advance
Change your route get method to post
Route::post('/create', 'Admin\DashboardController#create');
Also change your controller
public function create(Request $request)
{
$users = new User;
$users->name = $request->name;
$users->lastname = $request->lastname;
......add other input rows here----
$users->save();
return view('admin.create')->with('users', $users);
}
Related
I have a problem with save-aboutus page. I am pretty new to Laravel, I don't know how can I fix this.
I get this error:
The GET method is not supported for this route. Supported methods:
PUT.
routes(web.php):
Route::group(['middleware' => ['auth','admin']], function() {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('/role-register',[App\Http\Controllers\Admin\DashboardController::class, 'registered']);
Route::get('/role-edit/{id}',[App\Http\Controllers\Admin\DashboardController::class, 'registeredit']);
Route::put('/role-register-update/{id}',[App\Http\Controllers\Admin\DashboardController::class, 'registerupdate']);
Route::delete('/role-delete/{id}',[App\Http\Controllers\Admin\DashboardController::class, 'registerdelete']);
Route::get('/abouts',[App\Http\Controllers\Admin\AboutusController::class, 'index']);
Route::post('/save-aboutus', [App\Http\Controllers\Admin\AboutusController::class, 'store']);
});
Controller:
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$aboutus = new Abouts;
$aboutus->$title = $request->input('title');
$aboutus->$subtitle = $request->input('subtitle');
$aboutus->$descreption = $request->input('description');
$aboutus->save();
return redirect('/abouts')->with('success','Success');
}
}
and blade.php
<form action="/save-aboutus" method="POST">
{{ csrf_field() }}
<div class="modal-body">
<div class="form-group">
<label for="recipient-name" class="col-form-label">Title:</label>
<input type="text" name="title" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Sub-title</label>
<input type="text" name="subtitle" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">Description</label>
<textarea class="form-control" name="description" id="message-text"></textarea>
</div>
</div>
</form>
You just need to add a GET route if you want to access it with GET method:
Route::get('/save-aboutus', /* handler */);
For example if there is a form which creates customers, and when you filled the form and submit the page should redirect and show the data which was inserted to that form by a specific individual id. in my case it redirects to a page and views all the data in the database.
Here's my Web.php
Route::get('customers','CustomersController#index');
Route::get('customers/create','CustomersController#create');
Route::post('customers','CustomersController#store');
Route::get('customers/{customer}','CustomersController#show');
Here's my Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Customer;
use App\Company;
class CustomersController extends Controller
{
public function index(){
$customers = Customer::all();
return view('customers.index',compact('customers'));
}
public function create(){
$companies = Company::all();
return view ('customers.create',compact('companies'));
}
public function store()
{
$data = request()->validate([
'name'=>'required | min:3',
'email'=>'required | email',
'active'=>'required ',
'company_id'=>'required',
]);
Customer::create($data);
return redirect('customers');
}
// * Route Model binding
public function show(Customer $customer){
return view('customers.show',compact('customer'));
}
}
Here's my Customer Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $guarded = [];
public function getActiveAttribute($attribute){
return [
0 => 'Inactive',
1 => 'Active',
] [$attribute];
}
public function scopeActive($query){
return $query->where('active',1);
}
public function scopeInactive($query){
return $query->where('active',0);
}
public function company()
{
return $this->belongsTo(Company::class);
}
}
Here's my Create Blade
#extends('layouts')
#section('title','Add New Customer')
#section('content')
<div class="row">
<div class="col-12">
<h1>Add New Customer</h1>
</div>
</div>
<div class="row">
<div class="col-12">
<form action="/customers" method="POST" >
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{old('name')}}">
<div>{{$errors->first('name')}}</div>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" name="email" value="{{old('email')}}">
<div>{{$errors->first('email')}}</div>
</div>
<div class="form-group">
<label for="">Customer Status</label>
<select name="active" id="active" class=" form-control">
<option value="" disabled>Select Customer Status</option>
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
</div>
<div class="form-group">
<label for="company_id">Company</label>
<select name="company_id" id="company_id" class=" form-control">
#foreach ($companies as $company)
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class=" btn btn-dark">Add Customer</button>
#csrf
</form>
</div>
</div>
#endsection
Here's my show blade
#extends('layouts')
#section('title','Details for ' . $customer->name)
#section('content')
<div class="row">
<div class="col-12">
<h1>Details for {{ $customer->name }}</h1>
</div>
</div>
<div class="row">
<div class="col-12">
<p><strong>Name : </strong> {{ $customer->name }}</p>
<p><strong>Email : </strong> {{ $customer->email }}</p>
<p><strong>Company : </strong> {{ $customer->company->name }}</p>
<p><strong>Status : </strong> {{ $customer->active }}</p>
</div>
</div>
#endsection
Redirect to the show route rather than the customers index route:
$customer = Customer::create($data);
return redirect('customers/' . $customer->id);
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
Basically, I want to show a posts comment.
Whatt is supposed to happen is that,when a user goes to view a post using a url such as http://localhost:8000/post/show/1,their supposed to be able to to type a comment on a post,press 'submit' ,the comment data inserted into the database,the page should reload and the comments on that post should be shown including the comment just commented by the user
BUT
what currently happens is that after typing the comment in http://localhost:8000/post/show/1 it inserts the comment data into the database,reloads the page but does not show any comment on that page.
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
Post.php
<?php
// Post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable')->whereNull('parent_id');
}
}
Comment.php
<?php
// Comment.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id');
}
}
DBSCHEMA for comments table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
// create_comments_table
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('parent_id')->unsigned();
$table->integer('applauds')->unsigned();
$table->integer('boos')->unsigned();
$table->text('body');
$table->integer('commentable_id')->unsigned();
$table->string('commentable_type');
$table->string('No of flags');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Update the PostControllerfunction as below:
public function show($id)
{
$post = Post::where('id', $id)->with('comments.user')->first();
return view('show', compact('post'));
}
It should get comments and user details attached
On the Post Model, update comments function
public function comments()
{
return $this->hasMany(Comment::class, 'parent_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