I am using Laravel 5.6 and trying to create a simple form to create a post. I have my web routes that looks like this..
Route::resource('posts', 'PostsController')->middleware('auth');
My form looks like this...
<form action="{{route('posts#store')}}" method="POST">
<input name="title" type="text">
</form>
And my PagesController looks like this
public function store(Request $request)
{
$post = new Post;
$title = $request->input('title');
$post->save();
}
But I am getting the following error message..
Route [posts#store] not defined
Where am I going wrong?
You may check resource controllers & try following code.
<form action="{{route('posts.store')}}" method="POST">
<input name="title" type="text">
</form>
<form action="{{route('posts.store')}}" method="POST">
<input name="title" type="text">
</form>
public function store(Request $request)
{
$post = new Post;
$post->title= $request->input('title');//change
$post->save();
}
Related
my Route :
Route::resource('/posts',PostsControllerWithAll::class);
my form :
i try to read the given data from this form
<form method="POST" action="{{ action('PostsControllerWithAll#store') }}">
#csrf
<input type="text" name="title" placeholder="enter title">
<input type="submit" name="submit">
</form>
my controller :
public function create()
{
return view("posts.create");
}
public function store(Request $request)
{
return $request->all();
}
and this is my route list
route list image , please check the link image
Try using the named route, like you show on your print screen.
<form method="POST" action="{{ action('posts.store') }}">...</form>
To validated the fields values on store method, use dd($request->all()) this dump the variable and die, it's good to see what is expected.
i just started laravel and i am trying to create a simple post form for practive, when submitted it gives an 419 Error so I used #csrf inside the form.
HTML Form:
<form method="POST" action="/posts">
#csrf
<input type="text" name="title" placeholder="Enter Title">
<input type="submit" name="submit">
</form>
Route:
Route::resource('/posts', 'PostController');
Store Function:
public function store(Request $request)
{
//
$post = new Post;
...
$post->save();
return redirect('/posts'); //Tried
return redirect()->route('posts.index');
}
#csrf fixed the 419 error and made the post work.
The Post works only when there is no redirect(). I think the #csrf is making the redirect() not work.
I've been searching and couldn't find a solution. How do I fix this?
make action="{{ route('posts.store') }}" instead of action="/posts"
make return redirect()->route('posts.index');
What is the correct way of adding a custom method to a resource controller in Laravel 5.6?
What I have so far is a new method in my ProfileController:
public function approve($id){
$user = User::find($id);
$user->state = '1';
$user->save();
return redirect('/dashboard')->with('success', 'User approved.');
}
As well as the following lines added to my web.php file:
Route::post('/profile/{$id}/approve', 'ProfileController#approve');
Route::resource('profile', 'ProfileController');
The form in my view is (afaik) correctly rendered to:
<form method="POST" action="http://myurl.com/profile/10/approve" accept-charset="UTF-8">
<input name="_token" type="hidden" value="v3F1RRhi7iJL2o4egOhcRiuahaGQBwkGkfMal1lh">
<input name="_method" type="hidden" value="PATCH">
<input class="btn btn-success" type="submit" value="Approve User">
</form>
Unfortunately nothing happens, except the "Sorry, the page you are looking for could not be found." page to be shown.
What am I missing? And to expand a bit on this question also, is this even a valid way to implement "single field updates" on a db entry?
Thank you for your help!
i see you have two problems:
firstly correct the route like that
Route::post('/profile/{id}/approve', 'ProfileController#approve');
secondly you have to delete
<input name="_method" type="hidden" value="PATCH">
or replace your route like that:
Route::patch('/profile/{id}/approve', 'ProfileController#approve');
You would want to remove the $ sign from your route:
Route::post('/profile/{id}/approve', 'ProfileController#approve');
The rest of it is correct.
You have written the parameter like var: $id, and you may write it without '$'.
But really you can use the Laravel implicit model binding function to do this:
Route::post('/profile/{user}/approve', 'ProfileController#approve');
And then in your controller:
public function approve(User $user){
// Delete this line--> $user = User::find($id);
$user->state = '1';
$user->save();
return redirect('/dashboard')->with('success', 'User approved.');
}
I have a project that is going to have 8 different forms all updating the same 'user' table in my database. I have the user authentication working and it makes a user in the table on my localhost mysql database. However when I start updating the table I keep getting errors such as email is not unique or http errors or ReflectionException in RouteDependencyResolverTrait.php line 57: Internal error: Failed to retrieve the default value.
I have tried everything, my create works but it makes a new row and doesn't update the existing row which the user is signed in on.
I'm only new to Laravel 5.4 and finished going through all the Laracasts, so I'm absolutely stumped at what to do.
Does anyone have any thoughts or know how to fix it or restructure it better? Please let me know if I have missed anything out. I have been trying to get this working for 2 days.
Basics.php
<?php
namespace App;
class Basics extends Model
{
public $table = "users";
protected $fillable = [
'family_name',
'given_names'
];
}
BasicsController.php
class BasicsController extends Controller
{
public function index()
{
$user = \Auth::user();
return view('/details/basics', compact('user'));
}
public function update(Request $request, $id)
{
$basics = Basics::find($id);
$basics->family_name = $request->input('family_name');
$basics->given_names = $request->input('given_names');
$basics->save();
return redirect("/details/basics");
}
}
basics.blade.php
#extends ('layouts/app')
#section ('content')
{{--Do #includes for all form components with the components file--}}
#include ('layouts/header')
<main class="main">
<form action="/details/basics" method="POST">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<fieldset>
<label>Family name</label>
<input type="text" name="family_name" placeholder="Family name" value="{{ old('family_name') }}" />
</fieldset>
<fieldset>
<label>Given names</label>
<input type="text" name="given_names" placeholder="Given names" value="{{ old('given_names') }}" />
</fieldset>
<button type="submit" value="Save" name="save" class="button button-primary button-wide">Save</button>
</form>
</main>
#endsection
web.php
Route::get('/', function () {
return view('welcome');
});
// Authentication Routes
Auth::routes();
Route::get('/logout', 'Auth\LogoutController#destroy');
Route::get('/home', 'DashboardController#index');
Route::get('/dashboard', function () {
$user = Auth::user();
return view('dashboard', compact('user'));
});
// Eligibility Assessments
Route::get('/assessment/student', 'AssessmentController#index');
Route::post('/assessment/results', 'AssessmentController#store');
// Details
Route::get('/details/basics', 'BasicsController#index');
Route::patch('/details/basics', 'BasicsController#update');
You need to add a rule in your post request which will exclude the email field when updating the model. This is why you're getting the email is not unique error. Although you're not posting the email field but still the save method is doing that. Try using post instead of patch. Just for debugging purposes in your Route.php
I am new to Laravel and I'm having trouble with posting data to a controller. I couldn't find the corresponding documentation. I want to something similar in Laravel that I do in C# MVC.
<form action="/someurl" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>
Controller
[HttpPost]
public ActionResult SomeUrl(string someName)
{
...
}
You should use route.
your .html
<form action="{{url('someurl')}}" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>
in routes.php
Route::post('someurl', 'YourController#someMethod');
and finally in YourController.php
public function someMethod(Request $request)
{
dd($request->all()); //to check all the datas dumped from the form
//if your want to get single element,someName in this case
$someName = $request->someName;
}
This works best
<form action="{{url('someurl')}}" method="post">
#csrf
<input type="text" name="someName" />
<input type="submit">
</form>
in web.php
Route::post('someurl', 'YourController#someMethod');
and in your Controller
public function someMethod(Request $request)
{
dd($request->all()); //to check all the datas dumped from the form
//if your want to get single element,someName in this case
$someName = $request->someName;
}