Till now we have covered :
Laravel fundamentals that consists of
Routes:
Controller:
View:
Little bit of blade templating engine
Routes:
In route we route the link example:
|
Route::get('/',function(){ return "Hello To Root"; }); Route::get('/contact',function(){ return "Welcome to Contact Page"; }); //If we need to do with id variable then from get we get from below routes Route::get('/contact/{id}/{pass}',function($id,$pass){ //it's taking input from user through browser addressbar return "ID: ".$id."Pass: ".$pass; }); |
Some route example including where method:
|
Route::get('user/{name}/{id}',function($name,$id){ return " Vaisab: "."Amar Naam: ".$name." and ID:".$id." :D"; })->where(['id'=>'[0-9]+','name'=>'[A-Z]+']); |
naming routes from edwin’s video:
|
Route::get('admin/posts/example',array('as'=>'admin.zone',function(){ $url=route('admin.zone'); return "this url is:".$url; })); |
Laravel Routing:
https://laravel.com/docs/5.4/routing
Controller:
To create controller with predefined functions we need php artisan command
|
php artisan make:controller --resource ZakiLive |
This command will create automatic controller file name ZakiLive.php in
folder with predefined function.
Link Controller With Routes
So, we can set in routes/web.php file and link with controller by below code:
here index is a function/method that is exist in with ZakiLive.php controller file
Passing data to controller from route:
in route/web.php file
Now in ZakiLive.php controller file in index method/function we are getting the id, name from the url route so the code of this function below:
|
public function index($id,$name) { echo "Hello! ID: ".$id." name: ".$name." "; return "from Zaki"; } |
So when we are typing:
http://cms.dev/post/99/jj
It is giving the output:
Hello! ID: 99name: jjfrom Zaki
Resources and Controllers:
By typing this in web.php route file:
|
Route::resource('post','ZakiLive'); |
Check we didn’t linked to any specific methods in ZakiLive controller file..This is the magic of resource function in laravel and then typing PHP artisan command from terminal we get:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
C:\laragons\www\cms>php artisan route:list +--------+-----------+------------------+--------------+---------------------------------------+------------ --+ | Domain | Method | URI | Name | Action | Middleware | +--------+-----------+------------------+--------------+---------------------------------------+------------ --+ | | GET|HEAD | api/user | | Closure | api,auth:ap i | | | GET|HEAD | post | post.index | cms\Http\Controllers\ZakiLive@index | web | | | POST | post | post.store | cms\Http\Controllers\ZakiLive@store | web | | | GET|HEAD | post/create | post.create | cms\Http\Controllers\ZakiLive@create | web | | | GET|HEAD | post/{id}/{name} | | cms\Http\Controllers\ZakiLive@index | web | | | GET|HEAD | post/{post} | post.show | cms\Http\Controllers\ZakiLive@show | web | | | PUT|PATCH | post/{post} | post.update | cms\Http\Controllers\ZakiLive@update | web | | | DELETE | post/{post} | post.destroy | cms\Http\Controllers\ZakiLive@destroy | web | | | GET|HEAD | post/{post}/edit | post.edit | cms\Http\Controllers\ZakiLive@edit | web | +--------+-----------+------------------+--------------+---------------------------------------+------------ --+ |
So, from the above list if we type http://cms.dev/post/create then it will return from create method available in ZakiLive.php controller file
so here it is I defined:
|
public function create() { return "This is Create Method"; } |
Output:
This is Create Method
Controller reference in official laravel docs:
https://laravel.com/docs/5.2/controllers#introduction
Views:
Views available in laravel 5.4 in resources/views/dekho.blade.php
Now need to route the url to controller ZakiLive.php…so in this file the below code:
|
public function Dekhao() { return view('dekho'); //here dekho is the view file in the directory in blade.php format } |
Now link the route/web.php file with controller file
code:
Passing Data to View:
We can pass data from url to view through controller by using below code:
In Controller ZakiLive.php we need to write the below code:
|
public function ShowID($id,$name,$pass){ //return view('dekhoid')->with('id',$id); //it's good for only one id sending for more than one we can use comapct function return view('dekhoid',compact('id','name','pass')); } |
in route we need to pass the url with id :
web.php
In views folder now we can create a blade formatted php file to display the passed data from url
|
<html> <head> <title>Dekho ID</title> </head> <body> ID: {{$id}} Name: {{$name}} Password: {{$password}} </body> </html> |
View Data Without Controller(Passed data only from Route to View):
|
Route::get('/jhinga',function(){ return view('hello/greetings',['name'=>'james']); }); |
in views folder:
view/hello/greetings.bade.php file
code:
|
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>Hello, {{$name}}</h1> </body> </html> |
Laravel View Reference:
https://laravel.com/docs/5.4/views#creating-views
Master Layout with Blade Templating
for layout setup i am explaining here from routing to controller to blade templating from first to last(A-Z)
In routes/web.php add this line:
and now in controller file ZakiLive.php
add this line:
|
public function bladeShow($id,$pass){ return view('bladeshow',compact('id','pass')); } |
now in views folder create a folder “bladebladedekhao” then a file “appshow.blade.php”
in this file add some codes
|
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div class="container"> @yield('content') </div> <b> @yield('dhaka') </b> @yield('footer') </body> </html> |
check attentively that here @yield(‘content’) is declaring the section in <div> </div> and @yield(‘dhaka’) is defining the inside of <b> </b> section where <body> </body> section is defining by @yield(‘footer’) section
Now views/bladeshow.blade.php file we are going to write this code:
|
@extends('bladebladedekhao.appshow') @section('content') <h1>BladeShow te Shagotom {{$id}} {{$pass}}</h1> @endsection @section('footer') @stop |
where extends is a blade syntax and it’s helping to include “appshow.blade.php” file to the bladeshow.blade.php file in views folder
@section(‘content’) is inherited part from the appshow file @endsection/@stop is closing the section gracefully 😀
{{id}} and {{pass}} variable is coming from the url by using below link:
|
http://cms.dev/bladeshow/id/009 |
Same we can do for contact page :
to do this in routes/web.php file we can do :
then in controller file ZakiLive.php:
|
public function ContactShow(){ return view('contact'); } |
Then in views/contact.blade.php file we can add this code:
|
@extends('bladebladedekhao.appshow') @section('footer') <h1>Contact Page</h1> @stop |
and after typing the url we will get the output:
url:
Output:
Some more blade features with
For contact page we will assign manual variable in controller to send it to contact page. Let’s see: How we can do it:
|
public function ContactShow(){ $people= ['Zaki','Live','Dhaka','Live']; return view('contact',compact('people')); } |
Now in Contact page:
|
@section('content') <h1>Contact Page</h1> @if(count($people)) <ul> @foreach($people as $person) <li>{{$person}}</li> @endforeach </ul> @endif @endsection |
So Output will be like this:
|
Contact Page Zaki Live Dhaka Live |
if we change in controller like this:
|
public function ContactShow(){ $people=[]; return view('contact',compact('people')); } |
It will show nothing but only contact page:
So Output will be like this:
if we change in controller like this:
|
public function ContactShow(){ $people=['hk']; return view('contact',compact('people')); } |
It will show nothing but only contact page:
So output will be like this:
Reference for laravel blade engine:
https://laravel.com/docs/5.4/blade
Migrate Database and Dropping them:
Create database manually from phpmyadmin giving the name laravel_cms then in .env file we will set the database credentials like pass,username etc.
then we will type:
|
php artisan make:migration |
then in database/migration folder we will check some predefined files name for creating our own table with migration.
then we need to type in terminal about this:
|
php artisan make:migration create_posts_vanga --create="ghost |
It will create the file below like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePostsVanga extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('ghost', function (Blueprint $table) { $table->increments('id'); $table->string('book'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('ghost'); } } |
Now to update this we can create this:
and it will change the mysql db and add “ghost” table in database that can be checkecd from phpmyadmin
now if we want to roll back/drop the database ghost that we created we need to just type this command:
|
php artisan migrate:rollback |
then aftwer checking the database with php myadmin we will see that “ghost” table is not available there 🙂
if we again type
then ghost table will be back from the database
Adding Column to Existing Database and Dropping Them:
|
php artisan migrate:status |
https://laravel.com/docs/5.2/migrations
RAW SQL Query:
For inserting into database:
|
Route::get('/insert',function(){ DB::insert('insert into posts(title, contents) values(?,?)',['PHP with laravel','Laravel is the best thing that has happened to PHP']); }); |
For Reading from the database:
|
Route::get('/read',function(){ $results=DB::select('select * from posts where id=?',[1]); foreach($results as $post){ return $post->title; } }); |
Reading Slightly Difference:
|
Route::get('/read',function(){ $results=DB::select('select * from posts where id=?',[1]); // return $results; //return var_dump($results); foreach($results as $post){ return $post->title; } }); |
Update:
|
Route::get('/update', function(){ $update=DB::update('update posts set title="Update korsi title" where id=?',[1]); }); |
Delete:
|
Route::get('/delete',function(){ $delete=DB::delete('delete from posts where id = ?',[1]); return $delete; }); |
Reference: https://laravel.com/docs/5.2/database
Database Eloquent ORM:
|
php artisan make:model Post |
Some example to see with database in route with Post ORM:
|
Route::get('/find',function(){ $posts=\cms\Post::all(); foreach ($posts as $post){ return $post->title; } }); |
another:
|
Route::get('/find',function(){ $posts=\cms\Post::find(2); return $posts->title; // foreach ($posts as $post){ // return $post->title; // } }); |
Order by in database:
|
Route::get('/findwhere',function(){ $posts=\cms\Post::where('id',2)->orderBy('id','desc')->take(1)->get(); return $posts; }); |
Insert method:
|
Route::get('/basicinsert',function(){ $post=new \cms\Post(); $post->title='neW ORM title eloquent'; $post->contents="Eloquent is really cool"; $post->save(); }); |
ORM insert by find(specific_id_in_db)
code:
|
Route::get('/ormuoinsert',function(){ $post=cms\Post::find(2); $post->title='New eloquent title insert 2'; $post->contents="New eloquent is really cool"; $post->save(); }); |
Creating Data and Configuring mass Assignment:
Post:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
<?php namespace cms; use Illuminate\Database\Eloquent\Model; class Post extends Model { // protected $table='posts'; // protected $primaryKey='id'; // protected $fillable=[ 'title', 'contents' ]; } |
In routes/web.php
|
Route::get('/create',function(){ cms\Post::create(['title'=>'the crete method','contents'=>'WOW I am learning a lot with it']); }); |
Update ORM:
|
Route::get('/updateorm',function(){ cms\Post::where('id',2)->update(['title'=>'NEW PHP TITLE','contents'=>'hello']); }); |
Delete with ORM:
|
Route::get('/deleteorm',function(){ $post=cms\Post::find(2); $post->delete(); }); Route::get('/delete2',function(){ cms\Post::destroy(3); }); Route::get('/deletemulti',function(){ cms\Post::destroy([4,5]); //cms\Post::where('id',0)->delete(); }); |
Softdelete :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
Route::get('/readsoftdelete',function(){ // $post=\cms\Post::find(1); // return $post; $post=cms\Post::onlyTrashed()->where('id',6)->get(); return $post; }); Route::get('/readsoftdelete1',function(){ // $post=\cms\Post::find(1); // return $post; $post=cms\Post::withTrashed()->where('id',6)->get(); return $post; }); |
Force delete:
|
Route::get('/forcedelete',function(){ cms\Post::withTrashed()->where('id',6)->forcedelete(); }); |
Eloquent Relationship:
One To One:
Create a table Ghost:
|
php artisan make:migration create ceate="ghost" |
then
then go to phpmyadmin and add some data to the table “ghost”
then create a model with the help of ORM
|
php artisan make:model Amar |
in Amar model file:
|
<?php namespace cms; use Illuminate\Database\Eloquent\Model; class Amar extends Model { public function Posta(){ return $this->hasOne('App\Amar'); } } |
Our main task is in:
routes folder where we can create naming routes for a page which plays role to redirect a page from web browser to view for the user using help of the PostsController.
Make a recipe for my working project:
In routes/web.php (aka routes.php) we need to add a line
|
Route::get('ghosts/{voot}/{voottype}/{vooterbasha}','VootController'); |