Ir al contenido principal

Entradas

Mostrando entradas de julio, 2016

PHP / Laravel VIII (Eloquent)

Relationship between tables Create the father table php artisan mak:mig create_migration_card_table --create=cards Create the child table php artisan mak:mig create_migration_card_table --create=notes Relationship Create the foreign key in the child table $table->integer('card_id')->unsigned()->index();  Create the Model php artisan make:model Note php artisan make:model Card Shortcut In this way we can create the model and the migration at the same time php artisan make:model <Class> -m Refresh DB php artisan migrate:refresh  Using Eloquent In the father class (Model) add a method to get the childs public function  notes (){                  return $this-> hasMany ( <Class>::class );              } In the child class (Model) add the next method to get the father  public function card(){ ...

PHP / Laravel VII (Eloquent)

Eloquent Once you have created your model in /App you can use it in the Controller to start doing the queries. Insetead of  $notes = DB::table('note')->get(); You can replace it for: $ notes  = Note::all();       Then you can sent the objects to the view return view('<folder> . <file_name>', compact(' notes ')); Commands <Class>::find($id); TIP You can create a direct search using the following, laravel will search by default by ID Router Define a router to receive an object:   Route::get('show/{notes}', 'NoteController@show'); Controller In this case we are waiting an object of type Note, automatically will find by ID in the database public function showObj(Note $notes){                       return view('notes.show', compact('notes'));     } View In the view we receive the object n...

PHP / Laravel III (Model)

Help php artisan help Creating classes (Model) Using artisan php artisan make:model <class_name> The class will be created in app Add the namespace use App\<class> Standart By default once you create the Model in SINGULAR, laravel will consider that you table will be in PLURAL In case you want to maintain the singular add to you model: protected $table = '<class name>'; Fillable In the class created add  protected $fillable = ['note']; DB Configuration In the file .env Type the user and password Create the database in mysql Migration Create the tables in the DB Type in artisan: php artisan: php artisan make:migration <migration_name> --create=<table_name> or php artisan mak:mig <migration_name> --create=<table_name> The migration will appears on the folder database/migrations Migration commands $table->string('<field_name>'); $table-...

PHP / Laravel VI (Layout)

Layouts In resources/views you can create your templates Named the file as: layout.blade.php @yield('<section>')  will be the section that will be replaced later by your view Implement @extends('<template>') To specify the template to use @section('<section_name>') To indication which section will be replaced @stop To indicate the end of the section Example: <!DOCTYPE html> <html>     <head>         <title>Notes</title>         <meta charset="UTF-8">                  @yield('header')     </head>     <body>                  @yield('content')                           @yield('footer')        ...

PHP / Laravel V (Controller / Routes)

Controller Artisan also create the controllers php artisan make:controller <Controller name>Controller The new controller will appers in app/Http/Contoller folder Add the namescape DB:   use DB; Functionallity Create the fuction index public function index() { return view ('<view_name>.index'); } <view_name>.index means That there is a view in /resources/views/<view_name>/index.blade.php Passing parameters return view ('<view_name>.index', compact('<array>') ); Other form is  return view('<view_name>.index')->with('<array>', $<array>) Routes In the file app/Http/routes indicate the controller to be used Route::get('notes', 'NoteController@<function>'); 'notes' is the name of the URL 'NoteController is the name of the file controller @<method> is the function to be called In my...

MongoDB I

NoSQL DB Categories Document DB (MongoDB) Key-value DB (Redis, *Speed) Column-family DB (Cassandra) Graph DB (Neo4J) Document DB Contains information in any format as JSON Document - {"name":"user", "desc": "employee"} Collection - Set of documents Benefits Availability Consistency Partion tolerance Ease of deployment No object-relation mapping required Format MongoDB store the information in BSON (Binary JSON format) Contains about the type of a field value Primitive types are supported as date time, raw binary, ObjectId ObjectId  By default MongoDB assign an id if you don't declare one {     "_id" : ObjectId("234234234"),     "name" : "pizza" ,      "desc" : "pepperoni" } 4- digits : timestamp 3 - machine  2 - proc Id 3 - increment id.getTimestamp()  -  returns the timest in ISO formatpe:  Co...

NodeJS V - Express Generator

Express Generator Scanffold out an Express application Install  npm install express-generator -g Use sudo in mac Create the application express <project_name> Install node npm install Start npm start The class app.js is the initial file

Node III - HTTP

Node HTTP Module Using the module: var http = require('http');  Creating a server: var server = http.createServer(function(req, res){ . . . });  Starting the server: server.listen(port, . . . ); Request Incoming request message information available through the first parameter “req”   req.headers, req.body, . . .  Response Response message is constructed on the second parameter “res”  res.setHeader("Content-Type", "text/html");   res.statusCode = 200;   res.writeHead(200, { 'Content-Type': 'text/html' });  res.write(’Hello World!');   res.end(' <html><body><h1>Hello World</h1></body></html> '); Example var http = require('http'); var hostname = 'localhost'; var port = 3000; var server = http.createServer(function(req, res){ console.log(req.headers); res.writeHead(200, {'Content-Type': 'text/html'}); res.end('...

NodeJS II

Callbacks First parameter is always an error Code using Callbacks module.exports = function(x,y,callback) {   try {     if (x < 0 || y < 0) {         throw new Error("Rectangle dimensions should be greater than zero: l = "                             + x + ", and b = " + y);     }     else          callback(null, {             perimeter: function () {           return (2*(x+y)); },             area:function () {            return (x*y); }     });   }   catch (error) {         callback(error,null);   } } Execute the function using Callbacks var rect = require('./rectangle-2'); function solveRect(l,b) {     console....

NodeJS I

1. Install        Download from  https://nodejs.org/en/ 2.Verify          Open the CMD and type: node -v   or type   npm -v         If in the screen appear the node version, the installation has been done with success First Example var rect = { perimeter: function (x, y) {            return (2*(x+y)); }, area: function (x, y) {             return (x*y); } }; function solveRect(l,b) {     console.log("Solving for rectangle with l = " + l + " and b = " + b);     if (l < 0 || b < 0) {         console.log("Rectangle dimensions should be greater than zero:  l = "                + l + ",  and b = " + b);     }     else { console.log("The area of a rectangle of dimensions length = " ...