Import Model via Ajax Request Laravel - without namespacing the model - php

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

Related

Laravel import model in function

I'm trying to import the model in function and call create function of the specific model.
I know which model to call from Route::currentRoute();, so it must look like
$model = Route::currentRouteName() // return like 'Blog/Post'
$create = new App\Models\$model::create($request->input());
Any ideas on how to do it in that way?
You must put the full model path into the string, then it will work.
Also make sure that you replace forward slashes with backslashes.
And if you use 'create' you don't need 'new'.
Here the final result:
$model = str('Blog/Post')->replace('/', '\\');
$fullPath = 'App\Models\\' . $model;
$create = $fullPath::create($request->input()]);
We can just use call it putting a full path of the model. for eg:
public function callModel(Request $request){
$create = App\Models\$model::create($request->input());
dd($create);
}

Laravel calls a different method than specified in Route function

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

Laravel calling model with variable in controller

I am using one function in controller for inserting in several different tables with different Models. I mean that in controller model is variable.
This code bellow works perfectly but i don't like syntax, i am pretty sure there must be some other ways than str_replace for calling model with \App\ in front of it's name.
Calling only by Model name without \App\ causes laravel error Class not found. I have written use \App\ModelName in controller's file but it still does't works.
public function storeCommon(Request $request){
$model = '"\App\"'. $request->model;
$model = str_replace ( "\"", "", $model ) ;
........
........
$row['text'] = $request->text;
........
........
$common = $model::create($row);
}
I would rather define array of possible models and use it within the code. This way you will protect your code against call for unwanted models and off course your code will be much readable:
protected $possibleModels = [
'Model1' => \App\Model1::class,
'Model2' => \App\Model2::class,
...
];
public function storeCommon(Request $request){
if (!isset($this->possibleModels[$request->model])) {
abort(404);
}
$model = $this->possibleModels[$request->model];
$row['text'] = $request->text;
...
$common = $model::create($row);
}

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