I'm building a blog post to learn Laravel 5.4 and am struggling to find any examples of how to update a Post anywhere.
My form is as follows
<form method="POST" action="/posts/{{ $post->id }}/edit">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
My Routes are as follows
Route::get('/posts/{post}/edit', 'PostsController#edit');
Route::patch('/posts/{post}', 'PostsController#update');
And my controller methods are
public function edit( Post $post )
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post )
{
Post::where('id', $post)->update($request->all());
return redirect('home');
}
I get a MethodNotAllowedHTTPException error but am not sure which part / parts of this I am getting wrong.
I'm assuming it must be the point at which I'm using the PATCH function, or potentially just the way I'm mass-assigning the new values. Any help would be greatly appreciated.
you should use
{{ method_field('PATCH') }}
as your form field
and change action to
/posts/{{ $post->id }}
like this:
<form method="POST" action="/posts/{{ $post->id }}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
There are a couple of things you have missed out.
Firstly, as #Maraboc pointed out in the comments you need to add method spoofing as standard HTML forms only allow for GET and POST methods:
<input type="hidden" name="_method" value="PATCH">
or
{{ method_field('PATCH') }}
https://laravel.com/docs/5.4/routing#form-method-spoofing
Then you will also need to omit the "edit" uri in your forms action:
<form method="POST" action="/posts/{{ $post->id }}">
or
<form method="POST" action="{{ url('posts/' . $post->id) }}">
https://laravel.com/docs/5.4/controllers#resource-controllers
(scroll down a little bit to the Actions Handled By Resource Controller section)
You also may find it helpful to watch https://laracasts.com/series/laravel-5-from-scratch/episodes/10
Hope this helps!
When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.
Related
``I had one page on that I'm Showing data from two table with left join and also edit button in front of every column when user click to edit button ,it goes a different page where user can update input fields but at the same time i want if some field is not showing data we can create data only when both the id of tables are matching
By this i can easily update but i can't create it shows error
Attempt to assign property "SchemeId" on null
Controller
public function SchConfigrationUpdate(Request $request, $SchemeId)
{
$scheme = tbl_schemeconfigration::find($SchemeId);
$scheme->SchemeId = $request->input('SchemeId');
$scheme->MerchantCode = $request->input('MerchantCode');
$scheme->BankAccountNumber = $request->input('BankAccountNumber');
$scheme->BankAccountIFSC = $request->input('BankAccountIFSC');
$scheme->save();
return redirect()
->back()
->with('success', 'Scheme Update Successfully');
}
**view**
<form action="{{ url('SchConfigration-update/' . $user->SchemeId) }}" method="post"
enctype="multipart/form-data">
#csrf
#method('PUT')
<input type="hidden" name="SchemeId" value="{{ $user->SchemeId }}">
<div class="form_row">
<div class="form_item">
<label>Scheme Name</label>
<input type="text" id="SchemeId" name="SchemeId"
placeholder="Enter scheme name" class="form-control" required
value="{{ $user->SchemeId }}">
{{-- value="{{ request()->SchemeId }}" --}}
{{-- if we want to pass scheme name just pass $user->SchemeName --}}
</div>
<div class="form_item">
<label>Merchant Code</label>
<input type="text" id="MerchantCode" name="MerchantCode"
placeholder="Enter Merchant Code" class="form-control" required
value="{{ $user->MerchantCode }}">
</div>
</div>
<div class="form_row">
<div class="form_item">
<label>Bank Account Number</label>
<input type="text" id="BankAccountNumber" name="BankAccountNumber"
placeholder="Bank account Number" maxlength="17" class="form-control" required
value="{{ $user->BankAccountNumber }}">
</div>
<div class="form_item">
<label>Bank Account IFSC</label>
<input type="text" id="BankAccountIFSC" name="BankAccountIFSC"
placeholder="Bank account IFSC" maxlength="11" class="form-control" required
value="{{ $user->BankAccountIFSC }}">
</div>
</div>
<div class="btn_row">
<input type="submit" value="submit" class="primary_btn">
<a class="btn btn-primary" href="{{ url('SchConfigration') }}"
role="button">Back</a>
</div>
</form>
`
Try This
public function SchConfigrationUpdate(Request $request, $SchemeId)
{
$scheme = tbl_schemeconfigration::updateOrCreate(
['SchemeId' => $request->input('SchemeId')],
[
'MerchantCode' => $request->input('MerchantCode'),
'BankAccountNumber' => $request->input('BankAccountNumber'),
'BankAccountIFSC' => $request->input('BankAccountIFSC')
]
);
return redirect()
->back()
->with('success', 'Scheme Update Successfully');
}
hi i am trying to show the value of name which is stored in database but i am getting this error can anyone please guide me how can i rectify my error .
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{ $Task ->name }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ $Task ->email }}" >
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
here is my edit controller
public function edit($Taskid)
{
//
$Edit= Superior::find($Taskid);
return view('Myview.edit')->with('Task','$Edit');
}
here is my route for edit
Route::get('Superior/{Task}/edit','SuperiorController#edit');
i am using Task as a key but it is giving me an error
i also tried but didn't work
{{$Task['name']}}
You should remove ' that wraps $Edit.
return view('Myview.edit')->with('Task', $Edit);
Change your code like this.
Form Code
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{isset($Edit) ? $Edit->name : '' }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ isset($Edit) ? $Edit->email : '' }}">
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
Route
Route::get('Superior/edit/{id}','SuperiorController#edit');
Controller Code
public function edit($id){
$Edit= Superior::find($id);
return view('Myview.edit',compact('Edit'));
}
Hopefully it'll solve the problem
Create function like this. if Taskid in your database column.
public function edit(Request $Request, $Taskid)
{
//
$Edit= Superior::where('Taskid', $Taskid)->first();
return view('Myview.edit', compact(Edit));
}
In your html it will like this
{{ $Edit->name }}
In your Route
Route::get('Superior/{Taskid}/edit','SuperiorController#edit');
i try to looping my edit form. but the foreach loop doesn't work. anyone can help?
#foreach($siswas as $siswa)
<form id="editform" action="{{route(siswa.update, $siswa->id}}" method="post">
#method('PATCH')
#csrf
<div class="modal-body">
<div class="form-group">
<label for="nama" class="col-form-label">Nama</label>
<input type="text" value="{{$siswa->nama}}" id="nama" name="editNama" class="form-control" >
</div>
</div>
</form>
#endforeach
Several errors there:
route name should be a string (in quotes),
route parameter should be an associative array,
in provided example, closing parenthesis missed
(Assuming that parameter in Route is id), try with
{{ route('siswa.update', ['id' => $siswa->id]) }}
Laravel named routes.
You should try this:
your edit function like this:
public function edit($id)
{
$siswas = Yourmodel::find($id);
return view('yourviewpath',compact('siswas'));
}
Your view file like this:
<form id="editform" action="{{route(siswa.update, [$siswas->id]}}" method="post">
#method('PATCH')
#csrf
<div class="modal-body">
<div class="form-group">
<label for="nama" class="col-form-label">Nama</label>
<input type="text" value="{{ old('editNama', $siswas->nama) }}" id="nama" name="editNama" class="form-control" >
</div>
</div>
</form>
You should incorporate siswa in your form id:
#foreach($siswas as $siswa)
<form id="editform-{$siswa->id}" action="{{route(siswa.update, $siswa->id}}" method="post">
#method('PATCH')
#csrf
<div class="modal-body">
<div class="form-group">
<label for="nama-" class="col-form-label">Nama</label>
<input type="text" value="{{$siswa->nama}}" id="nama" name="editNama" class="form-control" >
</div>
</div>
</form>
#endforeach
How can I remove photo from calendar's event in edit calendar's event view? In list of events I did delete method and it works. Now when I try to do the same in edit.blade.php it gives error:
Call to a member function photos() on null
I have two tables in relationship one calendar to many photos, file upload works, but I stucked on edit part.
Look at my controller function:
public function deletePhoto(CalendarRepository $calRepo, $id)
{
$calendars = $calRepo->find($id);
$calendars->photos($id)->delete();
return redirect()->action('CalendarController#edit');
}
and here is fragment of edit.blade.php:
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
#foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}" style="width:100px; height:auto;"/>
</div>
<i class="fas fa-times"></i>Remove
</div>
#endforeach
</div>
</div>
I need to remove photo from Photo table and redirect to edit.blade.php (about the specific event id of the calendar)
Thanks for any help.
EDIT:
<div class="card-body">
<form action="{{ action ('CalendarController#editStore')}}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<input type="hidden" name="id" value="{{ $calendar->id }}"/>
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
#foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}"/>
</div>
<form method="POST" action="{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}">
#csrf
#method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
</div>
#endforeach
</div>
</div>
<div class="form-group">
<label for="header">Header</label>
<input type="text" class="form-control" name="header" value="{{ $calendar->header }}"/>
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="{{ $calendar->description }}"/>
</div>
<div class="form-group">
<label for="date">Date</label>
<input type="date" class="form-control" name="date" value="{{ $calendar->date }}"/>
</div>
<input type="submit" value="Save" class="btn btn-primary"/>
</form>
</div>
You use the same $id to find the photo and the calendar instance.
GET request is not recommended for deleting a resource, so a better approach would be in your routes you can have something like this:
Route::delete('photo/{photo}', 'PhotosController#delete')->name('photo.delete');
Then in your view, you should surround the button with a Form, for example:
<form method="POST" action="{{ route('photo.delete', $photo) }}">
#csrf
#method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
Then your confirm function in JS should submit the form if the user accepts to delete the photo. And also remember to return false as default in the confirm function so it does not submits the form by default.
Your controller will then be:
public function delete(Photo $photo)
{
$photo->delete();
return redirect()->back();
}
--- EDIT
Route::delete('calendar/{calendar}/photo/{photo}', 'CalendarController#deletePhoto')->name('photo.delete');
and the action in the form can be:
{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}
The method in the controller:
public function deletePhoto(Calendar $calendar, Photo $photo)
{
$calendar->photos()->where('id', $photo->id)->delete();
return redirect()->action('CalendarController#edit');
}
I have this form "Edit tache". Editing one "tache" affects two tables user and technicien, when i choose to edit tache the edit form is autofilled with information from the technicien table but the fields related to the table user are not autofilled. How could i autofill the form with information from the two tables? Thank you.
controller
public function edit($id)
{
$technicien=technicien::find($id);
$user = user::orderBy('id', 'asc')->get();
return view('technicien.edit',['moyenne_avis'=>$technicien],['actif'=>$technicien],['user_id'=>$technicien])->with('users', $user );
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
return redirect('technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Tache</h1>
<form action="{{ route('technicien.update', $actif->id , $moyenne_avis->id ) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="">Nom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">Prenom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name ="moyenne_avis" class="form-control"value ="{{$moyenne_avis->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name ="actif" class="form-control"value ="{{$actif->actif}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
route
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::put('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
$user = user::find($id);
if they are retrieved using one id on the controller, else please tell the relation between them.
and in your view, i don't see a reason to check old since its not updated yet! use
{{$user->nom)}}