Laravel calls a different method than specified in Route function - php

I'm having troubles with something that I just can't explain. I'm working with Laravel and I just write a route for get method, no such a big deal, but for this route I'm calling a method called generatefile and Laravel is calling my index method which doesn't make sense. This is my code.
ReportController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\ReportTypes;
use App\Report;
use App\Hardware;
use App\Customer;
use App\User;
use App\ReportNotes;
use DB;
use PDF;
use Illuminate\Support\Carbon;
class ReportController extends Controller
{
public function index($type){
$data = new ReportTypes;
$datatype = $data->getData($type);
$hm = $datatype->report_type_includes_hardware ? true : false;
$customers = Customer::all();
$users = User::all();
$report = new Report;
$reports = $report->getData($type);
if($hm){
$models = Hardware::all();
return view('report/types/index',compact('datatype','hm','models','customers','users','reports'));
}
return view('report/types/index',compact('datatype','hm','reports','customers','users'));
}
public function generatefile(){
$data = ['title' => 'Welcome to HDTuto.com'];
$pdf = PDF::loadView('myPDF', $data);
return $pdf->download('hdtuto.pdf');
}
public function details($type, $folio){
$data = new ReportTypes;
$datatype = $data->getData($type);
$report_notes = new ReportNotes;
$type_id = DB::table('report_types')->where('report_type_serie','=',$type)->get()->first();
$report = DB::table('reports')->where([
['FK_report_type_id','=',$type_id->PK_report_type_id],
['report_folio','=',$folio]
])->first();
$notes = $report_notes->getData($report->PK_report_id);
return view('report/types/details',compact('datatype','notes','report'));
}
public function store(Request $request){
$report = new Report;
$data = new ReportTypes;
$report->FK_report_type_id = $request->RPType;
$report->report_folio = $data->nextFolio($request->RPType);
$report->FK_customer_id = $request->RPCustomer;
$report->FK_hardware_model_id = $request->RPHardware;
$report->FK_creator_user_id = $request->RPCreator;
$report->FK_responsable_user_id = $request->RPResp;
$report->FK_assistant1_user_id = $request->RPAssist;
$report->FK_report_state_id = 1;
$report->report_issue = $request->RPIssue;
$report->report_description_issue = $request->RPDescIssue;
$report->report_created_at = Carbon::now();
$report->report_attended_at = Carbon::now();
$report->report_updated_at = Carbon::now();
$report->report_finished_at = Carbon::now();
$report->save();
$data->incrementFolio($request->RPType);
return redirect()->route('reports',$request->RPTypeSerie);
}
}
My Route
Route::get('reports/getfile','ReportController#getfile')->name('report.generatefile');
Route::get('reports/{type}','ReportController#index')->name('reports');
And when I try to access this route, I get this:
I get the error in the index method, but I'm not calling it.
Can anyone please explain me why is this happening?

Put your route before to index route and try.
You can to check this question: Laravel Route issues with Route order in web.php

First, check your route.php if there is no duplicate route URI w/ the same method Route::get('reports/getfile'... or route name ->name('report.generatefile').
It is possible that you have 2 duplicate routes and you are using the route which uses ReportController#index.
Second, getFile function doesn't seem to exist in your ReportController which you are using in your route:
Route::get('reports/getfile','ReportController#getfile')->name('report.generatefile');

You can try
php artisan passport:install --force

Related

Laravel: set dependecy injection for Service like Controller

I have this code in my CompanyController
use App\Services\CompanyService;
public function store(CompanyService $companyService) {
$result = $companyService->store();
return response()->json($result);
}
And this code in my CompanyService
use stdClass;
use App\Company;
use Illuminate\Http\Request;
public function store(Request $request, Company $company) {
// this also not work
dd($request->all());
$data = new stdClass;
$data->status = 1;
$data->message = 'success';
return $data;
}
When i run this code, Laravel show error
Too few arguments to function App\Services\CompanyService::store() 0
passed but exactly 1 expected
I know that type hint dependecy injection issue, because it work in Controllers but not work in my CompanyService when i call store() without params
How can i fix this and make it work in my CompanyService ?
Edit:
You can use call() method of service container
In your case:
public function store()
{
$companyService = app(CompanyService::class);
$result = $companyService->call('store');
return response()->json($result);
}
Unfortunately it's not possible to method inject dependencies but you can inject your dependencies in your construct method, so in your case it would be like this:
use stdClass;
use App\Company;
use Illuminate\Http\Request;
class Foo
{
protected $request;
protected $company;
public function __construct(Request $request, Company $company)
{
$this->request = $request;
$this->company = $company;
}
public function store()
{
// this works
dd($this->request->all());
// Also injected
dd($this->company);
$data = new stdClass;
$data->status = 1;
$data->message = 'success';
return $data;
}
}
PS:
I believe laravel handles method injection in one of it's middlewares so as far as I know there is not possible way to perform method injection you can read about Service Container for more info

Import Model via Ajax Request Laravel - without namespacing the model

I am trying to import a model via an Ajax request without namespacing the model.
public function dataTypeRender(Request $request)
{
$input = request()->all();
$model = $request->input('model'); //this is the model name
$cols = $request->input('cols');
$modelTest = $model::all(); //not working
dd($modelTest);
}
Is there a way to do this? I'm trying to then do something with the model data.
I think without namespace it gonna be a little harder, because your model could be any model, but i think this could help you.
public function example(Request $request){
$data = $request->all(); //get All data request
$namespace = 'App\\'; //set namespace
$modelWithNameSpace = $namespace.$data['model']; //concat namespace and model name
$model = str_replace("'", "", $modelWithNameSpace); //remove quotes (idk if it's the best approach)
return $model::all(); //return the modell with all
}
This seemed to work nicely.
$model = 'App\\' . $request('model'); // adjust for the namespace/folder where you put your models
$data = (new $model)->all();
or
$data = (new $model)->find(1);

repository pattern in laravel doesn't seem to work

App::bind('App\Http\Repositories\languageRepository',
function( $app, array $parameters)
{
return new App\Http\Repositories\languageRepository($parameters[0]);
} );
Route::get('/test/{id}', 'testController#getme');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Repositories\languageRepository;
class test extends Controller
{
//
protected $language;
public function __construct(languageRepository $rep){
$this->language = $rep;
}
public function getme(){
$this->language->getMe();
}
}
When user accesses the route /test/5 for example, it goes to test Controller. what I'd like to do is that it should automatically pass my route parameter to App:bind function and automatically create languageRepository class with the constructor value passed as my route paramter. what happens is the code actually tells me $parameters[0] is undefined offset. why is that? I've tried App::make but then how do I pass the parameter from route to App::make?
You can accomplish this using the container's request instance, for query parameters:
App::bind('App\Http\Repositories\languageRepository',function($app)
{
$request = $app['request'];
$parameters = $request->all();
return new App\Http\Repositories\languageRepository($parameters[0]);
});
You can accomplish this using the container's request instance, for a route parameter:
App::bind('App\Http\Repositories\languageRepository',function($app)
{
$request = $app['request'];
$segment = $request->segment(1);
return new App\Http\Repositories\languageRepository($segment);
});

Laravel 5.2 - creating a model from a variable

I am trying to access dynamically models so that I can get the count for each of them depending on which string I pass to the function.
This is what my function in the controller looks like:
public function numberOf(Request $request){
$modelName = $request['option'];
$model = new $modelName;
$data = $model->count();
return json_encode($data);
}
But when I pass a string, like in this case 'Article' I get an error:
Fatal error: Class 'Article' not found
Even though I am calling it in the controller:
use App\Article;
I had to add App to model name, so that my function looks like this now and everything works fine now:
$modelName = 'App\\'.$request['option'];
$model = new $modelName;
$data = $model->count();
return json_encode($data);

Laravel 5 - Namespace + Dynamic Model

I have a very basic function to delete stuff on a simple website on Laravel 4.x that works like this:
public function delete()
{
...
$Model = Input::get('Model');
$Action = $Model::find($Id);
...
}
Now on Laravel 5, I'm trying to do the same but so far I can't because the namespaces. Since the $Model is dynamic I don't want to make use for everything.
And something like this:
use App\C\Models as Model;
public function delete()
{
...
$Action = Model\$Model::find($Id);
...
}
Simple do not works. What'd be the right approach to get this to work?
Simply store the namespaced classname as a string first:
$Model = Input::get('Model');
$NamespacedModel = '\\Model\\' . $Model;
$Action = $NamespacedModel::find($Id);
In the same case, My code is like this...
public function FunctionName(Request $request)
{
$modelName = $request->model;
$model = '\\App\\Models\\'.$modelName;
$q = $model::find($request->id);
$q->someColumn = 'someValue';
$q->save();
return back();
}
Note: I am using Laravel 8

Categories