Previously, I was trying for a few hours to get my "destroy()" function working. Which would sound ridiculous to most of you but simply putting "method("DELETE")" in the form made it work, which I still don't why.
Now, I'm stuck with the update() function. Once I click update, the page just reloads and does nothing. It doesn't update the info in the database nor does it redirect me back to the index. Here's the code:
Route (web.php):
Route::resource('books', BookController::class);
Form (Edit.blade.php):
#section('content')
<div class="row">
<div class="col-lg-12">
<form action="{{ route('books.update', $book->id) }}" method="POST">
#csrf
#method('PUT')
<div class="mb-3 mt-3">
<label for="exampleFormControlInput1" class="form-label">Book Title</label>
<input type="text" name="title" class="form-control" value="{{ $book->title }}">
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Book Author</label>
<input type="text" name="author" class="form-control" value="{{ $book->author }}" placeholder="Author Name..">
</div>
<div class="btn-group" role="group">
<button type="submit" class="btn btn-block btn-danger">Update</button>
<button type="button" class="btn btn-success">Go Back</button>
</div>
</form>
</div>
</div>
#endsection
Controller (BookController):
public function update(Request $request, Book $book)
{
$validated = $request->validate([
'title' => 'required|max:255|unique:books',
'author' => 'required|max:255'
]);
Book::where('id',$book->id)->update($validated);
return redirect()->route('books.index')->with('message','Book Updated!');
}
What am I doing wrong here? Some help would be appreciated.
I think the problem is in your unique validation. When you don't change the title in the form and hit updated, the unique validation fails. It fails because the title already exists in the database. We have to skip unique validation for the resource that is being updated.
$validated = $request->validate([
'title' => 'required|max:255|unique:books,title,'.$book->id, // <-- change this
'author' => 'required|max:255'
]);
$book->update( $request->only(['title', 'author']) );
Related
I am new to programming especially laravel. I am trying to make a CRUD and have already added example data in prequel (using Docker). I can see the data, but when I´m trying to create new posts with a form I get Code 419 page expired. I know that´s normal and the solution is to add #csrf to the form. But after doing this I get 403 Forbidden. I tried a lot but can´t find a solution to fix it.
I would be really happy if someone could help me fix my problem.
Here is my create.blade.php
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">{{ __('Alle Gerichte') }}</div>
<div class="card-body">
<form action = "/recipe" method="POST">
#csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name">
</div>
<div class="form-group">
<label for="beschreibung">Beschreibung</label>
<textarea class="form-control" id="beschreibung" name="beschreibung" rows="5"></textarea>
</div>
<input class="btn btn-primary mt-4" type="submit" value="absenden">
</form>
<a class="btn btn-primary btn-sm mt-3 float-right" href="/recipe"><i class="fas fa-circle-up"></i>Zurück</a>
</div>
</div>
</div>
</div>
</div>
#endsection
hi is that you have created validation rules
in StoreRecipeRequest
do that
public function authorize()
{
return true;
}
Controller Code will be like this:
public function store(StoreRecipeRequest $request)
{
//dd($request);
$recipe = new Recipe( [
'name' => $request->name,
'beschreibung' => $request->beschreibung,
]);
$recipe->save();
return redirect('/recipe');
}
Also if it's not solved. Then let's try it.
public function store(StoreRecipeRequest $request)
{
$recipe = new Recipe();
$recipe->name = $request->name;
$recipe->beschreibung = $request->beschreibung;
$recipe->save();
return redirect('/recipe');
}
also, can you add in your Recipe Model?
protected $fillable = [
'name',
'beschreibung',
];
I need some advice. I make a view for editing a database that contains textarea form. Unfortunately, when I hit the save button it doesn't give any change to the database. What should I do?
Here's the form in infoupdate.blade.view:
<form action="{{ route('updateinfo') }}" method="POST">
#method('PUT')
#csrf
<div class="intro-y box p-5">
<div>
<div class="mt-3"> <label for="by" class="form-label">Oleh</label> <input name="by" id="by" type="text" class="form-control" value="{{ auth()->user()->username }}" readonly></div>
</div>
<div>
<div class="mt-3"> <label for="selectedTime" class="form-label">Waktu Pengumuman</label> <input name="selectedTime" id="selectedTime" type="text" class="form-control" value="{{ $infos->selectedTime }}" readonly></div>
</div>
<div class="mt-3">
<label class="pb-8" for="contentInfo">Isi Pengumuman</label>
<textarea name="contentInfo" id="contentInfo" class="w-full border-2" rows="10" cols="100">{{ $infos->contentInfo }}</textarea>
</div>
<div class="text-right mt-5">
<button type="submit" class="btn btn-primary w-24">Simpan</button>
</div>
</div>
</form>
Also the update function in DBIController.php:
public function update(Request $request){
$request->validate([
'contentInfo' => 'required|min:16'
]);
DB::table('infos')->where('id', $request->id)->update([
'contentInfo' => $request->contentInfo
]);
return redirect()->route('DBI')->with('message','Data Pengumuman Berhasil di Update');
}
and the route for displaying the form and the route for updating database in web.php :
Route::get('/infoeditor/{id}',[DBIController::class, 'edit'])->middleware('admin')->name('infoeditor');
Route::put('/updateinfo',[DBIController::class, 'update'])->middleware('admin')->name('updateinfo');
on your update route, you need to add /{id}
Route::put('/updateinfo/{id}',[DBIController::class, 'update'])->middleware('admin')->name('updateinfo');
Then, in your controller method, you will add $id as parameter
public function update(Request $request, int $id)
{
$request->validate([
'contentInfo' => 'required|min:16'
]);
DB::table('infos')->where('id', $id)->update([
'contentInfo' => $request->contentInfo
]);
return redirect()->route('DBI')->with('message','Data Pengumuman Berhasil di Update');
}
And in your view, you update the action url with $id
<form action="{{ route('updateinfo', ['id' => $infos->id]) }}" method="POST">
i am creating crud in laravel.i ran into the problem with Missing required parameters for [Route: employees.update] [URI: employees/{employee}]. (View: C:\xampp\htdocs\jbs\resources\views\employees\edit.blade.php)what i tried so far i attach below. i added the model view controller below
Edit Blade Page
#extends('employees.layout')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Employee</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('employees.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('employees.update',$employees->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>StudentName:</strong>
<input type="text" name="name" value="{{ $employees->studname }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Course</strong>
<input type="text" name="name" value="{{ $employees->course }}" class="form-control" placeholder="course">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Fee</strong>
<input type="text" name="name" value="{{ $employees->fee }}" class="form-control" placeholder="fee">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
Controller
public function edit(Employee $employees)
{
return view('employees.edit',compact('employees'));
}
public function update(Request $request, Employee $employees)
{
$request->validate([
'studname' => 'required',
'course' => 'required',
'fee' => 'required',
]);
$employees->update($request->all());
return redirect()->route('employees.index')
->with('success','Employee updated successfully');
}
Model
protected $fillable = [
'studname', 'course','fee',
];
}
routes
Route::resource('employees','App\Http\Controllers\EmployeeController');
Your route parameter is employee not employees. Route parameter names for resource routes are singular by default. The route is actually like this:
employees/{employee}
So in your edit method of your Controller you are using the wrong parameter name so you are getting a new instance of Employee instead of the Implicit Route Model Binding that would inject the instance based on the route parameter, you need to match the typehinted parameter name to the route parameter name:
// {employee}
public function edit(Employee $employee)
{
...
return view(...., ['employee' => $employee]);
}
Now in the view you will have your actual existing Employee instance that you can use to generate the URL to the route instead of an empty Employee that does not have an id which was returning null:
{{ route('employees.update', ['employee' => $employee->id]) }}
// or
{{ route('employees.update', ['employee' => $employee]) }}
The route system can get the correct key from the model instance.
You should pass $employees->id as a hidden input field.
Make your route as
Route::post('employee/update', 'YourController#yourFunction');
and in the edit page the form action should look like
<form action="{{ route('employees.update'}}" method="POST">
make a hidden input field for passing id
<input type="hidden" name="id" value="{{$employees->id}}"></input>
You should pass the route name to the route function like this
route('route name',parameter1)
Example :
Route::post('employee/update/{id}' ,YourController#update)->name('update_employee');
<form action="{{ route('update_employee',$employees->id) }}" method="POST">
#csrf
...
I have a page that shows a topic, And underneath the topic there are replies. In between these 2, there is a text field where the user can type a reply. The problem is. I get the error in the title when I try to post a reply. I used the same method on a previous project of mine and there it works just fine. How can I solve this?
Here are the files
topic.blade.php
<div class="card">
<div class="card-content">
<span class="card-title">Leave a Reply</span>
<div class="row">
<form method="POST" action="{{ route('createreply') }}">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
<input type="hidden" name="post_id" value="{{ $topic->id }}">
<div class="form-group col s12">
<textarea id="message-body textarea1" class="form-control materialize-textarea" name="reply" placeholder="Type your reply"></textarea>
</div>
<div class="col s12">
<button class="btn right blue-grey darken-4" type="submit">Reply</button>
</div>
</form>
</div>
</div>
</div>
ReplyController.php (Store method)
public function store(Request $request)
{
Reply::create($request->input());
return back();
}
Web.php
route::post('/reply/create', 'ReplyController#store')->name('createreply');
Thank you in advance!
<textarea id="message-body textarea1" class="form-control materialize-textarea" name="reply_text" placeholder="Type your reply"></textarea>
Try this. The name attribute was not reply_text as you have in db
I have seen this post, however I don't believe it is relevant to my issue because I believe I am correctly passing post data through a post route.
Here is the relevant route code:
Route::get('/pass', 'PageController#pass');
Route::post('/pass/{request}',['uses' => 'PageController#passController']);
I would like to have one controller method for the 'pass' page, but to isolate the issue I have separated them.
Here are the relevant methods in PageController.php:
public function pass(){
return view('pass')->with(array(
'title'=>'Create A Pass'
));
}
public function passRequest($request){
$data['request'] = $request;
$validator = Validator::make($request->all(), [
'studentID' => 'required|max:255',
'teacherID' => 'required|max:255',
'destination' => 'required|max:255',
]);
if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}
$pass = new Pass;
$pass->student = DB::table('users')->where('studentID', $request->studentID)->first()->id;
$pass->teacher = DB::table('users')->where('teacherID', $request->teacherID)->first()->id;
$pass->destination = $request->destination;
$pass->save();
return view('home')->with(array(
'title'=>'Home',
'success'=>'null'
));
}
I used the method stated here in order to pass data to the controller. If this is bad practice/obsolete I'm open to any suggestions.
This is the form in the 'pass' page responsible for sending the post data
<form action="{{ url('pass') }}" method="POST" class="form-horizontal">
{!! csrf_field() !!}
<fieldset>
<!-- Text input-->
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="studentID">Student ID</label>
<div class="col-md-3">
<input id="studentID" name="studentID" type="text" class="form-control input-md">
</div>
</div>
</div>
<!-- Text input-->
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="teacherID">Teacher ID</label>
<div class="col-md-3">
<input id="teacherID" name="teacherID" type="text" class="form-control input-md">
</div>
</div>
</div>
<!-- Text input-->
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="destination">Destination</label>
<div class="col-md-3">
<input id="destination" name="destination" type="text" class="form-control input-md">
</div>
</div>
</div>
<div class="container">
<div class="form-group">
<div class="col-sm-offset-4 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-check"></i> Create Pass
</button>
</div>
</div>
</div>
</fieldset>
</form>
On submission of this form I get the MethodNotAllowedHttpException Exception.
If a stack trace of the error would be helpful, please let me know. If there are any suggestions on style, I'm open to that as well.
This form tag will generate a POST request to the URL /pass:
<form action="{{ url('pass') }}" method="POST" class="form-horizontal">
Your routes file does not allow that. It only allows GET requests to that url, but POST requests to /pass/{request}.
Not sure if its just a copy/paste mistake, but your POST route is set up to call PageController#passController method, but the method you shared from your controller is named passRequest. Those will need to match also.
In addition to what Jeff Lambert pointed out, you should not put the {request} variable in the route.
You should remove that and have laravel inject the Request object for you.
Import the Request class if you haven't already at the top of the class.
use Illuminate\Http\Request;
And your function should look like the following...
public function passRequest(Request $request)
{
...
}
If you have additional parameters to pass through the URL, then you may add them to the route, and add the arguments to the method after Request $request. Laravel will figure out what to do with it.
try this one...
Route::post('/pass/post','PageController#passController')->name('post_insert');
in your html form change to ...
<form action="{{ route('post_insert') }}" method="POST" class="form-horizontal">
change it also ...
public function passRequest(Illuminate\Http\Request $request){
....