Laravel 5.6 passing eloquent model as parameter to a function - php

I have a selection control on a blade form that is to be refreshed via ajax through this function:
function getOpciones(tbName) {
$.get('/ajax/read-data/' + tbName, function(data){
return (data);
});
}
The function takes a string variable 'tbName' whith the name of the table the control is related to, and passes it on as a parameter to the route:
Route::get('/ajax/read-data/{modelo}', 'AjaxController#readData');
Then the controller should get the parameter {modelo}, and retrieve the records in that table:
use App\RegFiscal;
public function readData($modelo) {
$arreglo = $modelo::all();
return response($arreglo);
}
But even though I am referencing the model with 'use App\RegFiscal', all I get is this error in laravel log:
2018-03-23 18:52:08] local.ERROR: exception
'Symfony\Component\Debug\Exception\FatalErrorException' with message
'Class 'RegFiscal' not found' in
C:\wamp64\www\laravel\cte\app\Http\Controllers\AjaxController.php:32
I´m new to Laravel, so needless to say I am lost and any help would be greatly appreciated. Thanks!

Just because you use App\RegFiscal doesn't mean $modelo is associated with it.
What you can do, though, is use app("App\\$modelo") to load in your model based on the parameter you get from the router. You would no longer need to use App\RegFiscal either.
$arreglo = app("App\\$modelo");
return response($arreglo::all());
This is assuming your model is stored in the default app directory within your Laravel project. If not you can change "App\" to where ever it is stored. If for example your model is in app\models\modelname.php it would be "App\Models\\$modelo".

You can do this as the following:
public function readData($modelo) {
$modelName = '\App' . '\\' . $modelo;
$class = new $modelName();
arreglo = $class::all();
return response($arreglo);
}

To those like me who wanted to inject it on a constructor, here's how to do it:
~$ php artisan make:provider MyProvider
Then override the register function like so:
class MyProvider implements ServiceProvider {
/** #override */
public function register() {
$this->app->bind(ShapeInterface::class, function ($app) {
return new Square($app->make(MyModel::class));
});
}
}
The ShapeInterface is a simple interface and Square is a simple class that implements the shape interface with a constructor parameter of the eloquent model.
class Square implements ShapeInterface {
private MyModel $model;
function __construct(MyModel $model) {
$this->model = $model;
}
...
}

Related

I can't inject two or more class on my service provider

I am currently implementing repository pattern and dependency injection on my laravel project. But when I inject two or more class, I got this error
Too few arguments to function App\Repositories\UserRepository::__construct(), 0 passed
I don't know what's wrong but I think I did it correctly. Here's my code:
My service provider:
public function register()
{
$this->app->bind(LoanRepository::class, function() {
return new LoanRepository(new Loan, new UserRepository);
});
}
and here's my Repository
public function __construct($loan, $userRepository)
{
$this->loan = $loan;
$this->userRepository = $userRepository;
}
does anyone experience this?
The error is telling you that the UserRepository class is expecting some arguments that you are not passing it. Below would be an example of how you would pass a constructor argument to your new UserRepositiry call.
public function register()
{
$this->app->bind(LoanRepository::class, function() {
return new LoanRepository(new Loan, new UserRepository($constructorArgsHere);
});
}
I haven't worked with Laravel so I don't know what the UserRepository class constructor is looking for as arguments, but that should be an easy thing to find out just looking at the file where that class is defined or a quick google search.

How to pass parameter to a laravel elequent model's event observer

I have a model in laravel and I want to do something after the first time which an object of my model is created. the simplest way is to add a static boot method inside my model's class like the code below:
class modelName extends Model
{
public static function boot()
{
parent::boot();
self::created(function ($model) {
//the model created for the first time and saved
//do something
//code here
});
}
}
so far so good! the problem is: the ONLY parameter that created method accepts is the model object itself(according to the documentation) :
Each of these methods receives the model as their only argument.
https://laravel.com/docs/5.5/eloquent#events
I need more arguments to work with after model creation. how can I do that?
Or is there any other way to do something while it's guaranteed that the model has been created?
laravel version is 5.5.
You're close. What I would probably do would be to dispatch an event right after you actually create the model in your controller. Something like this.
class WhateverController
{
public function create()
{
$model = Whatever::create($request->all());
$anotherModel = Another::findOrFail($request->another_id);
if (!$model) {
// The model was not created.
return response()->json(null, 500);
}
event(new WhateverEvent($model, $anotherModel));
}
}
I solved the issue using static property in eloquent model class:
class modelName extends Model
{
public static $extraArguments;
public function __construct(array $attributes = [],$data = [])
{
parent::__construct($attributes);
self::$extraArguments = $data ;
public static function boot()
{
parent::boot();
self::created(function ($model) {
//the model created for the first time and saved
//do something
//code here
self::$extraArguments; // is available in here
});
}
}
It works! but I don't know if it may cause any other misbehavior in the application.
Using laravel events is also a better and cleaner way to do that in SOME cases.but the problem with event solution is you can't know if the model has been created for sure and it's time to call the event or it's still in creating status ( and not created status).

Laravel service container - registering an object shared to all services

I have tried to register to the container an Uuid and i have tried to retrive it from a route controller more than once, but the uuid value is not the first registered.
Can anyone help me to understand?
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
if(App::bound('conf')==NULL)
App::instance('conf', Uuid::generate()->string);
}
}
class InstanceController extends Controller
{
public function getUuid()
{
return App::make('conf');
}
}
I need to register an unique value or object that will be accessible to all.
I have also tried to put this code:
config(['uuid' => Uuid::generate()->string]);
in Laravel command handle method:
class RegisteredInstances extends Command
{
public function handle()
{
config(['uuid' => Uuid::generate()->string]);
}
}
and execute it, but when i try to retrive the uuid from a service, the response is null.
Now i have registered a laravel command that do this:
class RegisteredInstances extends Command
{
.
.
.
public function handle()
{
if(App::bound('conf')==NULL)
App::instance('conf', Uuid::generate()->string);
if(config('uuid2')==NULL)
config(['uuid2' => Uuid::generate()->string]);
}
}
A task every minute execute this command and i try to retrive the uuid from a service controller like this:
class InstanceController extends Controller
{
public function getUuid()
{
return App::make('conf');
}
public function getUuid()
{
return config('uuid2');
}
}
The problem, in this case, is that the controller return NULL:
You need to use laravel Configuration (accessing-configuration-values) with AppServiceProvider
Example:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
config([ 'theconfig.uuid' => $class_here->UUID ]);
}
}
Then to use it, call
config('theconfig.uuid');
anywhere in the program
Is this in Laravel 4? I haven't seen the App::instance markup before, but I found it in Laravel 4.2 docs for the IoC Container.
This looks like a case for using a singleton. You can use this to ensure that conf is only resolved once. Looking at 4.2 docs, you could define your singleton as follows.
App::singleton('conf', function()
{
return Uuid::generate()->string;
});

Laravel extend TestResponse class

I'm trying to add a custom assertion to the TestReponse class so I can make something like this:
$response = $this->json('POST', '/foo/bar');
$response->myCustomAssertion();
I tried creating an App\TestResponse class that extends the original one and then binding it in the App\Provider\AppServiceProvider class.
public function register()
{
$this->app->bind('Illuminate\Foundation\Testing\TestResponse', function ($app) {
return new App\TestResponse();
});
}
But $response->json() is still returning the original one and not my own implementation.
How can I extend the TestResponse class?
If you want a little more fine-grained control, you can also extend the Illuminate\Foundation\Testing\TestResponse, as you have done, and then override the createTestResponse method in your TestCase class to return an instance of your custom response class:
// Laravel 8 and above
protected function createTestResponse($response)
{
return tap(App\TestResponse::fromBaseResponse($response), function ($response) {
$response->withExceptions(
$this->app->bound(LoggedExceptionCollection::class)
? $this->app->make(LoggedExceptionCollection::class)
: new LoggedExceptionCollection
);
});
}
// Before Laravel 8
protected function createTestResponse($response)
{
return App\TestResponse::fromBaseResponse($response);
}
From Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.
The TestResponse class uses the Macroable trait so you can add macro functions at runtime.
TestResponse::macro('nameOfFunction', function (...) {
...
});
You can add this to a Service Provider's boot method or somewhere before you need to make the call to that macro'ed method.

Laravel attach returns undefined method for belongsToMany relationship

I would like to create a question which has many surveys. In the questions Model:
public function surveys()
{
return $this->belongsToMany(Survey::class, 'survey__surveyquestions');
}
And in the controller when saving a new question:
private $questions;
public function __construct(QuestionsRepository $questions)
{
parent::__construct();
$this->questions = $questions;
}
public function store(Request $request)
{
$this->questions->create($request->all());
$this->questions->surveys()->attach($request->surveys);
return redirect()->route('admin.survey.questions.index')
->withSuccess(trans('core::core.messages.resource created', ['name' => trans('survey::questions.title.questions')]));
}
But I get the following error when it gets to the attach line:
(1/1) FatalErrorException Call to undefined method
Modules\Survey\Repositories\Eloquent\EloquentQuestionsRepository::surveys()
I notice the error mentions EloquentQuestionsRepository but I have added no methods in there so it's just an empty class:
class EloquentQuestionsRepository extends EloquentBaseRepository implements QuestionsRepository
{
}
QuestionRepository:
interface QuestionsRepository extends BaseRepository
{
}
As explained in the response to the main post - the constructor resolves the QuestionsRepository to instance of EloquentQuestionsRepository, which by the look of it is not what the store method needs.
What I would probably do is to make call to create method directly on the model and remove constructor all together - that is unless you need the instance of QuestionsRepository anywhere else in your controller:
public function store(Request $request)
{
$question = Question::create($request->all());
$question->surveys()->attach($request->surveys);
...
}
Also - I'm not sure passing $request->all() is the best thing to do - I'd probably use $request->only(...) or $request->all(...) specifying which items you want to get from the request rather than passing everything from the request to the create method.
On the other note - you could also use Form Request, which would validate data for your before passing it to the store method.
https://laravel.com/docs/5.5/validation#form-request-validation

Categories