I've implemented a modal type Update and Delete functions in my website but it always return Too few arguments to function App\Http\Controllers\AdminController::destroy(), 1 passed in D:\SUDRTest\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected
it is also the same for the Update function as well
Here is my route for the CRUD
Route::resource('papers', AdminController::class)->only(['edit', 'update', 'destroy']);
Here is the View
<li class="pdfpaperInfo">
<div class="colpdf col-1" data-label="Title:">{{ $paper->PaperTitle }}</div>
<div class="colpdf" data-label="Paper Type:">{{ $paper->PaperType }}</div>
<div class="colpdf" data-label="College:">{{ $paper->College }}</div>
<div class="colpdf" data-label="Author(s):">{{ $paper->Authors }}</div>
<div class="colpdf" data-label="Date Published:">{{ $paper->DatePublished }}</div>
<div class="pdfbtnCont">
<button class="pdfBtn redBtn" onclick="location.href='{{route('MyProfile')}}'">Back</button>
<button class="pdfBtn redBtn" id="modalOneBtn" onclick="location.href='{{route('papers.edit', $paper->PaperID)}}'">Update</button>
<button class="pdfBtn redBtn" id="modalTwoBtn">Delete</button>
</div>
</li>
<div id="modalOne" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="m1Close close">×</span>
<div class="modalinfoCont">
<h2>Update Paper</h2>
#include('admin.updatepaper')
</div>
</div>
</div>
<div id="modalTwo" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="m2Close close">×</span>
<div class="modalTwoCont modalinfoCont">
<h2>Delete Paper</h2>
<br>
Are you sure you want to delete this paper?
<br>
<br>
<div class="modalbtnCont">
<form method="POST" action="{{route('papers.destroy', $paper->PaperID) }}">
#csrf
#method('DELETE')
<button class="redBtn" type="submit">Yes</button>
</form>
<button class="redBtn" type="submit">No</button>
</div>
</div>
</div>
</div>
</div>
and the controller
public function destroy(Papers $paper, $PaperID)
{
$paper=Papers::find($PaperID);
$paper->delete();
return redirect()->back();
}
public function edit(Papers $paper, $PaperID)
{
$paper=Papers::find($PaperID);
return view('admin.updatepaper',compact('paper'));
}
public function update(Request $request,Papers $paper, $PaperID )
{
$request->validate([
'PaperTitle' => 'required',
'PaperType' => 'required',
'file' => [
'required',
File::types('pdf')
->max(12 * 1024),
],
]);
$paper=new Papers();
$file=$request->file;
$filename=time().'.'.$file->getClientOriginalExtension();
$request->file->move('assets', $filename);
$paper->file=$filename;
$paper->DatePublished=$request->DatePublished;
$paper->PaperTitle=$request->PaperTitle;
$paper->PaperType=$request->PaperType;
$paper->Authors=$request->Authors;
$paper->update();
return redirect()->back();
}
I've tried not to do it in modal form and still it kept on displaying the same error and I don't know what is the missing parameter since it doesn't tell me
You need to take another look at route-model binding.
Laravel will by default do the Papers::find($paperID) and pass the Papers model as the Papers $papers argument to your methods.
So the destroy method should be:
public function destroy(Papers $paper)
{
$paper->delete();
return redirect()->back();
}
Of course you can disable route-model binding and do your own thing but it doesn't seem necessary here.
Its not clear what you intend to do in the update method. If you want to create a new paper on update and keep the old one then change $paper->update() to $paper->save() and you should be good. But if you want to do an actual update you should do something like this:
update(Papers $paper, Request $request) {
// validate
$paper->DatePublished=$request->DatePublished;
// update other fields
$paper->save();
return redirect()->back();
}
Related
I've made a blog and now I'm trying to implement a comment section. I want it so that when the user tries to post, it's saves the comment and redirects the user to the same page. But when I write a comment and try to post it, the application redirects me to a different page. I'm learning how to make a blog with laravel, so I don't know when to use url and when to use routes. Here's the code that I've written.
#auth
<div class="card ml-5 col-lg-8">
<ul class="list-group list-group-horizontal">
<h5 class="list-group-item active">
Comments
<h5>
<div class="card-body">
<form method="post" action="{{url('save-comment/'.Str::slug($blog->title).'/'.$blog->id)}}">
#csrf
<textarea name="comment" class="form-control py-5"></textarea>
<input type="submit" class="btn btn-primary mt-3">
</div>
</ul>
</div>
#endauth
<div class="card ml-5 col-lg-8">
<h5 class="card-header mb-4">Comments<span class="badge badge-info ml-2"> {{count($blog->comments)}}</span></h5>
<div class="card-body mt-3">
#if($blog->comments)
#foreach($blog->comments as $comment)
<blockquote class="blockquote">
<p class="mb-0">{{$comment->comment}}</p>
<footer class="blockquote-footer">Username</footer>
</blockquote>
<hr>
#endforeach
#endif
</div>
</div>
BlogController :
function save_comment(Request $request,$slug,$id)
{
$request->validate([
'comment'=>'required',
]);
$data = new Comment;
$data->user_id=$request->user()->id;
$data->post_id=$id;
$data->comment=$request->comment;
$data->save();
return back();
}
Routes :
Route::get('/blog/', [App\Http\Controllers\BlogController::class, 'index'])->name('blog');
Route::get('blogs/{slug}','App\Http\Controllers\BlogController#getArticles')->name('article.show');
Route::get('blog.update/{id}','App\Http\Controllers\BlogController#edit');
Route::put('blog.update/{id}','App\Http\Controllers\BlogController#update');
Route::post('save_comment/{slug}/{id}','App\Http\Controllers\BlogController#save_comment')->name('save_comment');
Route::get('/admin/blog', 'App\Http\Controllers\BlogController#getBlog')->name('admin.blog');
If there's someone willing to assist come up with a solution to this problem, please assist me. I think the problem lies where I've written the url lies. When I change the url to route, it gives me an error of route not defined.
Route::resource('/blog','App\Http\Controllers\BlogController');
It redirects you to an empty page because you made a mistake on the url of your route. In your web.php file, your route is :
Route::post('save_comment/{slug}/{id}', 'App\Http\Controllers\BlogController#save_comment')->name('save_comment');
While in your form you wrote save-comment/ :
<form method="post" action="{{url('save-comment/'.Str::slug($blog->title).'/'. $blog->id)}}">
The error is due to this. I therefore advise you to modify the action in your form like this save_comment/:
<form method="post" action="{{url('save_comment/'.Str::slug($blog->title).'/'. $blog->id)}}">
This should be fixed !
Please change your code like this and check...
action="{{route('save_comment', $blog->id])}}"
Route::post('save_comment/{id}','App\Http\Controllers\BlogController#save_comment')->name('save_comment');
**BlogController**
function save_comment($id, Request $request)
{
$request->validate([
'comment'=>'required',
]);
$data = new Comment;
$data->user_id=$request->user()->id;
$data->post_id=$id;
$data->comment=$request->comment;
$data->save();
return back();
}
I am trying to display a ForEach loop on my view that shows each code type. That part works. I am having trouble figuring out how to display a count of how many pieces of code (stored in another table) exist for each code type. I don't understand how to use the id of the code_type to query the code_type_selected table and to return that count.
In my CodeType model
protected $fillable = [
'name',
'color',
'icon'
];
public function codeTypesSelected() {
return $this->hasMany(CodeTypesSelected::class, 'code_type_id', 'id');
}
In my CodeTypeSelected model
protected $fillable = [
'code_type_id',
'code_id',
];
public function codeTypes() {
return $this->belongsTo(CodeType::class, 'id', 'code_type_id');
}
In my controller
public function index(Request $request)
{
$codeTypes = CodeType::latest()->get();
$codeTypesSelected = CodeTypesSelected::latest()->get()->groupBy('code_type_id');
//dd($codeTypesSelected);
return view('code_layout.index', compact('codeTypes', 'codeTypesSelected'));
}
And in my view
<!-- /. ROW -->
<div class="row">
#foreach($codeTypes as $codeTypeItem)
<div class="col-md-3 col-sm-12 col-xs-12">
<div class="panel panel-primary text-center no-boder bg-color-{{ $codeTypeItem->color }}">
<div class="panel-left pull-left {{ $codeTypeItem->color }}">
<i class="fa fa-{{ $codeTypeItem->icon }} fa-5x"></i>
</div>
<div class="panel-right">
<h3>
{{ count($codeTypesSelected) }}
</h3>
<strong> {{ $codeTypeItem->name }} </strong>
</div>
</div>
</div>
#endforeach
</div>
<!-- /. ROW -->
Have you tried this
CodeType::latest()->withCount('codeTypesSelected')->get()
I'm trying to create a Laravel Notification. I created a table called send_profiles. When a Candidate is logged in and searching through jobs, he can send his job profile to the employer. All of that data is in a table called job_seeker_profiles. I'm developing a Job Search type of application.
I created a new Notification class called SendProfile.php:
public function toDatabase($notifiable)
{
$user = Auth::user();
return [
'user_id' => Auth::user()->id,
'employer_profile_id' => DB::table('send_profiles')->where('user_id', $user->id)->orderBy('id', 'desc')->offset(0)->limit(1)->get('employer_profile_id'),
];
}
I don't know the best way to go about this but anyway this is my route.
web.php:
Route::get('/admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}/send-profile', 'AdminEmployerJobPostsController#sendProfile')->name('admin.employer.post-a-job.show.send-profile')->middleware('verified');
AdminEmployerJobPostsController.php:
public function sendProfile($employerId, $jobPostId)
{
$user = Auth::user();
$jobSeekerProfile = JobSeekerProfile::all()->where('user_id', $user->id)->first();
$employerProfile = EmployerProfile::limit(1)->where('id', $employerId)->get();
$jobPosts = JobPosts::all();
$jobPost = JobPosts::findOrFail($jobPostId);
$user->sendProfile()->create();
$employerProfile->notify(new SendProfile());
return back()->with('send-profile', 'Your Profile has been sent!');
}
This is my error:
Missing required parameters for [Route: admin.employer.post-a-job.show.send-profile] [URI: admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}/send-profile]. (View: /Applications/XAMPP/xamppfiles/htdocs/highrjobs/resources/views/admin/employer/post-a-job/show.blade.php)
show.blade:
#extends('layouts.admin')
#section('pageTitle', 'Create a User')
#section('content')
#include('includes.job_seeker_search_employers')
<!-- The Modal -->
<div class="modal" id="myModal5">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">{{ $jobPost->job_title }}</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<h5>{{ $jobPost->job_description }}</h5>
</div>
<!-- Modal footer -->
<div class="modal-footer">
{!! Form::open(['method'=>'POST', 'action'=>'AdminEmployerJobPostsController#sendProfile', 'files'=>true, 'style'=>'width: 100%;']) !!}
<div class="form-group">
{!! Form::hidden('user_id', Auth::user()->id, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::hidden('employer_profile_user_id', $employerProfile->id, ['class'=>'form-control']) !!}
</div>
<div class="row">
<div class="col">
{!! Form::button('Back', ['class'=>'btn btn-danger btn-block float-left', 'data-dismiss'=>'modal']) !!}
</div>
<div class="col">
{!! Form::submit('Send Profile', ['class'=>'btn btn-primary btn-block float-right']) !!}
{!! Form::close() !!}
</div>
</div>
<br><br><br><br>
</div>
</div>
</div>
</div>
#stop
If I remove the form, I at least don't get an error. So I actually think there is an issue with the form.
To be clear, all I want is to insert the user_id and the employer_profile_id into the send_profiles table and then send a notification to the employer.
Your route specifies a GET request to a URL that contains certain parameters:
/admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}/send-profile
Your form is using AdminEmployerJobPostsController#sendProfile as an action; this is translated into a URL by searching the route list, and choosing what Laravel thinks is most appropriate. Since you haven't passed anything to fill the employerId and jobPostId parameters, you're getting this error when the URL is generated.
Even if you could get the URL generated, you'd have a problem because your form is sending a POST request to this GET route.
What you need to do is ensure you have a POST route pointing to a new controller method. You won't pass any parameters to this route in the URL, so your controller method will only typically accept a Request object as a parameter. Second thing you should do is specify your form's target more accurately. Pass in the route name instead of making it guess.
public function sendProfile(Request $request)
{
// you get this here so no need to pass it in the form
$user = Auth::user();
// your relations should be set up so you don't need to do this:
// $jobSeekerProfile = JobSeekerProfile::all()->where('user_id', $user->id)->first();
// instead do this:
$jobSeekerProfile = $user->jobSeekerProfile();
// a simple find is much neater than what you had
$employerProfile = EmployerProfile::find($request->job_seeker_profile_user_id);
// not sure why this is here?
$jobPosts = JobPosts::all();
// also your form isn't passing a job post ID
$jobPost = JobPosts::findOrFail($request->jobPostId);
// ??? creating an empty something?
$user->sendProfile()->create();
$employerProfile->notify(new SendProfile());
return back()->with('send-profile', 'Your Profile has been sent!');
}
I am quite new to Laravel
I have two views
Book
Read
The Book View displays a single book
<section class="cont-readingone">
<div class="container">
<div class="row row-grid">
<div class="col-md-6">
<div class="row">
<div class="col-md-6">
<div class="cont-reading-image">
<img src="{{ $book->image_url }}" alt="trending image" />
</div>
</div>
<div class="col-md-6">
<div class="out-box">
<h2>{{ $book->name }}</h2>
<h3>{{ $book->author->name }}</h3>
<br>
Start Reading<br><br>
<img src="\images\cart-buy.png" width="13px"/> Buy
</div>
</div>
</div>
</div>
In my controller, I was able to achieve it using
public function show(Book $book) {
$relatedBooks = Book::where('author_id', $book->author_id)
->where('id', '!=', $book->id)
->get();
return view('book')->with('book', $book)->with('relatedBooks', $relatedBooks);
}
In my web.php
Route::get('/books/{book}', [BooksController::class, 'show'])->name('book');
What I am trying to achieve is that, when I click Start Reading on
the Single Book Page, it takes me to another view page (Read) but it takes the book id that I clicked.
In the Read View I have this code,
<script>
"use strict";
document.onreadystatechange = function () {
if (document.readyState == "complete") {
window.reader = ePubReader("{{ $book->epub_url }}", {
restore: true
});
}
};
</script>
My problem is that I don't know how to take the id of the book that I
click and Pass it to the Read View
I will be glad if someone can explain the logic to me as I am confused.
To do via POST
//Book View
//change Start Reading to
<form action="{{ route("your.route.to.read") }}" method="POST">
#csrf
<input name="book_id " value ={{$book->id}} hidden>
<button type="submit">Start Reading</button>
</form>
//your Route will be
Route::get('/read',YourReadController#yourFunction)->name('your.route.to.read');
//your controller will be
public function yourFunction(Request $request)
{
//book id is in $$request->book_id
//your operation here
return view('read')->with('data',$dataYouWantToSend);
}
To do via GET
//Book View
//change Start Reading<br><br> to
Start Reading
//route for get will be
Route::get('/read/{book_id}',YourReadController#yourFunction)->name('your.route.to.read');
//your countroller will be
public function yourFunction($book_id)
{
//book id is in $book_id
//your operation here
return view('read')->with('data',$dataYouWantToSend);
}
i'm learning laravel for now , i'm trying to build a crud application how i got the url with a question mark how i can remove it from the url
the url that i got is like ..../blogs?1
here is the view
#extends ('layouts.app')
#section('content')
<div class="row">
#foreach($blogs as $blog)
<div class="col-md-6">
<div class="card">
<div class="card-header">
{{$blog -> title}}
</div>
<div class="card-body">
{{$blog->content}}
</div>
</div>
</div>
</div>
#endforeach
#endsection
<?php
Route::get('/', function () {
return view('welcome');
});
Route::name('blogs_path')->get('/blogs','BlogController#index');
Route::name('create_blog_path')->get('/blogs/create','BlogController#create');
Route::name('store_blog_path')->post('/blogs','BlogController#store');
Route::name('blogs_path1')->get('/blogs/{id}','BlogController#show');
Route::name('edit_blog_path')->get('/blogs/{id}/edit','BlogController#edit');
how can i fix this , thank you in advance
Because the second argument in route('blogs_path', $blog->id) is parameter.
try this:
Routes:
Route::name('blogs_path')->get('/blogs/{id}/','BlogController#index');
Controller:
public function index(Request $request, $id)
{
...
}
You made a mistake in the routing of the template Blade.
{{ route('blogs_path1', ['id' => $blog->id]) }}