A single function that does CRUD in Laravel - laravel

I want to create a function for doing all kind of CRUD operation. This function will be called from any controller's inside a function. Then the function will search for a table/Model, then based on operation type it will do the rest things.
It will be good to implement this kind of function that can be used commonly? It will increase the complexity of CRUD? Or, should do the CRUD in controllers' functions?
My project is pretty big. Im thinking to write a function to do CRUD.

Welcome to stackoverflow.
You should consider using a controller, like ProductsController, and inside it, you should have a funcion for every action (store, update, etc).
Doing all of this in a single function only adds unnecessary complexity to the project and this isn't how things should be done.
As a starting point, execute this command on you project: php artisan make:controller ProductsController --resource. This will create an well structured controller for Products CRUD actions.
Hope you find this helpfull.

Related

Generic Model Based CRUD API Laravel

Is there a built in or library based way to implement generic Eloquent/Model based views in Laravel for simple CRUD endpoints?
At the moment I am writing the logic for index, store, destroy, update manually, but all the code is essentially the same.
e.g.
public function destroy($id)
{
$customer= CustomerInfo::find($id);
$customer->delete();
}
I'm more used to Django and the DRF which implements a ModelViewSet class which handles all (most) of the logic for simple CRUD applications.
Implement generic Eloquent/Model based views in Laravel for simple CRUD endpoints
I'm not sure about that but Laravel provides route to model binding, which shortens your code. There's also resource controller which is already pretty much declare all needed methods for you. What's left is quite minimal. So after using model binding and resource controller, your destroy() method may look like:
public function destroy(CustomerInfo $customerInfo)
{
$customerInfo->delete();
}
But the method name and its agrument is already declared like a placeholder, you just write delete() line. More about model binding and resource controller
Use single artisan command to have CRUD endpoints all in one.
php artisan make:controller PhotoController --resource
Because of this common use case, Laravel resource routing assigns the typical create, read, update, and delete ("CRUD") routes to a controller with a single line of code. To get started, we can use the make:controller Artisan command's --resource option to quickly create a controller to handle these actions:

Laravel do i need two controllers to handel two store functions?

Basically what i want to do is that 1 Controller handels two store functions called like this:
public function singleupload(){
..some code
}
and
public function multiupload(){
..some code too
}
as i continued to define the routes (get/post)
Route::get('/MultiUpload', 'controller#MultiUpload');
Route::get('/SingleUpload', 'controller#SingleUpload');
Route::post('/MultiUpload', 'controller#storeMulti');
Route::post('/SingleUpload', 'controller#storeSingle');
and as i tried to post some data to my database it tells me that there is no 'store' function. So i opened the docs and found this:
POST /photos store photos.store
So my question is can i create two store functions in one controller or do i have to create a different one so every route has its own controller with its own store function?
You are doing some things wrong.
First of all follow Repository Pattern.
You should always write all common functions in Repository which can be accessible in entire Project.
You should use controller only to fetch the request from the Route and pass all the logic to the Repository.
These Process will help you reduce all you coding lines.
Hope this helps !!!
cheers!!
NO you don't need to create new Controller. You can add new action for this.
But it also, depends on the how is your application functionality.
Normally, i personally recommended to create generic function or traits or add the functionality in base controller.
you can handle multiple store functions in one controller there is no need to create 2 different controllers.

Laravel Create Function to be used in different Controllers/in the same Controller

It's more a general question, I want someone to point me to the direction I should go.
1) FUNCTION FOR SAME CONTROLLER: I have two methods: Store and Update in the same controller. They both should contain some complex request validation (which I can't do via validator or form request validation because it's too complex). This means for me now using the same code twice in two methods... How can I take this code, create a function and use it in both Store and Update methods just with a single line, like:
do_this_function($data);
2) FUNCTION FOR DIFF. CONTROLLERS: I have another code which I use in many different Contollers. It transliterates Russian letters into Latin ones ('Сергей' = 'Sergey' and so on). How and where should I register this function to be able using it in different Contollers?
transliterate_this($data);
I have read something about helpers. Should I use them? If you an experienced Laravel develper and say so, I will read everything about them. Or, if you advice something else - I'll read about that.:) I just don't want to spend time reading about something useless or "not right way to-do-it".
Appreciate any help!
1) You could create a form request validation or you could just create a private function that would handle the validation and return the result.
2) You can create a helpers.php file, see here, and you put your translation function inside. Then you can call it from anywhere:
In a controller transliterate_this($data);
In a view {{ transliterate_this($data); }}.
You can do complex validation even inside a FromRequest. You can override the getValidatorInstance for example, or any other method from the base class to plug your validation logic on top of the basic validation rules. Else just consider making an external class to handle that complex validation and inject it in your controllers using Laravel's IoC container.
You should create a facade to provide that feature. Then you can easily use it anywhere (and additionally create a helper method for it if that makes you feel better). Something like Transliterate::toLatin($str)
everyone! Thank you all for great answers. But I have discovered that the problem was that I didn't know anything about Object-Oriented Programming.
(Ans I was working in Laravel:)).
After I took an Object Oriented Bootcamp from Laracasts, I started 'seeing' how Laravel actually works and I know can easily create methods inside classes and use them in other classes.
https://laracasts.com/series/object-oriented-bootcamp-in-php
(of course, you can read something else on OOP, but Jeffrey Way has really outstanding explanation talent!)

Can i use instances of a laravel model instead of facades?

I'm wondering whether it is possible/advisable to use instances of a laravel model instead of using the Facade. Why all this trouble? I have a model which will be used with many tables, and i want to be setting the model's table automatically using the constructor. Is it possible/advisable, or what is the best approach of achieving the same end?
I have researched around with no much success.
UPDATE
THis is the scenario: an exam system, where different exams are "created". after an exam is created, a table is created in the database under the name Exam_#, where # is the ID of the exam. I want to access all exam from one model: Exam, but you see the particular table the model is to use can vary significantly, so we cannot set the table variable statically. The model shall not know the table it will use until it(the model) is called. So thats why i was wondering whether i can be passing the ID of the exam when i am calling the model or something like that. I hope my question is now more clear.
At the end of this, Laravel is still PHP... Anything you can do in PHP can be done in Laravel.
is (it) possible/advisable to use instances of a laravel model instead of using the Facade?
You can achieve exactly the same results using an instance of the model as you would using the static facade.
$user = User::find(1);
$user2 = new User();
$user2 = $user2->find(1);
Both instances of the above model contain the same results.
Is it advisable? I really don't like the static facades at all, they bring with them more trouble than they are worth, especially when it comes to testing (despite being able to mock them, they create tight coupling where most of us need loose coupling). My answer to this would be: don't use the facades at all.
What is the best approach of achieving the same end?
As #JoelHinz suggested, create a base model with common properties and then use the models as they are intended. i.e. ONE table to ONE model and create the relationships between them. Don't use the same model for multiple tables, this is not how Laravel models were intended and you will lose a lot of the power Eloquent provides by taking the approach you mentioned.
Updates from comments
To get you started with testing in Laravel this is a good end to end tutorial Tutsplus Laravel4 + Backbone. Ignore the backbone part, what you're interested in is the testing parts that start about a 1/3rd of the way down the page. This will get you testing controllers straight away and introduce you to the repository pattern to create testable DAL structures.
Once you get the hang of writing tests, it becomes very easy to write a unit test for anything. It may seem like a scary subject, but that is purely down to not understanding how it works, it really is quite simple. Take a look at the PHPUnit documentation as well, it is an excellent resource.

yii writing a resuable code for cascade dropDownMenu

I wrote a three cascade dropDownLists that its listData are generated from the database models.
The lists are generated with an Ajax call to action in the controller based.
I want to reuse this code and to share it with more pages.
I tried to do the following:
Write it as a Custom Widget.
currently i use 'createurl' function that calls a function in the matching controller.
I cant write JavaScript since i want to use the existing db models.
In this case i need to write the action functions in an independent file - so should i write a controller? where should i place it?
Write it as a part of a module - but it seems overkill.
any suggestions, i am sure that there is a right and simple way to do it.
You could create it as a helper. A helper is just a class in the components which has no direct action in the M->C->V action flow but can be used in any controller, model, view, component, module, etc...
I would write a helper method to call it from the controller.
Another suggestion could be to extend CController to your own base controller and have your actual controllers, extend from your custom base controller. That way you can make it easily available in every controller, and then you just set some members that contain the models to use which you set in the actual controller.
If you need more help on this, find me on freenode #yii

Resources