Currently, I am working on a blog. But when I try to update a post, it's not getting updated and no error is reproduced.
What's wrong with the code? Anyone, please give me some suggestions to improvise my code. I want to understand what's the problem. So, anybody can explain in detail then I will really appreciate this thing.
PostController code:-
public function update(Store $request,Post $post)
{
$data = $request->validated();
$post = $post->update($data);
return redirect('/index')->withMessage('successfully published');
}
View page code:-
<form method="POST" action='/update'>
#csrf
<table class="table table table-striped table">
<div class="form-group">
<label for="name" class="col-form-label text-md-right">Title</label><br>
<input required="required" placeholder="Enter title here" class="" type="text" name="title" class="" value="{{$data->title}}" required/>
</div>
<div class="form-group">
<label for="name" class="col-form-label text-md-right">Description</label>
</div>
<div>
<textarea name="description" class="" required>{{$data->description}}
</textarea></div>
<div>
<input type="submit" name="update" class="btn btn-success" value="Update" required />
</div>
</table>
</form>
Route file code :-
Route::get('posts/edit/{edit}',[PostController::class,'edit']);
Route::post('/update',[PostController::class,'update']);
Change your Route update to this :
Route::post('posts/update',['as'=>'update','uses'=>'PostController#update']);
And in View Page
<form method="POST" action="{{ route('update') }}">
Related
Actually, I editing the post in my blog but when I tried it cant update instead of it creating a new record. I also tried update() function but it can't work. What's wrong with the code? Anyone, please give me some suggestions to improvise my code. I want to understand what's the problem. So, anybody can explain in detail then I will really appreciate this thing.
My Code:
PostController Code:
public function update(Request $request,Post $post)
{
$post->user_id=$request->user()->id;
$post->title=$request->title;
$post->Description=$request->description;
$post->save();
return redirect('/show');
}
Route file Code:
Route::get('posts/edit/{edit}',[PostController::class,'edit']);
Route::post('/update',[PostController::class,'update']);
View Page Code:
<form method="POST" action='/update'>
#csrf
<table class="table table table-striped table">
<div class="form-group">
<label for="name" class="col-form-label text-md-right">Title</label><br/>
<input placeholder="Enter title here" type="text" name="title" value="{{$data->title}}" required/>
</div>
<div class="form-group">
<label for="name" class="col-form-label text-md-right">Description</label>
</div>
<div>
<textarea name="description" required>{{$data->description}}
</textarea></div>
<div>
<input type="submit" name="update" class="btn btn-success" value="Update" required />
</div>
</table>
</form>
the edit doesn't work when i click on edit button it recover the values of the input but do not modify it in the database
Edit.blade.php
#extends('index')
#section('content')
<script language="javascript" type="text/javascript" src={{ URL::to('js/extension.js') }}></script>
<div class="form-group" style="margin-top: 100px;">
<form class="forms-sample" action="/documentSideBar" method="get" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="form-group">
<label for="nomDocument">Nom document</label>
<input type="text" class="form-control" id="nomDocument" name="nomDocument"
value="{{$document->nomDocument}}" onChange='getoutput2()'>
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" id="description" name="description"
value="{{$document->description}}">
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="col-sm-3 col-form-label">Type</label>
<div class="col-sm-9">
<select class="form-control">
<option>Documents administratifs</option>
<option>Documents financiƩres</option>
</select>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="fille">Importer un fichier</label>
<input type="file" onChange='getoutput()' name="file" id="fille" class="form-control">
<input id='extension' type='hidden' name='extension'>
<input id="pathe" type="hidden" name="path">
</div>
<button type="submit" class="btn btn-success mr-2">Modifier</button>
<button class="btn btn-light">Annuler</button>
</form>
</div>
#endsection
documentSideBar.blade.php
<td style="width:300px">
<a href="/download/{{$document->id}}" class="btn btn-success"><i
class="glyphicon glyphicon-download-alt"></i></a>
<a href="/documentSideBar/{{$document->id}}/edit" style="float:right" class="btn btn-primary">
<i class="glyphicon glyphicon-edit"></i>
</a>
<form action="{{ action('DocumentsController#destroy', $document->id)}}" method="POST">
#method('DELETE')
#csrf
<button class="btn btn-danger"><i
class="glyphicon glyphicon-trash"></i></button>
</form>
</td>
this is the edit and update method in the controller
DocumentsController.php
public function edit($id)
{
$document = Document::findOrFail($id);
return view('documents.edit', compact('document'));
}
public function update(Document $document)
{
$document->nomDocument = request('nomDocument');
$document->description = request('description');
$d=request('path');
if(isset($d)){
$document->path ='username'.'/'.request('path');
}
$document->save();
return redirect('/documentSideBar');
}
public function show($id)
{
$document = Document::findOrFail($id);
return view('documentSideBar', compact('document'));
}
also i I want to adjust the buttons , idon't how to make the space between them
the error was in the web.php : here is the right route
Route::resource('documentSideBar','DocumentsController');
Route::get('download/{id}','DocumentsController#download');
Route::get('show/{id}','DocumentsController#show');
Route::post('/update/{id}','DocumentsController#update');
You should change your form method to POST, and change your action to the resource pattern:
<form class="forms-sample" action="{{route('documentSideBar.update', $document->id)}}" method="POST" enctype="multipart/form-data"
#csrf
#method('PATCH')
When you're modifying data in your database you should use POST, not GET.
Take a look here to see more information about the methods.
You also should take a look at the Resource Controller section to understand more about what are you using
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
(SOLVED) Thanks..
I just want to make a new view called "tambah.blade.php" and the controller is "JurnalController.php" with method "tambahJurnal", but it show an error. What's wrong with my route?
Here is my form:
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
And here is my method in JurnalController:
public function tambahJurnal(Request $request){
$jurnal = new Jurnals;
$jurnal->no_jurnal = $request->no_jurnal;
$jurnal->tgl_jurnal = $request->tgl_jurnal;
$jurnal->keterangan = $request->keterangan;
$jurnal->save();
}
This is my route:
Route::post('/tambah', 'JurnalController#tambahJurnal');
And it show an error like this:
enter image description here
You are creating a route for the POST method with this line:
Route::post('/tambah', 'JurnalController#tambahJurnal');
But then, you're trying to perform a GET request with your browser on that URL. That's why you're getting that error.
Try adding this line as well:
Route::get('/tambah', 'JurnalController#tambahJurnal');
#1. Add this route in your route file.
Route::get('/tambah', function()
{
return view('tambah');
});
#2. change in tambah.blade.php file
<form method="post" class="tambahJurnal" action="{{ route('tambah') }}" >
to
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
Thanks
You can write this. Hopefully this will solve your problem.
<h1>Tambah Jurnal</h1>
<form method="post" class="tambahJurnal" action="{{ url('tambah') }}" >
{{ csrf_field() }}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
</form>
try in form action ="/tambah"
try with
Route::any('/tambah', 'JurnalController#tambahJurnal');
first then if it works fine you can change to
Route::post('/tambah', 'JurnalController#tambahJurnal');
any will work for get post put ....
Change this line
Route::post('/tambah', 'JurnalController#tambahJurnal');
to
Route::post('tambah', 'JurnalController#tambahJurnal')->name('tambah');
and use blade Form
<h1>Tambah Jurnal</h1>
{!! Form::open(['route' => 'tambah','method' => 'POST','class' => 'tambahJurnal']) !!}
<div class="">
No jurnal
<input type="text" name="no_jurnal" value="">
</div>
<div class="">
Tgl Jurnal
<input type="date" name="tgl_jurnal" value="">
</div>
<div class="">
Keterangan
<input type="textarea" name="keterangan" value="">
</div>
<input type="submit" name="" value="Submit">
{!! Form::close() !!}
Advantage of using blade Form is , you don't explicitly need to specify {{ csrf_field() }}, blade injects csrf token itself.
Add route to show view
Route::get('/tambah', 'JurnalController#index');
And add index method to your controller
public function index(){
return view("tambah");
}
Also add /
action="{{ route('/tambah') }}"
Sometimes it happened that you are in /tambah and trying to post url becomes /tambah/tambah
I am newbie in laravel and I try to insert a data form a form having foreign key by using hide such as the code is mention below:-
<form class="form-horizontal" role="form" action="/WhatTodo/store" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="task_id" value=" {{$what->task_id}}">
<input type="hidden" name="work_id" value="{{$what->work_id}}">
<div class="form-group">
<label class="control-label col-sm-2" for="name"> Name</label>
<div class="col-sm-5">
{!!Form::select('name',$name)!!}
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="work">work:</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="work" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="Submit">
</div>
</div>
</form>
I have the controller with function:-
public function create($id)
{
$what=WhatTodoModel::findorFail($id);
$name=WOrk::lists('name','name');
return view('what/create',compact('what','name'));
}
You haven't really told us what your issue is or what error you're getting, but my guess given the current question is:
Assuming you're trying to implement a resource route and resourceful controller, the create method is used to show a form to create a new object, not edit an existing one. The create method does not take any parameters, therefore $id will be blank and WhatTodoModel::findorFail($id); will throw an exception.
If you want to edit an existing record, you do that using the edit action.
For creating a new record, create shows the form, store saves the record.
For editing an existing record, edit shows the form, update saves the record.