I am learning Laravel from scratch.
I am doing simple form "POST" but something is missing and I am not able to find out what is missing.
So here is my "routes.php":
Route::get('cards','CardsController#all');
Route::get('cards/{card}','CardsController#show');
Route::post('cards/{card}/notes','NotesController#new');
and view:
<div>
<form method="POST" action="cards/{{ $card->id }}/notes">
<div class="form-group">
<label>Note body:</label>
<textarea name="body" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
and here is controller:
`
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function new(Request $request){
return $request->all();
}
}
`
Now when I submit the form it gives me following error:
NotFoundHttpException in RouteCollection.php line 161
and the URL in browser becomes:
http://localhost:88/learning/cards/cards/1/notes
which is definitely wrong.
I think I am missing something very basic.
Please guide me.
Thank you.
Change the form action to,
<form method="POST" action="{{ url('cards/'.$card->id.'/notes') }}" >
Your action starts with "cards/". Change the action to start with '/cards/' so that it's path is not calculated relative to the current route.
<form method="POST" action="/cards/{{ $card->id }}/notes">
Related
i'm having trouble to get data from an input.
I try to be more specific
My application has many views, and each of these has an #include component that works as a search field.
For example the user types in the input the ID of the store, the controller compares the ID that user inserted with the DB store's ID and then compacts data and fill the views with infos of that specific store.
Im just testing how to get that data from the input but i'm getting this error:
Route [search.get_kcli] not defined.
Actually i'm trying to use that function only for get data by using a controller only for that input field.
What's wrong in it?
Thanks for help!
My code looks like this:
inside of app.blade.php
#auth
#include('partials.search')
#endauth
inside search.blade.php
<form method="POST" class="form-inline position-relative"
action="{{ route('search.get_kcli') }}">
#csrf
#method('POST')
<input class="form-control shadow-none" name="kcli" id="kcli" type="number"
placeholder="Codice..." aria-label="Search">
<button type="submit" class="btn btn-light search-btn"><i class="fas fa-search"></i></button>
</form>
Inside the SearchController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class SearchController extends Controller
{
function get_kcli(Request $request) {
$kcli = $request->input('kcli');
dd($kcli);
}
}
Inside web.php
Route::post('/search', [App\Http\Controllers\SearchController::class, 'get_kcli'])->name('search');
Change
<form method="POST" class="form-inline position-relative"
action="{{ route('search.get_kcli') }}">
to
<form method="POST" class="form-inline position-relative"
action="{{ route('search') }}">
I created a model and its migration like this:
php artisan make:model Lala -m
And I made this : php artisan migrate
I was going to call this road, but I have a mistake. Did I write it wrong? How can I call the search method when my form is submitted?
formular:
<?php
use App\Models\Lala;
?>
<form action="{{ route('Lala.search')}}" method="GET" >
<div class="input-group mb-3">
<input type="text"
name="name" class="form-control" placeholder="Geben Sie etwas an"
aria-label="Geben Sie etwas an"
aria-describedby="basic-addon2" autocomplete="off">
<div class="input-group-append">
<span class="input-group-text" id="basic-addon2">🎓</span>
</div>
</div>
<input type="submit" class="btn btn-primary" value="search">
</form>
I defined the route as follows in web.php :
use App\Models\Lala;
Route::get('/search',[
'as' =>'Lala.search',
'uses' =>'\App\Http\Controllers\stipendiensController#search']);
stipendiensController is defined like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Stipendien;
class stipendientsController extends Controller
{
public $name;
public function search()
{
return view('seite.Stipendien');
}
}
how to avoid this error? could I write this code differently? I try indeed to enter the data in my search bar and I compare in my database if the value entered in the search bar is there.
Thank you for helping me . Please
Try defineing your route like so:
use App\Http\Controllers\stipendiensController;
Route::get('/search', [stipendiensController::class, 'search'])
->name('Lala.search');
I'm learning Laravel and I got stuck trying to get data from a form.
I already am able to get data back with GET, but with POST I've been having a ton of trouble. Here's what I'm working with:
Form:
<form id="forms" method="POST" action="sugestoes" novalidate>
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-12">
<label for="obs">Observações:</label>
<textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
</div>
</div>
<hr>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
#php
if (isset($_POST["obs"])) {
echo "IN";
}
#endphp
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$name = $request->input('obs');
return redirect('sugestoes');
//
}
}
Route:
Route::post('sugestoes', 'PostController#store');
The intended behaviour that I'm trying to reach is for the post to be submitted, and then returning to the same page with an empty form. Later on I'll be sending the input data into a database, but for now I just want to get the post to work.
I guess I'm missing something really basic, but I've been following guides and looking online, I've done some progress but I'm really stuck here.
(some more info, this is Laravel 5.4, and I'm using XAMPP)
First, you need to call the model, use App/Your_model_name; then you have to save the data.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Suggest; //Suggest model, let's hope you have suggest table
class PostController extends Controller
{
public function store(Request $request)
{
$suggest = new Suggest; //model
$suggest->name = $request->obs; //name is DB name, obs is request name
$suggest->save(); //save the post to DB
return redirect()->back()->with('success', 'Saved successfully'); //return back with message
}
}
Then if you want to flash the message on the HTML page
#if(session('success'))
<div class="alert alert-warning alert-dismissible" id="error-alert">
<strong style="color: white;">{{session('success')}}</strong>
</div>
#endif
<form id="forms" method="POST" action="{{ route('sugestoes') }}" novalidate>
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-12">
<label for="obs">Observações:</label>
<textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
Remove the #php tag below the form, then in router.php
Route::post('/sugestoes', 'PostController#store')->name('sugestoes');
Then in Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$name = $request->input('obs');
return redirect('/sugestoes'); // you should have GET in Route.php
//
}
}
Add the following code in your action attribute on the form. It will capture the post URL. When you submit the form it will send the form data to the URL end-point.
action="{{ url('sugestoes')}}"
Then die and dump in your controller store function
public function store(Request $request)
{
dd($request->all());
}
I will get MethodNotAllowedHttpException when submitting a form in laravel
HTML file
<form action="{{ action('HomeController#store') }}" method="post">
<input name="_method" type="hidden" value="PATCH">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<input type="submit" name="Submit" value="submit">
</form>
Im my routes.php
Route::post('formaction','HomeController#store')
Controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class HomeController extends Controller
{
public function store(){
echo 'form submitted';
}
}
Why i will get MethodNotAllowedHttpException in my form action page?
I've refereed some questions related to this,but nothing help me
Even if the form is using the POST method, you are sending the extra param _method which will let the framework know that you want to use that method instead. If you send that extra param then you should set the route accordingly:
Route::patch('formaction','HomeController#store');
I have a little problem with the silex framework (I'm quite sure, that it's caused by Silex)..
I have a form and want to submit it with POST, but Silex throws the following exceptions:
MethodNotAllowedException in UrlMatcher.php line 101:
MethodNotAllowedHttpException in RouterListener.php line 149:
No route found for "POST /checkPW": Method Not Allowed (Allow: GET)
Thats what my controller looks like:
$app->get('/checkPW', function () use ($app) {
return $app['templating']->render(
'checkPW_blog.php'
);
});
And that's what the form looks like:
<form method="post" action="/checkPW">
<div class="modal-body">
<div class="form-group">
<input type="password" class="form-control" id="password"
name="password"
placeholder="Passwort">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success" id="submitPW">
Passwort bestätigen
</button>
</div>
</form>
(realised with Bootstrap)
The bizarre thing is, that when I send the form with the method GET instead of POST everything works fine...
Does anybody know whats the problem here..?
Thanks all.
Look, you only define a route for get:
$app->get('/checkPW', function () use ($app) {
return $app['templating']->render(
'checkPW_blog.php'
);
});
Just define one for post:
$app->post('/checkPW', function () use ($app) {
// do post stuff...
});