Using Model Functions in Views in laravel 5 - laravel-5

I have a project where i have a decent amount of Eloquent Models,
on most of the pages I need to use multiple models and their functions to build the page correctly.
I am used to this syntax:
use App\CustomFolder\CustomModel;
CustomModel::all(); // or whatever function / data I need
Though as said I have about 27 Models now and I need multiple ones on every page. So I created a blade layout (master page) where I link all these models, but now it seems these Models can't be called from the view itself.
So how can I either:
Make these Models globally available (preferred)
Make it so the models that I call (use Model;) in the layout are also available from my view.

You actually should get all your resources in the controller methods and not in your views. This goes against the MVC pattern Laravel and Eloquent both use.
In the controllers you can add and use them with the normal syntax:
use App\CustomFolder\CustomModel;
CustomModel::all(); // or whatever function / data I need
Learn more about using controllers in Laravel here.
EDIT
To not have to always type Use Path/To/Model in every controller, you can add the models to aliases in config/app.php. That way you can add for example 'CustomModel' => App\Path\To\CustomModel and use it in your controller methods as \CustomModel.

Although its a bad practice and will void MVC. But you can do like this inside php tags or {{}} braces in blade.
\App\Models\ModelName::get();

Related

How to refactor laravel application code effectively

I'm in a position to refactor my laravel 5.4 app codes due to some problems: Code complexity, lack of oo design experience etc.
I have read that controller codes should be as short as possible (it is claimed that they are glue code parts).
Q1: I have some library like codes. These library functions/classes should be in the related Model classes, or should I have extra classes which make use of Model classes?
Q2: If I don't need extra classes (so model classes are enough), should I call these business logics from blade files, or from controllers? (Business logics in controllers make them quite large and complex).
Example: In a controller function, I evaluate post dataset with a query. After that I need some business logic for each post like effectiveValueOfPost($post). Should I call it from controller and pass it to the related view, or call it from blade file.
Thank you.
I am going to start refactoring my whole Laravel application according to the latest standards today itself. Here is how an Ideal Laravel application should be structured or created.
Note: These are not the hard rules but it will be easier to maintain your Laravel applications for future updates if you structure it this way.
Controllers:
Controllers should have these methods to Create, Read, Update and Destroy a specific model. To make it simple, Let's say our model is Product:
index() - It will return the list of products.
create() - It will show a form to create a model. (If required)
store() - It will store product information coming from a create form.
show() - It will return a specific product.
edit() - It will show a form to edit any product.
update() - It will update any specific product.
destroy() - It will delete a specific product.
Routes: Routes will follow the same pattern for different models. Here are the routes that will call above given functions in the controller for a specific model, in this case, products.
/products - GET - ProductController#index
/products/create - GET - ProductController#create
/products - POST - ProductController#store
/products/{id} - GET - ProductController#show
/products/{id}/edit - GET - ProductController#edit
/products/{id} - PATCH - ProductController#update
/products/{id} - DELETE - ProductController#destroy
Models: Now, all everything that deals with the database (Queries, complex queries, relations) will be stored in your Model (app/Product.php, in this case). There are no specified functions that you can use in Model. So, you can put all your queries and processing data from queries will be stored as functions in Model.
Views: The most basic model will require create, edit, index and show views. Inside views directory, you can create different directories for different models. In this case, there will be a directory called products inside views directory and it would contain all the above-given views as well as extra views that it might need.
I have worked on some really complex applications in Laravel. And this is the structure I follow to avoid any kind of confusion while creating or updating the application code. I don't even have to remember a random name that I gave to some view because everything is properly structured and totally based on the name of the controller.
Some controllers don't require all those routes and functions, in that case, you can still follow this method without being confused.

Loading a model inside a module from API in codeigniter

I have one module Foo in my codeigniter HMVC. Also I have an api controller inside my application/controllers. I want to load a model inside application/module/foo/models from application/controllers/testapi
I have tested it by autoload as
$autoload['model'] = array('foo/Foo_model');
and called from testapi
$this->load->model('Foo_model');
But its not working
Try
$this->load->model('foo/foo_model');
from your controller.
then call model's function like this
$this->foo_model->function_name();
save model in application/modules/foo/models/Foo_model.php
CodeIgniter loads all possible models from application/models, I would suggest you modify the path of your models to something like application/models/modules with this you can load your model using
$this->load->model('modules/foo/Foo_model')
If you do want to retain your folder structure, I suggest you look at the constant paths of CodeIgniter and start modifying them to your will (although this is not really recommended).

is it better to have different controller and different model in MVC

I'm new at the MVC model. Im still learning on how to use codeigniter framework.
So, i have some questions, is it better to have different controller and different model to perform some functions or is it better to combine all into one controller and one model?
Thanks.
It is all up to you. But consideration seems like having particular controller for one object like User or Article etc. Depending on controller you would like to have appropriate model for manipulating consisting data User_model.php or Article_m.php.
If you have some generic methods and code that you want to pull out from DB from many controllers, you might have something like Generic_m.php.
If you have some other functions related to some area you can make your own library and use those.
For simple functions that don'e belong to any specific set you can create your own helpers.

Use CodeIgniter auto-loaded model in view

In the header of my site I have to add some meta-tags that are stored in the database. I have created a Metatag model and made it auto-loadable in autoload.php.
This model will provide all the meta-tags and they will be inserted in the header of all views, of any route. Can the model's meta-tags array (retrieved from the table) be somehow directly and globally used in the view or do I have to create it in every controller then pass it to the view through $data?
I have quite a few controllers and I would like to avoid requesting the meta-tags in each of them, specially when the model is auto-loaded globally.
I do not think there was any other way than to go through the controller . If that were the case, the MVC pattern would no longer respect what goes against the rule CodeIgniter .

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