Class '...\Input' not found - laravel

I want to implement a search option on my Laravel application, but had this error:Class 'Illuminate\Support\Facades\Input' not found, I have tried to add this row at config/app like this:
'aliases' => [
....
'Input' => Illuminate\Support\Facades\Input::class,
Also at Controller I have added those rows:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
At Route I have added
Route::any('/search',function(){
$image_tmp = $request->image;
$fileName = time() . '.'.$image_tmp->clientExtension();
$q = Input::get ( 'q' );
$book = Book::where('title','LIKE','%'.$q.'%')->get();
if(count($book) > 0)
return view('home')->withDetails($book)->withQuery ( $q );
else return view ('home')->withMessage('No Details found. Try to search again!');
});
But still it doesn't work.

Try this
config/app.php
use Request instead of Input
'aliases' => [
....
'Input' => Illuminate\Support\Facades\Request::class,
And your controller
use Illuminate\Http\Request;
and remove use Illuminate\Support\Facades\Input; top of your code

you have used the class in the controller but your route is never going to one as you are using a closure. so add Input class in your web.php file. at the top of your web.php file add
<?php
use Illuminate\Support\Facades\Input;
if you are using any latest version of laravel, the Input class no longer exists. so use Request class instead
<?php
use Illuminate\Http\Request;
you can use request() global helper too to get request values.
however i would suggest you to not use closure, rather use controller for logical operation.

Related

How to declare global variables available to all Blade Files? [duplicate]

This question already has answers here:
Laravel 5 - global Blade view variable available in all templates
(9 answers)
Closed 1 year ago.
I have products in a database that are essentially used on every single page. Instead of having to query the database, something like this:
$products = DB::table("products")->get();
And then passing it into view:
return view("site.products", array(
'products' => $products,
));
I don't want to do this for every view. Instead, I want $products to be available to ALL templates by default... so that I can do this:
#foreach ($products as $product)
... etc
How would I declare it in a global way to achieve this?
You can add below code in the boot method of AppServiceProvider
public function boot()
{
if (!$this->app->runningInConsole()) {
$products = DB::table("products")->get();
\View::share('products', $products);
}
}
Read more from here
A recommended way of doing this is to add a middleware that you apply to all the routes that you want to affect.
Middleware
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;
class GlobalVariablesMiddleware
{
$myVariable = "Value For Everyone";
View::share(['globalValue' => $myVariable]);
}
Add it to your kernel.php in the Http folder
protected $routeMiddleware = [
...
'myMiddleware' => \App\Http\Middleware\ GlobalVariablesMiddleware::class,
];
Once this is setup, you can easily apply it to individual routes or grouped ones to achieve what you are looking for
// Routes that will have the middleware
Route::middleware(['myMiddleware'])->group(function () {
// My first route that will have the global value
Route::resource('/profile', App\Http\Controllers\ProfileController::class);
// My second route that will have the global value
Route::resource('/posts', App\Http\Controllers\PostController::class);
});
By doing it this way, you can easily control the data in the future if you would chose not to have the data global.

having a problem about my codes in PagesController

Hi I'm having a problem about my codes in my PagesController.
I'm wondering what is the error about the notification it says undefined variable but it has the same codes in countOrder and countProduct and those two are working just only the notification
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\CustomerOrder;
use App\Product;
use App\Notification;
use DB;
class PagesController extends Controller
{
public function count() {
$countOrder = CustomerOrder::count();
$countProduct = Product::count();
$notification = Notification::count();
return view('/adminIndex',['customer_orders' => $countOrder],['products' => $countProduct],['notifications' => $notification]);
}
}
There may be a few issues here.
Firstly, when passing data to a view, you must use one array (rather than several):
return view('/adminIndex', ['customer_orders' => $countOrder, 'products' => $countProduct, 'notifications' => $notification]);
Secondly, the first argument of the view() helper expects the view file (found in the /resources/views folder). So if the file is adminIndex.blade.php use:
return view('adminIndex', ['customer_orders' => $countOrder, 'products' => $countProduct, 'notifications' => $notification]);
Hope this helps.

Moving project online broke laravel controller

That's my route:
Route::group(['prefix' => 'admin', 'middleware'=>['auth','role:admin']], function () {
Route::get('/co2index', 'UserController#adminCo2Index');
}
This is the controller method that fails:
<?php
namespace App\Http\Controllers;
use App\Http\Impl\ReferentManager;
use App\Http\Impl\RoleManager;
use App\Http\Impl\UserManager;
use App\Http\Impl\ValidationRulesManager;
use App\Models\User;
use App\Notifications\UserActivatedNotification;
use App\Models\Vendita;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Referent;
use App\Models\Ddt;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Session;
class UserController extends Controller
{
public function adminCo2Index()
{
$search = \Request::get('search'); //<-- we use global request to get the param of URI
$companies = User::where('name', 'like', '%' . $search . '%')->orderBy('name')
->paginate(10);
$ddts_count = DB::table('ddts')
->select('company_id', DB::raw('count(*) as total'))
->groupBy('company_id')
->get();
if ($companies && $ddts_count) {
return view('administration.co2Index')->with('companies', $companies)->with('ddts_count', $ddts_count);
} else {
return view('administration.co2Index')->with('companies', null)->with('ddts_count', null);
}
}
}
on my online server if I try to visit:
mysite.com/admin/co2index
it returns:
BadMethodCallException Method [adminCo2Index] does not exist. in
Controller.php line 82:
On localhost it works! Also, I have other methods on UserController class that work even online without any problem!
If I put 'null' on $companies or $ddts_count the correct empty view is loaded on localhost. If I do the same thing online I have still the same error!
If I put a dd('ciao') on the top of the method the error is still showed and no message on front end...
This looks very strange to me! I can't see any typos... thanks for the help!
hit this command
composer dump-autoload

DOM/PDF error - Non-static method Barryvdh\DomPDF\PDF::download() should not be called statically

My Controller is this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade as PDF;
class PrintPDF extends Controller
{
public function print(){
$details =['title' => 'test'];
$pdf = PDF::loadView('textDoc', $details);
return $pdf::download('this.pdf');
}
}
My routes
Route::get('/print', 'PrintPDF#print');
when accessing localhost/print I get an error
Non-static method Barryvdh\DomPDF\PDF::download() should not be called
statically
I followed the install instructions on their site. I have tried to change my controller adding use PDF, instead of use Barryvdh\DomPDF\Facade as PDF; Yet the error persists
This happens because you are namespacing the wrong PDF class.
You are namespacing Barryvdh\DomPDF\PDF and try to use this class as "Facade" which is wrong.
To solve your problem
Set namespace to the facade:
use Barryvdh\DomPDF\Facade as PDF;
Function Should be like this
$details =['title' => 'test'];
$pdf = PDF::loadView('Your view path', $details);
You can download directly using download method
return $pdf->dowload('test.pdf');
or you can save in specific directory of your project
return $pdf->save('path of your directory/filename.pdf');
This worked for me....
make the function call like this
return $pdf->download('invoice.pdf');

why method post not found in Illuminate\support\facade\input

I work with laravael 5.3.9 .
In my controller I use
Illuminate\Support\Facades\Input;
But when I try to get input from a user form using method post like that :
function add(){
$fullName = Input::post('fullName' , 'test');
I get this error .
the only method that Input class has is "get" .
I dont want that in my system I want to work with method post , delete , put ....
I guess, Input::post method is not used with L5.3. Use the Request facade OR $request to get your input variable.
Try this in your controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class area_owners extends Controller
{
function add(Request $request)
{
// I assume all these input variable have same name in you FORM.
$fullName = $request->input('fullName');
$smsCode = $request->input('smsCode');
$authorizationId = $request->input('authorizationId');
$areaNumber‌​ = $request->input('areaNumber‌​');
$neigh_project_Id = $request->input('neigh_project_Id');
$area_owners = DB::table('area_owners')
->insert(['fullName'=>$fullName,
'smsCode'=>$smsCode,
'authorizationId'=>$authorizationId,
'are‌​aNumer'=>$areaNumber‌​,
'neigh_project_Id'=‌​>$neigh_project_Id])‌​;
return view('area_owners_add', ['area_owners' => $area_owners]);
}
}
Let me know if it helps.

Resources