Many to Many Relationship

Suppose one user has many roles.
ankur = admin,editor
salman = editor,author
You need to create 3 tables in your Database. users, roles, and role_user. After creating your table, you need to data insert

cd Directory
php artisan tinker
DB::table('users')->insert(['name'=>'Ankur','created_at'=>new DateTime,'updated_at'=>new DateTime])
DB::table('users')->insert(['name'=>'Salman','created_at'=>new DateTime,'updated_at'=>new DateTime])
DB::table('roles')->insert(['role'=>'admin','created_at'=>new DateTime,'updated_at'=>new DateTime])
DB::table('roles')->insert(['role'=>'editor','created_at'=>new DateTime,'updated_at'=>new DateTime])
DB::table('roles')->insert(['role'=>'author','created_at'=>new DateTime,'updated_at'=>new DateTime])
DB::table('role_user')->insert(['role_id'=>'1','user_id'=>'1','created_at'=>new DateTime,'updated_at'=>new DateTime])
DB::table('role_user')->insert(['role_id'=>'2','user_id'=>'1','created_at'=>new DateTime,'updated_at'=>new DateTime])
DB::table('role_user')->insert(['role_id'=>'2','user_id'=>'2','created_at'=>new DateTime,'updated_at'=>new DateTime])
DB::table('role_user')->insert(['role_id'=>'3','user_id'=>'2','created_at'=>new DateTime,'updated_at'=>new DateTime])

Now you have to create model

cd Directory
php artisan make:model user
php artisan make:model role

user model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class user extends Model
{
    public function roles(){
    	return $this->belongsToMany(Role::class);
    }   
}

role model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class role extends Model
{
    public function users()
    {
        return $this->belongsToMany(User:class);
    }
}

Retrieve the related record:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\user;

class HomeController extends Controller
{
    public function index(){
    	$roles = user::find(2)->roles;    	
    	//$roles = user::find(2)->roles()->orderBy('role')->get();    	
    	return view('welcome',compact('roles'));
    }
}
Advertisements

One to One Relationship

Suppose You have a users table and passports table. Each user has only one passport number. On this case, you can one to one relationships

cd Directory
php artisan tinker
DB::table('users')->insert(['name'=>Amir,'created_at'=>new DateTime,'updated_at'=>new DateTime])
DB::table('passports')->insert(['number'=>'998787545782','user_id'=>'1','created_at'=>new DateTime,'updated_at'=>new DateTime]);

Now you have to create model for user and passport

cd Directory
php artisan make:model user
php artisan make:model passport

user model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class user extends Model
{
    public function passport(){
    	//return $this->hasOne('App\passport');
    	return $this->hasOne(Passport::class);
    }   
}

passport model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class mobile extends Model
{
    public function user(){
    	return $this->belongsTo(User::class);
    }
}

Retrieve the related record:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\user;

class HomeController extends Controller
{
    public function index(){
    	$passport_data = user::find(1)->passport;    	
    	return view('welcome',compact('passport_data'));
    }
}

Recursive Function

A recursive function is a function that calls itself. If the function keeps calling itself, how does it know when to stop? You set up a condition.

<?php
class Recursion{
	function recursive($num){
		echo $num,"<br/>";
		if(isset($num) && $num < 10){
			return $this->recursive($num + 1);
		}
	}
}
$obj = new Recursion;
$obj->recursive(1);

Sublime 3


Multiple Same Word Selection:
-----------------------------
select and then ctr+d
 or 
ctr + mouse click 


Code Formating:
---------------
	Menually: Edit > Line > Reindent

	For key set:
		Preferences > key bindings
			[
				{
					"keys" : ["ctrl+shift+r"],
					"command" : "reindent"
				}
			]
		



Sublime Package:
----------------
ctr+shift+p
Install Package
	1. Emmet package
	2. SideBarEnhancements
	3. All AutoComplete
	4. Syntax Highlighting for Sass
	5. CodeFormatter

	
Amazing Emmet for Sublime. Just do it and hit the tab
.container>.row>table.table>(thead>th*3)+(tbody>tr*3>td*3)


Laravel Package:
----------------
ctr+shift+p
Install Package
	1. Blade Snippets
	2. Laravel Blade Highlighter


	
Wordpress Package:
----------------
ctr+shift+p
Install Package
	1. WordPress
	2. WooCommerce Autocomplete
	
	

JQuery Package:
---------------
ctr+shift+p
Install Package	
	1. JQuery


Traits in PHP

Basically multiple inheritance does not supported by PHP. One class can only extend or inherit from one other class. But there are many cases when you want to extend more than one classes for code reusability. To overcome this problem, as of version 5.4, PHP introduced a new feature of code reuse named trait.

So what are Traits?. Traits are a mechanism for code reuse. A Trait is simply a group of methods that you want include within another class. A Trait, like an abstract class, but It is not allowed to instantiate a trait on its own.

/* mobile.php */

<?php

class Mobile{

	function model(){

		echo "Samsung Galexy 5 <br/>";
	}

}


/* charger.php */

<?php

trait Charger{

	function battery(){

		echo "2500 MP Battery <br/>";
	}

	function power(){

		echo "Power of Battery </br>";
	}


}


/* projector.php */

<?php

trait Projector{

	function range(){

		echo "Projector range 5 meter <br>";
	}

	function power(){

		echo "Power of range <br/>";
	}

	
}



/* body.php */

<?php

trait Body{

	function display(){

		echo "Display 5.5 inc <br/>";
	}

	public $name = "Smart Phone";

	abstract function tabu();
}


/* glass.php */

<?php

include_once('body.php');

trait Glass{

	use Body;

	/*function display(){

		echo "Cornic Gorila Glass <br>";
	}*/
}


/* index.php */

<?php

include_once('mobile.php');
include_once('charger.php');
include_once('projector.php');
include_once('glass.php');


class Samsung extends Mobile{

	use Charger,Projector,Glass{

		Charger::power insteadof Projector;
		Projector::power as Ppower;
	}

	/*function display(){

		echo "This is from index";
	}*/

	function tabu(){

	}
}



$obj = new Samsung;
$obj->model();
$obj->battery();
$obj->range();
$obj->power();
$obj->Ppower();
$obj->display();
echo $obj->name;


Namespace

Normally PHP does not allowed to use same multiple class name or method. So what you do on that situation. You can use Namespace. Namespace just like a virtual directory. You can use same multiple class name or method without a name collision.


/* first.php */
<?php

class A{

	function __construct(){

		echo "I am at global space </br>";
	}
}

/* second.php */
<?php

namespace second;

class A{

	function __construct(){

		echo "I am at second space <br/>";
	}
}


/* index.php */
<?php

include "first.php";
include "second.php";

use second\A;

//$obj = new second\A;	// qualified class name

$obj = new A;		// unqualified class name

$obj = new \A;		// fully qualified class name




Late Static Bindings

The self keyword does not follow the same rules of inheritance. self always resolves to the class in which it is used. This means that if you make a method in a parent class and call it from a child class, self will not reference the child as you might expect.


<?php

class Php{

	public static function framework(){

		echo static::getClass()."<br/>";

	}

	public static function getClass(){

		return __CLASS__;
	}
}

class ChildPhp extends Php{

	public static function getClass(){

		return __CLASS__;
	}
}


$php = new php;
$php->framework();


$childphp = new ChildPhp;
$childphp->framework();

Constant

A constant is declared with the const keyword followed by a name (is not prefixed with a dollar sign like properties).
Syntax: const CONSTANT_NAME
The main difference between properties and constants is that the constant value cannot be changed once it is set.


<?php

/**
 * Area	= π r2
 */

// Procedural way constant define
// define("CONSTANT_NAME", 'value');

class Circle {

	const PI = 3.1415;

	public function area($radius){

		return $radius * $radius * self::PI;
	}
	
}

$circle = new Circle;
echo $circle->area(5);

Static in PHP

Static properties and methods in php can directly accessible without creating object of class. You can declare static property and method using static keyword.

<?php

class User{

	public static $name;

	static function getName(){

		self::$name = "Ataur Rahman";
	}
}

User::getName();
echo User::$name;

PHP Inheritance

It allows you to create a new class that reuses the properties and methods from an existing class. A class that inherits from another class is called subclass (also a child class).


<?php

class Fruits{

	public $name;

	public function setName($name){
		$this->name = $name;
	}
}

class Apple extends Fruits{

	function juice(){
		echo "Juice <br/>";
	}
}


class Mango extends Fruits{

	
}

$apple = new Apple();
$apple->juice();
$apple->setName("My Apple");
echo $apple->name;

echo "<br/>";

$mango = new Mango();
$mango->setName("My Mango");
echo $mango->name;