can anyone tell me how to override actioncreate and actionupdate method yii2 rest api..
class CabController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Cab';
public function actions(){
$actions = parent::actions();
unset($actions['create']);
unset($actions['update']);
return $actions;
}
public function actionCreate(){
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$model = $this->modelClass;
$model->load(Yii::$app->request->post());
$cur_time = date('Y-m-d H:i:s');
$model->date_created = $cur_time;
$$model->save(false);
}
If i do like this mean i got error like 500 internal server error and error message like Call to a member function load() on a non-object ..how to solve this issue..
Thanks...
You are not creating object, instead you are assigning string api\modules\v1\models\Cab to $model variable. Change assigning part to:
$model = new $this->modelClass;
In case of "Class not found" error add leading backslash to class name: \api\modules\v1\models\Cab.
Related answers about creating object from string:
Creating PHP class instance with a string
Dynamically create PHP object based on string
How to create object from String?
Related
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;
}
...
}
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
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);
There is a model:
class Model extends ActiveRecord
{
public static function model($className=__CLASS__) {
return parent::model($className);
}
public function toSave(Array $data)
{
$this->setAttributes($data);
$this->save(); // returns true
return $this;
}
}
and running
$model = Model::model()->toSave($data);
and when im dumping $model there is all data which setted from $data but not exists PrimaryKey (id).
but, if i run
$model = new Model;
$model->toSave($data);
works as expected.
Where is a problem?
you are doing multiple save, through iterating, and pass new set of $data everytime. $model here is an object of single record. So by doing everytime, new model , you are creating fresh new object, assign data and save. Later you did is the right approach.
You usage in invalid in the first instance
$model = Model::model()->toSave($data);
In this case, the usage is calling the toSave() method statically.
First, the usage is illegal unless you change your declaration
public static function toSave(Array $data) { ... }
In addition, when invoked statically, the value for $this is invalid.
Therefore, the valid usage is your second version:
$model = new Model;
$model->toSave($data);
References:
http://php.net/manual/en/language.oop5.static.php
New to Laravel and still fresh to OOP! I'm assuming this has more to do with OOP than strictly Laravel.
So my main problem is that I am trying to pass all rows from a database table called 'fin_income_category' via a method in my model called Income to a controller called PlannerController. To do this I have created a static method within Income called getIncomeCategories()
First of all, here is my __construct method within Income:
public function __construct($income, array $attributes = array()){
parent::__construct($attributes);
$this->table = $income;
}
And here is the getIncomeCategories method also within Income:
public static function getIncomeCategories(){
$category = new self('fin_income_category');
$categories = $category->all();
return $categories;
}
Finally, here is the edit($id) method within the PlannerController where I am to call this method and pass the categories along to my view. Note that only the first statement in this function is the one in question...the others work fine:
public function edit($id)
{
$income_categories = Income::getIncomeCategories();
$newIncome = new Income('fin_income');
$newRecord = $newIncome->where('id', '=', $id)->get();
return View::make('planner.edit', array('record'=>$newRecord, 'categories'=>$income_categories));
}
When I run the code like this I receive an error from Laravel:
ErrorException
Missing argument 1 for Income::__construct(),
called in /opt/lampstack/frameworks/laravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 615 and defined
In other cases where I have instantiated a new Income I have not received this error.
Change the getIncomeCategories() to this
public static function getIncomeCategories(){
$category = new self('fin_income_category');
return $category->get()->toArray();
}
The reason why your code didn't work is because the eloquent all() found in
Illuminate\Database\Eloquent\Model; the code snippet is found below
public static function all($columns = array('*'))
{
$instance = new static;
return $instance->newQuery()->get($columns);
}
is instantiating a static class which is bound to the called class which is your Income Model Class in your case, and in the process requesting the arguments in the constructor