JShmay
JShmay

Reputation: 170

Where do we write functions that access the database in laravel 5?

I have a question concerning the laravel framework.

I've been checking out the laracast website that's been helping me start up and also a few resources here and there.

I am sorry for the noob question that follows but may I ask where I should be writing functions whose role is to modify data in the database ? Functions that would normally use the Eloquent ORM created models and issue the needed changes in the DB. So far what I'm doing is that I created a folder in the App directory called "classes" and I'm putting the utility classes there, but I'm not sure if it's following standards imposed by laravel 5.

Also concerning transactions, should I do it in those created functions ? or would it be best to call procedures made in the database ?. In my personal opinion and experience, I prefer creating procedures for database transactions but I still prefer to figure out the right way for this.

Thank you all !

Upvotes: 1

Views: 144

Answers (2)

Zaw Myo Htet
Zaw Myo Htet

Reputation: 540

I want to recommend you to read this book From Apprentice To Artisan .

I really like this concept.

Controllers and routes serve as a mediator between HTTP and your application. When writing large applications, don’t clutter them up with your domain logic.

So, if someone ask me where to make database operation, I will say "Model" in general. But my answer may different depending on business logic that I need to do and how large my application will be. Laravel already provide a beautiful way to do transactions. I found there are so many people who love database procedure, if you prefer to use database procedures, it is up to you.

In Engineering, every answer may true until certain time. And laravel is flexible enough to apply your own favourite pattern. It is a kind of freedom and a kind of dangerous. :-) ( Other people may have different opinion ).

Upvotes: 0

Hailwood
Hailwood

Reputation: 92691

The correct answer is wherever you want, but of course that doesn't help users who don't have experience working with these types of frameworks before so a more opinionated answer follows (for the latest version of the framework L5 at the time of writing).

In Laravel5 if you issue the artisan command php artisan make:model then the generated model gets stored in /App/* so it's a pretty good starting point. by having your models there you can do new /App/User(); and the PSR autoloader will pick this up and autoload it for you. Of course if you feel this is too generic then you can store them in /App/Models/* - change the namespace of the model and reference it via new /App/Models/User();.

One important thing to realize is that in MVC frameworks Models are quite ambiguous and don't relate only to the database, but really any external database. The Model is a mapping of the external data to a format your application can work with which is why Models don't jave a dedicated directory anymore.

For general "libraries" I would say - if it's reusable then create a composer package. If it's not then create /App/DescriptiveNamespace/*.

Upvotes: 1

Related Questions