Make changes to a site developed in Laravel 4.2 [closed]

1

I'm starting the development in Laravel and I have to implement new features in a site developed in Laravel 4.2 and by another programmer.

I need to create a function that adds rows to a Log style table, which needs to be accessible throughout the project.

public function logEmail( $email ){}

Do I create a model with this function? How to include the model so that this function is accessible anywhere?

    
asked by anonymous 13.10.2015 / 11:28

1 answer

1

You will create a model (Eloquent) with all the pertinent settings of the table that you reference 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 created_at and updated_at fields are not entered because they are automatically generated).

This would be the basic operation, where in any Local you could call this line to create a Log. In general you can make a line in BaseController Log in all 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 on the site itself has all the Eloquent settings and how it should be used.

    
13.10.2015 / 16:44