I'm just trying to insert title and body using eloquent on database and MethodNotAllowedHttpException appears wont let me do it cause of that error
Here's the controller
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
Here's my view create.blade.php
<form action="post.store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
i am assuming your are using resource route.
i think this <form action="post.store" method="POST"> should be <form action="{{ route('post.store') }}" method="POST"> and use this too {{ csrf_field() }} inside form otherwise you will get tokenmismatch error.
<form action="{{ route('posts.store') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
You may use command php artisan route:list and in column Name you will see route name and you will be able to call in view by {{ route('routename') }}
Example:
In route list is shown posts.store, so you will call in view {{ route('posts.store') }}
I think you may call incorrect route by above route view is posts.show but when insert you called post.store maybe posts.store
use in form action {{ route('posts.store') }}
Related
I'm making a form delete request, and every time I try to delete some content I get an error 'call to a member function delete on null'. I didn't used Laravel for a very long time, and I have forgotten how things work, and now I'm learning from beginning, following tutorial from Laracast. I compared the code and everything is good, but I get always some errors, and I cannot find a problem...
web.php
Route::resource('projects', 'ProjectsController');
ProjectsConroller
public function destroy($id)
{
$project = Project::find($id);
$project->delete();
return redirect('/projects');
}
edit.blade.php
#extends('layout')
#section('content')
<h1>Edit Project</h1>
<form method="POST" action="{{ url('/projects/$project->id') }}" >
{{ method_field('PATCH') }}
{{ csrf_field() }}
<div class="field">
<label class="label" for="title">Title</label>
<div class="control">
<input type="text" class="input" name="title" placeholder="Title" value="{{ $project->title }}">
</div>
</div>
<div class="field">
<label class="label" for="description">Description</label>
<div class="control">
<textarea name="description" class="textarea">{{ $project->description }}</textarea>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-link">Update Project</button>
</div>
</div>
</form>
<form method="POST" action="{{ url('/projects/$project->id') }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<div class="field">
<div class="control">
<button type="submit" class="button">Delete Project</button>
</div>
</div>
</form>
#endsection()
This part of your code is wrong:
<form method="POST" action="{{ url('/projects/$project->id') }}"
It's passing $project->id as the ID instead of evaluating. You have 2 ways:
<form method="POST" action="{{ url('/projects/'.$project->id) }}"
<form method="POST" action="{{ url("/projects/$project->id") }}"
As an addition,
In your controller method, you might want to use findOrFail() instead of find() so it returns 404 when the model is not found. find() will return null if model is not found, and you're already trying to call ->delete() on null.
i'm working on a crud app to learn laravel i'm doing good so far , other than when i want to update a post it gives me this
put method is not supported for this route
#extends ('layouts.app')
#section('content')
<form action="{{route('update_blog_path',['blog'=>$blog->id])}}" method="POST">
#method('PUT')
#csrf
<div class="form-group">
<label for="title">Title </label>
<input type="text" name="title" class="form-control" value={{$blog->title}}>
</div>
<div class="form-group">
<label for="title">Content </label>
<input type="text" name="content" class="form-control" value={{$blog->content}}>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary">Edit</button>
</div>
</form>
#endsection
<?php
Route::get('/', function () {
return view('welcome');
});
Route::name('blogs_path')->get('/blogs','BlogController#index');
Route::name('create_blog_path')->get('/blogs/create','BlogController#create');
Route::name('store_blog_path')->post('/blogs','BlogController#store');
Route::name('blogs_path1')->get('/blogs/{id}','BlogController#show');
Route::name('edit_blog_path')->get('/blogs/{id}/edit','BlogController#edit');
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
Try please;
#extends ('layouts.app')
#section('content')
<form action="{{ route('update_blog_path', ['blog' => $blog->id]) }}" method="POST">
#csrf
{{ mehod_field("PUT") }}
<div class="form-group">
<label for="title">Title </label>
<input type="text" name="title" class="form-control" value={{$blog->title}}>
</div>
<div class="form-group">
<label for="title">Content </label>
<input type="text" name="content" class="form-control" value={{$blog->content}}>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary">Edit</button>
</div>
</form>
#endsection
and
Route
<?php
Route::put('update_blog_path/{blog}', 'BlogController#update')->name("update_blog_path");
And your'e code wrong update term
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
change update
Looks like you have a typo in your routes file, change
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
to
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#update');
You misspelled the method name update.
I'm creating a laravel application that allows users to create a blog post.
I have created a PostsController as a resource with store function like this:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
return 123;
}
Also, I added a route in web.php
Route::resource('posts', 'PostsController');
If I list the routes with php artisan php artisan show:routes, POST method is listed:
The HTML form looks like this:
<form action="PostsController#store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
When I submit the form, I get MethodNotAllowedHttpException:
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
I used to use laravel collective for forms before. Haven't done any work in laravel for a while and now it seems to be deprecated (https://laravelcollective.com/), so I resorted to classic HTML form. How do I work around this?
Your action is incorrect within your form - you need to point the action to the URL of the route, and then the route will select the method, in this case, the 'store' method. Also add #csrf for more information CSRF Protection
<form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
add the name in textbox and textarea
form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title" name="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
I'm making a step program and therefore I wanted to use sessions. I have a form which allows the user to fill in some information and then the input values will be put in a session array called "basic_settins" like this
public function saveSettings(Request $request, Product $products)
{
$request->session()->push('basic_settings', $request->input());
return redirect('/themes');
}
But how can I get a specific item from that array and display it for instance in my view? By using something like this: {{ Session::get('store_name') }} in an input field.
The view looks like this:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<form method="POST" action="{{ route('saveBasicSettings') }}">
<div class="row">
<div class="col-md-12">
<button class="btn btn-primary mb-3 float-right">Next</button>
</div>
</div>
<div class="card z-depth-2" style="border-radius: 7px;">
<div class="card-body">
<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
{{ csrf_field() }}
<div class="form-group">
<label for="store_name">Store name</label>
<input type="text" class="form-control" placeholder="Store name" id="store_name" name="store_name" value="{{ Session::get('store_name') }}" required>
</div>
<div class="form-group">
<label for="company name">Company name</label>
<input type="text" class="form-control" placeholder="Company name" id="company_name" name="company_name" value="{{ Session::get('company_name') }}" required>
</div>
<div class="form-group">
<label for="company_tel">Company phonenumber</label>
<input type="text" class="form-control" placeholder="Company Phonenumber" id="company_tel" name="company_tel" value="{{ Session::get('company_tel') }}" required>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
#endsection
When I dd the request by doing this: $data = $request->session()->all(); and then dd($data);
I get the following result:
How can I make this work? Thanks in advance!
When you're using push(), an array will be created, so you need to do something like this:
{{ session('basic_settings.0.company_name') }}
Or:
{{ session('basic_settings')[0]['company_name'] }}
But I would recommend you to save the data with:
{{ session(['basic_settings' => request()->all()]) }}
Then you'll be able to read it with:
{{ session('basic_settings.company_name') }}
I have just created a new project in larvel and I am trying to submit form in db but it is not saving data in db.
here is my form
<form id="" method="post" class="form-horizontal" action="{{ route('updateadminprofile')}}"enctype="multipart/form-data" >
#if (Session::has('success'))
<div class="alert alert-success" role="alert" style="font-size: 18px;">
<strong>Success: </strong>
{{ Session::get('success') }}
</div>
#endif
<div class="form-group">
<label class="col-sm-4 control-label" for="userName"> Name</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="name" name="name" placeholder="name}" value="name" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="userName"> Description</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="description" name="description" placeholder="Description}" value="description" />
</div>
</div>
<button style="margin-left: 30%" type="submit" class="btn btn-primary" name="signup" value="sumbmit" >Save</button>
My route is
Route::group(['namespace' => 'PrivatePages'], function () {
Route::any('/updateadminprofile', ['as' => 'updateadminprofile',
'uses' => 'ProductController#UpdateAdminProfile']);
});
here is my contoller function
public function UpdateAdminProfile(CreateProductRequest $request)
{
$saveproduct = new Product();
$saveproduct->name = $request->name;
$saveproduct->description = $request->description;
$saveproduct->save();
}
it is not saving record in db. and when i try to submit form it gives me below text,
The page has expired due to inactivity.
Please refresh and try again.
when i added csrf in form it even not going to route specified in the action of form
Try this, you are probably missing the CSRF field in your form. Also keep the Flash message out of your form.
#if (Session::has('success'))
<div class="alert alert-success" role="alert" style="font-size: 18px;">
<strong>Success: </strong>
{{ Session::get('success') }}
</div>
#endif
<form id="form" method="post" class="form-horizontal" action="/updateadminprofile">
{{ csrf_field() }}
<div class="form-group">
<label class="col-sm-4 control-label" for="name"> Name</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="name" name="name" placeholder="name" value="name" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="description"> Description</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="description" name="description" placeholder="Description}" value="description" />
</div>
</div>
<button style="margin-left: 30%" type="submit" class="btn btn-primary" name="signup" value="submit">Save</button>
</form>
Edit: cleaned up some minor HTML errors and specified the route directly instead of a blade function. Could you try this?
I solved the issue by just adding following namespaces in the CreateProductRequest and ProductController
In Create CreateProductRequest updated following lines.
use Illuminate\Support\Facades\Request;
class CreateProductRequest extends Request{}
In Product Controller i use following namespace
.
use App\Http\Requests\CreateProductRequest;
Thanks All for your time and help