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'));
    }
}

Laravel Snippets

Incorrect Paths to assets - css and js
--------------------------------------
The issue is on localhost. I did not created custom virtual host.
However using {{asset('path')}} location seems to work everywhere.

Laravel 5.3 - Clear config cache in shared hosting
---------------------------------------------------
config:clear command just deletes bootstrap/cache/config.php file, 
so just delete this file manually.

Laravel Documentation

Installing Laravel

Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.

php composer.phar create-project --prefer-dist laravel/laravel portfolio "5.5.*"

Start Local Development Server:

cd DirectoryName
php artisan serve

This command will start a development server at http://localhost:8000:

Artisan

artisan is a command line interface that means artisan have a lots of command.

Artisan list

cd DirectoryName
php artisan

blade.php

blade is a laravel default template engine. You can use special syntax.

Create Controller and Routes

Command in your terminal:

cd DirectoryName
php artisan make:controller ControllerName

index, create, store, show, update, edit, delete

cd DirectoryName
php artisan make:controller SongsController --resource

Routing list

routes > web.php

Route::get('about','HomeController@about');
Route::resource('songs','SongsController');

cd DirectoryName
php artisan route:list

Create db table

cd DirectoryName
php artisan make:migration create_galleries_table --create=galleries
php artisan migrate

Tinker

cd DirectoryName
php artisan tinker
DB::table('songs')->insert(['title'=>'Bangladesh','artist'=>'James','created_at'=>new DateTime,'updated_at'=>new DateTime]);
DB::table('songs')->get();
DB::table('songs')->where('id',2)->get();
come out from the tinker just press ctrl+c

Model

The most important thing is your model name must be singular

cd DirectoryName
php artisan make:model song

If you want to create model and migration together

cd DirectoryName
php artisan make:model todo -m

Database

The database will have to change in 2 directory
root directory > .env
config > database.php
after that restart your server

helper and plugins installation

1.go laravelcollective.com then version select
2.You can see installation code
3.open composer.json file and add following code
“require”:{
“laravelcollective/html:”~5.2.0”
}
4.then you have to update the composer
5.so go to getcomposer.org/download/
after composer installation then update composer.phar file.
command in your terminal:
#php composer.phar update
6.Next you have to add provider and aliases just follow the laravelcollective
documentation