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

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.

Related

Undefined Variable Request in Laravel 5

I am having issues it says Request is undefined same goes for DB.
public function edit(Request $request,$id) {
$name = $request->input('stud_name');
DB::update('update student set name = ? where id = ?',[$name,$id]);
echo "Record updated successfully.<br/>";
echo 'Click Here to go back.';
}
Looks like you're missing use statements that tell your script where to find these. Add
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
in your file after namespace ...

Class '...\Input' not found

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.

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');

It can not find my variable in my view in laravel 5.6

<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\News;
use App\User;
use App\Event;
public function index()
{
$datanews['newss'] = News::orderBy('created_at','desc')->get();
$dataevent['events'] = Event::orderBy('created_at','desc')->get();
$user['users'] = User::all();
return view('user/aktuelles.index',$datanews, $dataevent,$user);
}
this is a part of my controller, i can show news and events in my view but everytime it says it can not find the variable user?
i dont know why because i dont know why it shows me the other variables but not the user variable???
in my view i can see the other variables like this:
#foreach ($newss as $news)
{{$news->newstitel}}
#endforeach
but i want to show user like this:
{{$user[1]->avatar}}
i tried everything so i tried so look my controller like this:
$user = DB::table('users')->get();
or get the variable via compact
and i tried exactly the same so like this
$user['users'] = User::orderBy('created_at','desc')->get();
and a foreach in my view but it can not found the variable user??
i dont know why
this is the error
Undefined variable: users
return view('user.aktuelles.index', compact('datanews', 'dataevent', 'user');
I am not sure how the helper view() handles the 4th parameter you are passing. According to the docs you can pass data like this:
return view('user/aktuelles.index',[
'datanews' => $datanews,
'dataevent' => $dataevent,
'user' => $user,
]);
or
return view('user/aktuelles.index')->with([
'datanews' => $datanews,
'dataevent' => $dataevent,
'user' => $user,
]);
Maybe give this way a try.
Edit:
The view() helper accepts 3 Parameters: $view = null, $data = [], $mergeData = [] so the 4th parameter you are trying to pass is not even working. You have to pass your data as second parameter or with the chained function with(array $data).
You have to return value in view like below method:
Change this line
$user['users'] = User::orderBy('created_at','desc')->get();
To
$user = User::orderBy('created_at','desc')->get();
return view('user/aktuelles.index', ['datanews' => $datanews,'dataevent'=> $dataevent,'users'=>$user]);
Now, You will get result inside your view using 'users'.
You are not passing variables to view correctly use with or compact to pass variables to view.
i prefer compact as it is simple and easier.
Passing data to view using compact:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\News;
use App\User;
use App\Event;
public function index()
{
$datanews = News::orderBy('created_at','desc')->get();
$dataevent = Event::orderBy('created_at','desc')->get();
$user = User::all();
return view('user.aktuelles.index',compact('datanews', 'dataevent','user'));
}
Passing variables to view using with:
public function index()
{
$datanews = News::orderBy('created_at','desc')->get();
$dataevent = Event::orderBy('created_at','desc')->get();
$user = User::all();
return view('user.aktuelles.index)->with(['datanews' => $datanews,'dataevent'=> $dataevent,'users'=>$user]);
}
You should try this:
use Illuminate\Support\Facades\DB;
use App\News;
use App\User;
use App\Event;
public function index()
{
$data['news'] = News::orderBy('created_at','desc')->get();
$data['events'] = Event::orderBy('created_at','desc')->get();
$data['users'] = User::all();
return view('user/aktuelles.index', $data);
}

Laravel How to get Route Profile based on Route Name

I have this route:
Route::get('/test',['as'=>'test','custom_key'=>'custom_value','uses'=>'TestController#index'])
I've been tried to use $routeProfile=route('test');
But the result is returned url string http://domain.app/test
I need ['as'=>'test','custom_key'=>'custom_value'] so that I can get the $routeProfile['custom_key']
How can I get 'custom_value' based on route name ?
For fastest way, now I use this for my question:
function routeProfile($routeName)
{
$routes = Route::getRoutes();
foreach ($routes as $route) {
$action = $route->getAction();
if (!empty($action['as']) && $routeName == $action['as']) {
$action['methods'] = $route->methods();
$action['parameters'] = $route->parameters();
$action['parametersNames'] = $route->parametersNames();
return $action;
}
}
}
If there's any better answer, I will be appreciate it.
Thanks...
Try this:
use Illuminate\Support\Facades\Route;
$customKey = Route::current()->getAction()['custom_key'];
I believe you are looking for a way to pass variable to your route
Route::get('/test/{custom_key}',[
'uses'=>'TestController#index',
'as'=>'test'
]);
You could generate a valid URL like so using
route('test',['custom_key'=>'custom_key_vale'])
In your view:
<a href="{route('test',['custom_key'=>'custom_key_vale'])}"
In your controller method:
....
public function test(Request $request)
{
$custom_key = $request->custom_key;
}
....
You can try one of the below code:
1. Add use Illuminate\Http\Request; after namespace line code
public function welcome(Request $request)
{
$request->route()->getAction()['custom_key'];
}
2. OR with a facade
Add use Route; after namespace line code
and use below into your method
public function welcome()
{
Route::getCurrentRoute()->getAction()['custom_key'];
}
Both are tested and working fine!

Resources