Target class [ProductController] does not exist - php

web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::post('/products/{qty}/add', 'ProductController#addProduct')->name('addProduct');
products-list.blade.php
<form action="{{ route('addProduct', 1) }}" method="post" class="ui form">
<input type="text" name="qty" value="1" />
<button type="submit" class="btn btn-primary" type="button">Add</button>
</form>
app/Http/Controllers/ProductController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function addProduct(Request $request, $qty)
{
dd($qty);
}
}
But the controller class exists?

Namespacing won't help in this scenario, because it's just a string:
Route::post('/products/{qty}/add', 'ProductController#addProduct')
This will work:
Route::post('/products/{qty}/add', [ProductController::class, 'addProduct'])
or you can do:
Route::post('/products/{qty}/add', 'App\Http\Controllers\ProductController#addProduct')

Related

Larvel 8 POST not supported for this route error

The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel 8 error used, what I am doing wrong here?
My form file name -- insertemp.blade.php
<form method="POST" action="/showemployee">
#csrf
<div class="form-group" style="margin-left: 30px">
<label for="id">ID</label>
<input class="form-control" type="number" name="eid" id="empid"><br>
</div>
</form>
My web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('/showemployee',[EmployeeController::class,'show']);
Route::post('/showemployee',[EmployeeController::class,'store']);
My controller file -- EmployeeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;
class EmployeeController extends Controller
{
public function show()
{
$emp = Employee::all();
return view('showemployee',['employees' => $emp]);
}
public function store()
{
echo "I am in store";
error_log(request('eid'));
return redirect('/');
}
}
Route::post('/showemployee', [EmployeeController::class,'store'])->name('form.post');
and
<form method="POST" action="{route('form.post')}}" >
#csrf
<div class="form-group" style="margin-left:30px">
<label for="id">ID</label>
<input class="form-control" type="number" name="eid" id="empid">
</div>
</form>
if still not works
check with :
php artisan route:list

Laravel showing null value after form submit

My View:
<form action="/AddJuiceForm" method="POST">
#csrf;
<div class="form-group">
<label for="FruitName">Fruit Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Fruit Name"
name="fruitname">
</div>
<div class="form-group">
<label for="JuiceName">Juice Name : </label>
<input type="text"
class="form-control"
placeholder="Enter the Juice Name"
name="juicename">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
My Routes/web.php:
Route::post('AddJuiceForm','JuiceController#insertjuice ');
My Controller:
<?php
namespace App\Http\Controllers;
use App\JuiceModel;
use Illuminate\Http\Request;
class JuiceController extends Controller
{
public function insertjuice()
{
dd(request()->all);
}
}
Result: I am getting 'null' as output
Help me where I am going wrong?
Inject the Illuminate\Http\Request object into the insertjuice method and then call the all() method on the variable within your JuiceController class:
public function insertjuice(Request $request)
{
dd($request->all());
}

How to display the form's content

Problem with displaying content entered in the form in laravel.
What code to add to display the value entered in the form?
--web.php
Route::get('/show-name', ['uses' => 'NameController#show-name', 'middleware' => 'auth']);
--
NameContoller.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name()
{
return view('show-name');
}
}
--
show-name.blade.php
<?php
print_r($_POST);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Message after using the button - MethodNotAllowedHttpException.
I will be grateful for your help.
You're sending a POST request, not a GET request.
Route::post('/show-name', [
'uses' => 'NameController#show-name',
'middleware' => 'auth'
]);
The documentation tells you how to get all the data
https://laravel.com/docs/5.8/requests#retrieving-input
The documentation tells you have to pass data to a view https://laravel.com/docs/5.8/views#passing-data-to-views
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NameController extends Controller
{
public function show-name(Request $request)
{
$input = $request->all();
return view('show-name')->with('data', $input);
}
}
The documentation tells you how to access data passed into a view https://laravel.com/docs/5.8/blade#displaying-data
#php
echo print_r($data);
#endphp
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="">
<div class="required field">
<label>Name</label>
<input type="text" name="email" id="name">
</div>
<input type="submit" class="ui primary button" id="send" name="send" value="Send"></input>
</form>
Further more, the form won't work without CSFR https://laravel.com/docs/5.8/blade#forms
Inputs are self closing, meaning there is no need for </input>
Further more, there is no need for <?php echo $_SERVER['PHP_SELF']; ?> because with Laravel, you can specifically define the name of the route.
<form method="post" action="/show-name" class="">
Watch some basic tutorials on Laravel because you're going about this all incorrectly.

Laravel 5.5 - $request->file returns null

I have created a form that uploads a file but it returns a null value when I submit. When I add in the enctype="multipart/form-data" it reloads the page and doesn't seem to go through my controller.
MY HTML FORM
<form class="form-horizontal" role="form" name="importform" method="POST" action="{{ route('import_type') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="control-group">
<label class="control-label"> </label>
<div class="controls">
<div class="control-group text-center">
<label class="btn btn-primary" for="file-selector">
<input id="file-selector" name="template_upload" type="file" value="" required autofocus style="display:none" onchange="$('#upload-file-info').html(this.files[0].name)" required> Upload List </label>
<span class='label label-default' id="upload-file-info"></span>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" id="import-submit" name="import-submit">
</div>
</div>
</form>
MY CONTROLLER: I am using the import method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ImportTypeRequest;
use \App\Guest;
use \App\Role;
use \App\User;
use \App\Type;
use Illuminate\Support\Facades\Auth;
class GuestController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$user = User::with('roles')->where('id', Auth::id())->get();
$types = Type::where('user_id', Auth::id())
->where('active',1)->get();
return view('view_import',compact('user','types'));
}
public function import(ImportTypeRequest $request)
{
$template_upload = $request->file('template_upload');
dd($template_upload);
}
}
Here are some suggested ways trying to solve this.
First of all in your import method add dd($request->all()) at its top and see what's the response. You should see all your form data and of course template_upload file. That's how you make sure that you see all the coming data from your form to your controller method.
Then try to get rid of ImportTypeRequest and just use Illuminate\Http\Request to see what will you get. If you got different result then the problem is in ImportTypeRequest class.
Then why don't you just use $request->template_upload?! It's cleaner I guess.

Deletion Form For Laravel App

I am not sure why I'm receiving a NotFoundHTTPException when I click on my delete button in my Laravel application.
index.blade.php
<form action="/users/3" method="POST">
<input type="hidden" value="DELETE" name="_method">
<button class="btn btn-sm btn-icon btn-pure btn-default on-defaul" data-original-title="Delete" data-toggle="tooltip" type="submit">
</form>
UsersController.php
<?php
namespace App\Http\Controllers;
use App\User;
use Gate;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
public function destroy()
{
return 'yes';
}
}
routes.php
Route::get('users', ['as' => 'users', 'uses' => 'UsersController#index']);
Route::get('users/{user}', ['as' => 'users.show', 'uses' => 'UsersController#show']);
Route::get('users/{user}/edit', ['as' => 'users.edit', 'uses' => 'UsersController#edit']);
Route::delete('users/{user}', ['as' => 'users.delete', 'uses' => 'UsersController#destroy']);
Change the form:
<form action="/users/3" method="DELETE">
<button class="btn btn-sm btn-icon btn-pure btn-default on-defaul" data-original-title="Delete" data-toggle="tooltip" type="submit">
</form>
You are using POST and you haven't defined a route for the "DELETE" verb. Either define a post instead of a delete route (Route::post(...)) or change the form to user the delete method as described above.
index.blade.php
<form action="{{url('/users/3')}}" method="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button class="btn btn-sm btn-icon btn-pure btn-default on-defaul" data-original-title="Delete" data-toggle="tooltip" type="submit">
</form>
UsersController.php
<?php
namespace App\Http\Controllers;
use App\User;
use Gate;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
public function destroy($id)
{
return 'yes';
}
}
You should change:
<form action="/users/3" method="POST">
To the following:
<form action="<?= route('users.delete', [$user]) ?>" method="POST">

Categories