Missing required parameters for route (Laravel 5.8) - laravel

I'm trying to recreate a blog application form old laravel version in new version (5.8). In old version, I used laravel collective for forms, and my edit post form looks like this:
#extends('layouts.app')
#section('content')
<h1>Edit Post</h1>
{!! Form::open(['action' => ['PostsController#update', $post->id], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title', $post->title, ['class' => 'form-control', 'placeholder' => 'Title'])}}
</div>
<div class="form-group">
{{Form::label('body', 'Body')}}
{{Form::textarea('body', $post->body, ['class' => 'form-control', 'placeholder' => 'Body Text'])}}
</div>
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
#endsection
Now I'm trying to recreate the same form without laravel collective forms because it seems to be deprecated.
This is my attempt at recreating this form:
#extends('layouts.app')
#section('content')
<h1>Edit post</h1>
<form action="{{ route('posts.update'), $post->id }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title" name="title" value={{$post->title}}>
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body" value={{$post->body}}></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
#endsection
I'm getting the following error:
Missing required parameters for [Route: posts.update] [URI: posts/{post}]. (View: C:\xampp\htdocs\blog\resources\views\posts\edit.blade.php)
Looks like I'm not sending the id parameter correctly.
Also,how can I recreate this part: {{Form::hidden('_method', 'PUT')}} in plain HTML?

You can recreate the method input with this:
#method('PUT')
You need to put the $post->id inside the route() and use the Form method Spoofing (#method('PUT')). Try this:
#extends('layouts.app')
#section('content')
<h1>Edit post</h1>
<form action="{{ route('posts.update', $post->id) }}" method="POST">
#csrf
#method('PUT')
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title" name="title" value={{$post->title}}>
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body" value={{$post->body}}></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
#endsection

Your error is caused by $post->id being outside of the route() function.
Change:
route('posts.update'), $post->id
to:
route('posts.update', $post)
As for the second question, this is how Form::hidden('_method', 'PUT') is rendered as HTML:
<input type="hidden" name="_method" value="PUT">
But you can use #method if you prefer a shorter way of writing it:
<form action="{{ route('posts.update', $post) }}" method="POST">
#method('PUT')
#csrf
...
</form>

Related

Laravel $request->all() is empty

I start study laravel.
The route to contacts page
Route::match(['get', 'post'], '/contacts', [ 'uses' => 'Admin\ContactController#show', 'as' => 'contacts' ] );
class ContactController extends Controller {
public function show( Request $request ) {
print_r( $request->all() );
return view( 'default.contacts', [ 'title' => 'Contacts' ] );
}
}
Form
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
When i submit the form, i get an array with token.
Array
(
[_token] => JMTxTwh5Cb4sPeDjGcVetgTt2yGy6mDsFs6jW3Tx
)
What can be a problem?
Thanks for answer.
You are missing the name in your inputs. Please add to them.
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" name="name" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" name="address" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>

unable to submit form tag in laravel

unable to call post URL from form tag. When I click on the submit button it is going to a different URL name viewstudentmarks.
<form method="post" style="padding-top:30px;" action="{{ route('updatestudentmark',['id' => $stuid]) }}">
{{csrf_field()}}
<input type="hidden" name="type" value="{{$examtype}}">
<div class="row text-center">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="row">
<div class="col-lg-12">
<div class="form-group">
</div>
</div>
</div>
</div>
</div>
#foreach($extra as $detail)
<a class="btn btn-success" href='{{ url("viewstudentmarks/{$detail->class_id}") }}'>Back</a>
#endforeach
<input type="submit" class="btn btn-primary">
</form>
and web.php
Route::post('updatestudentmark/{id}','MarksRecordController#update')->name('updatestudentmark');
Route::get('viewhealthdetails/{id}','HealthDetailsController#viewstudents')->name('viewhealthdetails');
In form tag add {{ route('') }} name:
<form method="post" style="padding-top:30px;" action="{{ route('updatestudentmark',['id' => $stuid]) }}">
{{csrf_field()}}
</form>
Also, check your controller code. In the controller, Have you call the redirect method or not.
Thanks
it's much better to use name routes like:
Route::post('updatestudentmark/{id}','MarksRecordController#update')->name('test.route');
and in blade do something like this in action form:
{{route('test.route',['id' => $stuid])}}

Laravel input loop

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

Laravel | Delete function - how to delete photo from calendar's event

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');
}

Strange view error on Laravel

I have some cotroller like this:
Route::get('/article/create', 'ArticlesController#create');
and here's my ArticlesController#create:
public function create(){
return view('articles.create',
[
'title'=>'Add Artikel',
'username'=>'Whatever Myname',
'status' => 'Offline'
]
);
}
when i trying to access blog.dev/article/create i got this strange errors:
"Trying to get property of non-object (View: E:\xampp\htdocs\blog\resources\views\articles\single.blade.php)"
how come i can get this kind of error when my view is pointing at articles.create but the error is at single.blade.php which it suppose for ArticlesController#view?
this is what in create.blade.php:
#extends('admin.layout')
#section('content')
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Horizontal Form</h3>
</div>
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/articles">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input class="form-control" id="title" name="title" placeholder="Judul Artikel">
</div>
</div>
<div class="form-group">
<label for="content" class="col-sm-2 control-label">Content</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="content" name="content"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
#endsection
and here's in my single.blade.php :
#extends('admin.layout')
#section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
<hr>
#foreach($article->comments as $comment)
<blockquote>
<p>{{ $comment->comment }}</p>
<small>{{ $comment->created_at->diffForHumans() }}</small>
</blockquote>
#endforeach
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/article/{{ $article->id }}/comment">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">Comment</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="comment" name="comment"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
#endsection
and here's my router:
Route::get('/articles', 'ArticlesController#index')->name('home');
Route::post('/articles', 'ArticlesController#save');
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
Route::post('/article/{article}/comment', 'CommentsController#save');
Route::get('/register', 'RegistrationController#create');
Route::post('/register', 'RegistrationController#store');
I try to change the create function by pointing into another view, but still it showing same error and pointing at single.blade view.
I delete all code at single.blade and write 'test' text, i don't get any errors. but i'm pointing my controller for viewing into create.blade not sigle.blade
After seeing you routes the problem is in this two routes :
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
In this case Laravel will consider the create in your path blog.dev/article/create as an id parameter of the view route here => /article/{id}.
So as a solution you should simply inverse the two routes :
Route::get('/article/create', 'ArticlesController#create');
Route::get('/article/{id}', 'ArticlesController#view');
You need to find what's in the file by reading more than just that line. Maybe you're including that file in the articles.create? Maybe you're accessing a variable that doesn't exist whenever you load the page. Edit your answer with more of the error and what's inside both blades and we can pinpoint what's wrong.

Resources