Hello so I am a beginner in laravel and having some problems. I am not using Illuminate html for my forms because I want just plain html forms. I'm getting this Use of undefined constant id - assumed 'id' in my edit.blade.php. Here is my edit.blade.php:
<form action="/books/{{$book.id}}/update" method="POST">
Title: <input type="text" name="title"> <br/>
Author: <input type="text" name="author"> <br/>
ISBN: <input type="text" name="isbn"> <br/>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit">
</form>
And in my controller:
public function getBook($id) {
$book = Books::findOrFail($id);
return view('books.edit', compact('book'));
}
Am I doing something wrong?
Ok so I fixed it by myself.
First thing is I changed the line {{$book.id}} to {{$book->id}}.
Second is to edit .env with this values:
CACHE_DRIVER=array
SESSION_DRIVER=cookie
QUEUE_DRIVER=array
Then restart server php artisan serve.
Related
I'm submitting these parameters to an external API at www.sample.com/gateway
<form action="www.sample.com/gateway/" method="POST">
<input type="hidden" name="MerchantCode" value="123abc">
<input type="hidden" name="MerchantRefNo" value="1234">
<input type="hidden" name="Particualrs" value="Transaction_type=Bill;Account Number=4321;Account Name=John Doe;Email Address=jd#test.com">
<input type="hidden" name="Amount" value="100">
<input type="hidden" name="PayorName" value="John Doe">
<input type="hidden" name="PayorEmail" value="jd#test.com">
<input type="hidden" name="ReturnURLOK" value="success.php">
<input type="hidden" name="ReturnURLError" value="{{ url('/paymentdetails') }}">
<input type="hidden" name="Hash" value="{{ md5('123abc' . '1234' . 100">
<button type="submit" class="btn btn-primary " value="POST TO GATEWAY"><i class="far fa-credit-card"></i> PAY NOW </button>
</form>
After which, on the ReturnURLError
Controller
public function getPaymentDetail()
{
$response = Http::get('https://sample.com/api.php');
return $response->json();
}
Route
Route::get('/paymentdetails', 'App\Http\Controllers\PaymentController#getPaymentDetail');
I am getting The POST method is not supported for this route. Supported methods: GET, HEAD. when external api sends back data to me.
I triend changing the route to post but then the error becomes The GET method is not supported in this route
Can someone help?
I wrote a post edit method which at first consisted only of text. I did the update using a PUT request.
Now I want to include images to my posts, so I added it, and it works for my POST request of creating a post but doesn't work for my update post, when I want to change image, since PUT request doesn't support file uploads.
So now I'm stuck trying to change my update method from PUT request that only updates text to a POST request that updates both the text and an image if supplied.
This is the code I wrote so far:
public function update(Request $request, Post $post)
{
//store updated image
if($request->hasFile('image') && $request->file('image')->isValid()){
if($post->hasMedia('posts')) {
$post->media()->delete();
}
$post->addMediaFromRequest('image')->toMediaCollection('post', 's3');
}
$post->update(request()->validate([
'body' => 'required'
]));
return redirect($post->path());
}
I think that the $post->update doesn't work for the POST request. I just want to update a text if an update was given.
Using Laravel 6.
EDIT: My form layout structure (simplified)
<form action="action="/posts/{post}" method="POST">
#method('PUT')
#csrf
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>
My routes:
Route::get('/posts/{post}/edit', 'PostsController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostsController#update');
I tried your code and it worked fine with me , only added #csrf in form tag.
<form action="/posts/{post}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>
When using the #csrf and the #method('') it shows correctly on the page code but doesn't work. Either fails with 419 error code or the original method isn't allowed for this page type error.
Using the "old" code seems to work
#csrf
{{ csrf_field() }}
#if(isset($post))
#method('PUT')
{{method_field('PUT')}}
#endif
The csrf_field and method_field options work even though they show the same values in the page code.
<input type="hidden" name="_token" value="hTLipJaANmyR2I9VPyF7QvUKDiQlte7BAoRLjqeN">
<input type="hidden" name="_token" value="hTLipJaANmyR2I9VPyF7QvUKDiQlte7BAoRLjqeN">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_method" value="PUT">
we can count checked value of checkbox using js, jquery, etc. But there is no available source that gives how to count it using Laravel
Form
<form action="" method="post">
#csrf
<input type="checkbox" name="hobbis[]" value="programming"> prog<br>
<input type="checkbox" name="hobbis[]" value="circket"> cricket<br>
<input type="checkbox" name="hobbis[]" value="football"> football<br>
<input type="submit" value="sub">
</form>
laravel
Route::post('/store', function (Request $req) {
return count($req->hobbis);
});
<form id="login-form" action="brand-dashboard" method="post">
<span class"email">email</span>
<input type="email" name="email">
<span class"email">password</span>
<input type="password" name="password"><br><br>
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="submit" value="login">
</form>
this is in my view page..then in my route..
Route::get('/brand-dashboard','BrandsController#auth_brand_admin');
in my Brands controller..i use the method like
public function auth_brand_admin()
{
return ('sample text');
}
but i got error when submiting the form ..the error is..
MethodNotAllowedHttpException in RouteCollection.php
Change your code to this
Route::post('/brand-dashboard','BrandsController#auth_brand_admin');
It's because you register route with GET method but send POST request.