Method('Patch') and CSRF token appear in page and don't work. I'm new and going through the beginner's course. For some reason, @method ('PATCH') and @CSRF text appears on my page and are not functioning properly. Code:
@section('title')Edit @endsection
@section('content')
<h1>Edit a pet</h1>
<form action="/Pets/{{$pet->id}}" method="POST">
@method('PATCH')
@csrf
<div class="form-group">
<label class="label" for="Pet_Name">Name</label>
<input type="text" class="form-control" name=Pet_Name value="{{$pet->Pet_Name}}">
</div>
<div class="form-group">
<label class="label" for="type">Type</label>
<input type="text" class="form-control" name=type value="{{$pet->Type}}">
</div>
<div class="form-group">
<label class="label" for="age">Age</label>
<input type="number" class="form-control" name=age value="{{$pet->Age}}">
</div>
<div class="form-group">
<label class="label" for="price">Price</label>
<input type="number" class="form-control" name=price value="{{$pet->Price}}">
</div>
<div >
<button class="btn btn-success" type="submit">Edit Pet</button>
</div>
</form>
<form action="/Pets/{{$pet->id}}" method="POST">
@method('DELETE')
@csrf
<button class="btn btn-danger" type="submit">Pet Adopted</button>
</form>
@endsection
you may need to check your laravel version, these @csrf @method blade directives available on version 5.6 and onward.
is this a blade file, like it actually has .blade.php as the file extension instead of just .php? Blade tags will only be parsed in blade files.
Yes. The filename is: edit.blade.php
Just make sure it's working try a post then troubleshoot from there.
Also use Chrome developer tools to inspect what's going on.
But try the post first just to see if all is working.
Please format your code by putting 3 backticks ``` on a line before and after each code block
<form action="/Pets/{{$pet->id}}" method="POST">
@method('PATCH')
@csrf
<div class="form-group">
<label class="label" for="Pet_Name">Name</label>
<input type="text" class="form-control" name=Pet_Name value="{{$pet->Pet_Name}}">
</div>
<div class="form-group">
<label class="label" for="type">Type</label>
<input type="text" class="form-control" name=type value="{{$pet->Type}}">
</div>
<div class="form-group">
<label class="label" for="age">Age</label>
<input type="number" class="form-control" name=age value="{{$pet->Age}}">
</div>
<div class="form-group">
<label class="label" for="price">Price</label>
<input type="number" class="form-control" name=price value="{{$pet->Price}}">
</div>
<div >
<button class="btn btn-success" type="submit">Edit Pet</button>
</div>
</form>
I have the exact same code in another project and it works fine.
the syntax highlight color look a bit abnormal to me... I guess that it could be your html tag error causing it? try to correct the name="Pet_Name" and the rest with double quote "" and check the rest of the tags is correctly in place and balance...
I corrected the name="" on my tags. No change.
Can you view source in your browser and then post the form here again
Apart from 'not working properly' you have not actually said what is wrong!
Hi, try this :
{{ csrf_field() }}
{{ method_field('PATCH') }}
That works as expected. I still don't understand why @csrf won't work in this project, but works in another.
Sorry. The @method ('PATCH') line wasn't working and the form was being sent as a POST request. I'm not sure if @csrf is working or not.
You should see a hidden input for _token in the rendered page.
Would you mind copy and paste the entire rendered page source here...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!--Bootstrap start-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<!--Bootstrap end-->
<title>Edit </title>
</head>
<body>
<h1>Edit a pet</h1>
<form action="/Pets/1" method="POST">
@method('PATCH')
@csrf
<div class="form-group">
<label class="label" for="Pet_Name">Name</label>
<input type="text" class="form-control" name="Pet_Name" value="Sir BarksAlot">
</div>
<div class="form-group">
<label class="label" for="type">Type</label>
<input type="text" class="form-control" name="type" value="Yappy little shit">
</div>
<div class="form-group">
<label class="label" for="age">Age</label>
<input type="number" class="form-control" name="age" value="3">
</div>
<div class="form-group">
<label class="label" for="price">Price</label>
<input type="number" class="form-control" name="price" value="33.48">
</div>
<div >
<button class="btn btn-success" type="submit">Edit Pet</button>
</div>
</form>
<form action="/Pets/1" method="POST">
@method('DELETE')
@csrf
<button class="btn btn-danger" type="submit">Pet Adopted</button>
</form>
<button><a href="/Pets">Cancel</a></button>
</body>
</html>
i think that the only thing that can go wrong is the laravel version only... can you show the composer.json ?
These methods were added in Laravel 5.6 - are you on an earlier version?
i had asked him to check before but he said "I have version 5.8", i'm doubt hence ask for the composer.json file to double confirm...
My composer.json file:
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.0.0",
"fideloper/proxy": "~3.3",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0"
So I was incorrect. I'm working on version 5.5. When I run php artisan -V I get: Laravel Framework 5.8.29
and you are running php artisan -V in the same folder as composer.json?
You say this command returns 5.8 but from the evidence and the way blade behaves you are running 5.5
Running php artisan -V in the project folder returns 5.5.46.
ok, so thats all ok. You are on 5.5 and those new methods don't work. Just switch back to the prior blade tags which you have already identified work ok.
Thank you, everyone, for your help. :)
Please sign in or create an account to participate in this conversation.