Ir al contenido principal

Entradas

Mostrando entradas de agosto, 2016

PHP / Laravel X (Containers)

Containers The containers will help to store objects that will be used in many parts Create a folder named Repositories in app/ Inside the folder create your class as <Name>Repository.php Create a method to return your values public getValues(){     return $values; } Controller Create your controller and receive as parameter your Container class If you will invoke many times the method you can call it in the constructor public function _construct( <Name>Repository $repository){      return $repository->getValues();     }

Mongoose II - Inner schema

Inner Schema Create a schema named dishSchema with an inner commentSchema // grab the things we need var mongoose = require('mongoose'); var Schema = mongoose.Schema; var commentSchema = new Schema({     rating:  {         type: Number,         min: 1,         max: 5,         required: true     },     comment:  {         type: String,         required: true     },     author:  {         type: String,         required: true     } }, {     timestamps: true }); // create a schema var dishSchema = new Schema({     name: {         type: String,         required: true,         unique: true     },   ...

Mongoose I

Mongoose Mongoose is an ODM  Object Data Model Generate a schema to order the documents in MongoDB Install To install mongoose in your project, type: npm install mongoose --save Create a Model All this code will be in a file <Model>.js to create a Model and be used as node package // grab the things we need var mongoose = require('mongoose'); var Schema = mongoose.Schema; // create a schema var dishSchema = new Schema({     name: {         type: String,         required: true,         unique: true     },     description: {         type: String,         required: true     } },  //This part creates two date fields to store when a field is created and when is updated  {     timestamps: true }); // the schema is useless so far // we need to create a mo...

MongoDB II

Driver Mongos is a ODM way to interact with MongoDB Assert package Package to assert conditions Install npm install --assert --save Install MongoDB in your project npm install --mongodb --save Execute MonogDB In a console change the path to the folder mongodb Type: mongod --dbpath=data In another console type: mongo Type: use <DB> In case you want to raise your project go to the folder in another tap and type: node <file>.js Insert a register in a project  var MongoClient = require('mongodb').MongoClient, assert = require('assert'); // Connection URL var url = 'mongodb://localhost:27017/<DB>'; // Use connect method to connect to the Server MongoClient.connect(url, function (err, db) {     assert.equal(err,null);     console.log("Connected correctly to server");         var collection = db.collection("dishes");         collection.insertOne({name: ...

PHP / Laravel IX (Seeder)

Seeder Fills the DB automatically with the registered previously loaded. Create a Seeder Use the format  <Table_Name>TableSeeder as standart php artisan make : seeder UsersTableSeeder The new class will appear in  database / seeders Query Builder In the method Run, add the registeres to be inserted DB :: table ( 'users' ) - > insert ( [ 'name' = > str_random ( 10 ) , 'email' = > str_random ( 10 ) . '@gmail.com' , 'password' = > bcrypt ( 'secret' ) , ] ) ; Run Seeder Only execute the DatabaseSeeder php artisan db : seed You can add your seeder as: // $this->call(<yourclass>::class); php artisan db : seed -- class = UserTableSeeder php artisan migrate : refresh -- seed