Make changes to a website developed in Laravel 4.2

Asked

Viewed 94 times

1

I am starting development in Laravel and I have to implement new features on a site developed in Laravel 4.2 and by another programmer.

I need to create a function that adds lines to a Log-style table that needs to be accessible throughout the project.

public function logEmail( $email ){}

Create a model with this function? How to include the model in order to have this function accessible anywhere?

  • Please explain the problem further, and if possible include a example of code that reproduces what is happening, because your question is not perceptible. See Help Center How to Ask.

  • The question, at this moment, is not related to the code itself, but in relation to the functioning of MVC, in this case in Laravel 4. I will have public Function logEmail( $email ){ which will be called in several places of the project. The question is: Do I create a new model? How do I "call" the model in order to access the function?

  • Rui Costa you can include those doubts you have in the question so that it is more specific. Just click on [Dit].

1 answer

1


You will create a model (Eloquent) with all settings relevant to the table that refers to your model.

Example

<?php
    class Log extends Eloquent 
    {
        public $table      = 'logs'; 
        public $primaryKey = 'id';
        public $timestamps = true;
        protected $fillable = array('description');

        public function getDates()
        {
            return array('created_at','updated_at');
        }
    }

Having a table similar to this layout referencing the Eloquent model above.

CREATE  TABLE `test`.`logs` 
(    
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,    
  `description` VARCHAR(255) NOT NULL ,    
  `created_at` TIMESTAMP NULL ,    
  `updated_at` TIMESTAMP NULL ,    
  PRIMARY KEY (`id`) 
);

Function:

$log = Log::create(array('description' => 'info'));

This line above inserts a record in the table of logs (the fields are not informed created_at and updated_at, because they are automatically generated).

That would be the basic functioning, where in any Local you could call this line to create a Log. In general you can make a line in the BaseController being the same responsible in always recording a Log in all the classes that inherited from this BaseController.

Example:

class BaseController extends Controller {


    public function __construct()
    {

    }

    protected function setupLayout()
    {
        if ( ! is_null($this->layout))
        {
            $this->layout = View::make($this->layout);
        }
    }

    //método padrão de LogEmail
    protected function logEmail($email)
    {
        return Log::create(array('description' => $email));
    }

}

No seu controller

<?php
    class BuscaController extends BaseController 
    {
        public function __construct()
        {
            parent::__construct();          
        }
        public function index()
        {

            $this->logEmail('[email protected]');

            return Redirect::route('index.home');

        }
    }

This link of the site itself has all the settings of Eloquent and how it should be used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.