Laravel - get data from input field not working - php

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') }}">

Related

Laravel 9 - passing data for every view from one controller

Hello i'm new in laravel. My application has a search bar component in almost every view.
The user types there the ID of the client, makes a query to db and compares the user's typed ID with the DB client ID.
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class SearchController extends Controller
{
function get_kcli(Request $request) {
$id = $request->input('kcli');
$current_page = $request->input('currentPage') . '.index';
$data = DB::connection('oracle')->table('CLIENTS')->where('KCLI', $id)->get();
return view($current_page, compact('data'));
}
}
Web.php
Route::post('/search', [App\Http\Controllers\SearchController::class, 'get_kcli'])->name('search');
Form search component view:
<nav class="navbar navbar-light">
<form method="POST" class="form-inline position-relative" action="{{ route('/search') }}">
#csrf
#method('POST')
<input class="form-control shadow-none" name="kcli" id="kcli" type="number" placeholder="Codice..." aria-label="Search">
<input type="text" name="currentPage" value="{{ Route::currentRouteName() }}" hidden>
<button type="submit" class="btn btn-light search-btn"><i class="fas fa-search"></i></button>
</form>
Anyone knows the best practice to store that data request globally an mantain it for every view that need to exctract it?
Example : I search for the client ID '5', the search controller makes a db query with the passed ID, then compacts data. That data has to be stored globally for get the results and post it in other views, without searching again on every view switch(the typed ID '5' has to remain in the search field on every view switch).
To print client_id you can store that "id" in session and print from the session by this way your search value keep remains and to get data in all views there is the concept View::share you can register that in AppServiceProvider please go through this link
https://laravel.com/docs/9.x/views#sharing-data-with-all-views

How to change the boolean in DB calling a function in href?

In a Laravel context, I've got this messages page, with all the messages belonging to a specific user. Initially all messages are not readed, so I put a button to change the boolean in DB (from 0 to 1) and finally show the message.
I'm doing this:
The view
#if ($message->readed != 0)
<p class="card-text message text-left">{{ $message->message }}</p>
#else
<form method="POST" action="/message/read">
#csrf
#method('PATCH')
<input type="hidden" name="message" value="{{ $message->id }}"/>
<button class="btn btn-info text-white" type="submit">
Leggi
</button>
</form>
#endif
The route in web.php
Route::patch('message/read', 'MusicianController#readMessage');
The function
public function readMessage(Request $request)
{
$message = Message::where('id', $request->id)->first();
$message->readed = 1;
$message->update();
return redirect()->back()->with('message', 'message updated');
}
But it's not working, as soon as I click the button to show the message (and even change the DB value) I've got this error: The PATCH method is not supported for this route. Supported methods: GET, HEAD.
Even if I had specified a patch method in routes and even in the form with #method('PATCH')
Could someone help me understand what's wrong please??
the main answer
your route is:
Route::patch('message/read', 'MusicianController#readMessage');
replace your route with following route that use for all CRUD opration:
Route::resource('message/read', 'MusicianController');
if you use ajax for submit data then replace your type and url with following:
type: "patch",
url: "{{url('message/read')}}",
if you don't use ajax than use following:
<form method="POST" action="{{url('message/read"')}}">
{{csrf_field()}}
{{ method_field('PATCH') }}
</form>
update: after version 5.6 you can use these syntax for above functions in any blade file:
<form method="POST" action="{{url('message/read"')}}">
#csrf
#method('PATCH')
</form>

Can't get input data from form LARAVEL

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());
}

Laravel- Form post going to wrong URL

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">

delete data from database using model in laravel 5

I am trying to delete data from my table employes by getting the id of the data and transferring it by using get route but i get the following error
ModelNotFoundException in Builder.php line 125: No query results for
model [App\employe].
her is my route
Route::get('/delete/{employe}', 'EmployeController#delete');
Route::post('/delete', 'EmployeController#handleDelete');
and i have model called employe to access my table
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class employe extends Model {
protected $fillable = array('fname','lname','age','sex','phone','email','houseno','city','kebele','state','username','password','workposition','salary','bankaccount','bankname','contactid','employedate');
}
part of my controller for deleting
public function delete(employe $employe)
{
// Show delete confirmation page.
return View('deleteemploye', compact('employe'));
}
public function handleDelete()
{
$employe = employe::findOrFail(Input::get('employe'));
$employe->delete();
return Redirect::action('EmployeController#index');
}
her is my view for deleting file
#extends('app')
#section('content')
<div class="page-header">
<h2>Delete {{$employe->fname}} Are you sure?</h2>
</div>
<form action="{{ action('EmployeController#handleDelete') }}" method="post" role="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="employe" id="employe" value="{{ $employe->id }}" />
<input type="submit" class="btn btn-default" value="Yes" />
No
</form>
#stop
Looks like laravel cant find the model with the primary key you are passing. Check the database and see if its there. If it is check the deleted_at field. If it has a date in it, it means it is already deleted. You can still find it by using withTrashed()
$employe = employe::withTrashed()->findOrFail(Input::get('employe'));
and if you have soft deleting enabled for this model, you can completely remove the given row by running
$employe = employe::withTrashed()->findOrFail(Input::get('employe'))->forceDelete();

Categories