November 11, 2015 · Laravel

Laravel 5+ deep structure explanation

File Structure
FileName Description
.env Environment Settings, including database hosting, password, application key, mail server setup, smtp and others
app/Http/routes.php Router class.
app/*Model Model classes are Generated here.
app/Http/Controller/*Controller Controller classes are generated here.
resources/views/*.blade.php View files are placed here.
database/migrations/*table.php Laravel setup database by setting up the object mapping, the relationship, schema and the table settings are place here.
resources/seeds/DatabaseSeeder.php Database Seeder, called by php artisan db:seed
resources/lang/{countrycode}/* language file
config/app.php Timezone / url / locale


Routing
Route::get (‘/‘ ,’[email protected]’);
Route::post(‘home’,’[email protected]’);
Route::get ('articles/{id}' , '[email protected]’);
Route::get (‘api’ ,function(){
 return ‘bar’;
)
}
Controllers
//Suggested use artisan to create the Model First, case sensitive, suggested first Character is Supper character.
php artisan make:controller PagesController —plain
Route::controllers ([
‘auth’=>’Controller'
]);
Http/Controllers/
public function index(){
 return view(‘welcome’)->with([
  ‘name’=> $var,
  ‘sec   => $var2
 ]) ;
}
public function contact(){
 //   /resources/view/forum/name.blade.php
 return view(‘forum.name’); 
}
public function getArticles(){
 $articles = Article:all();
 return $articles ; //json
 return view (‘article.index’ ,$articles);
}
public function show($id){
 $article = Article:find ($id);
 dd ($article); //var_dump ($article);die();
 if (is_null($article)){about(404)};

 $article = Article:findOrFail($id); //throw exception 

 return view (‘article.index’ , $article);
}
View -Using Blade Engine
// The variables must be passed by controllers / pre-loaded php code
// The {{}} is for printing variables
// {{ !! $name !! }} not escaped
// {{ $sec }} escaped

//File:resources/views/block/master.blade.php
@yield(‘content’)   //*optional

//File:resources/views/child.blade.php
@extends(‘block.master’)
@section(‘content’) //*optional
 @if ($firstname ==‘John’)
  

Hi John

@else

Hi You are not John

@endif @if (count($collection)>0)
    @foreach ($collection as $piece)
  • {{ $piece->title }}
  • @endforeach
@endif @stop {{!! !!}}
Database Migration
php artisan make:migration create_articles_table //suggested use make:model Article that also generate the associated migration
php artisan migrate
php artisan migrate:rollback


// File:database/migrations
public function up(){
 //creating new table
 //creating schema 

 Schema::create(‘articles’, function(Blueprint $table){
 $table->increments(‘id’); //int
 $table->string(‘title’); //varchar 255
 $table->text(‘body’); //
 $table->timestamps(); //created_at , updated_at
 $table->timestamp(‘published_at’);
});
}

public function down(){
 Schema::drop(‘articles’);
}
Model
//php artisan make:model Article
//File:app\Article
class Article extends Model (
 protected $fillable = [
  ‘title’ , ‘body’ , 'published_at'
 ]; //better secured application
 protected $table = ‘Articles’;
);

App\Article::all()->toArray();
$article->App\Article::find(1);
$article->save();
php artisan tinker
//Example Use
$article = new App\Article;
$article->title = ‘Article’;
$article->body = ‘body’;
$article->published_at = Carbon\Carbon::now();
echo ($article->toArray);
$article->save();
App\Article::all()->toArray();
$article->toArray();
$article->title = ‘My updated first article’;
$article->save();
$article = App\Article::find(1);

$article = App\Article::where(‘body’, ‘equal’)->first();
$article->body;
$article = new App\Article;
$article = App\Article::create(
[‘title’ => ‘New Article’,
‘body => ‘body’,
‘published_at = Carbon\Carbon::now()
]
);
Other useful libraries and references:
https://github.com/cviebrock/eloquent-sluggable
http://laravelcollective.com/docs/5.0/html