public function contact() {
return 'Under Construction';
}
Returns HTMLpublic function about() {
return '<h1>About the site</h1>details...';
}
Returns view located at resources/views/pages/services.blade.php
public function services() {
return view('pages/services');
}
ctrl + p
-> search ext install laravel blade
-> install laravel blade snippets
extensionwith
methodpublic function about() {
$param1 = 'this is a parameter of about us page';
return view('pages/about')->with('title', $param1);
}
Content of the blade template@extends('layouts.app')
@section('cntnt')
<h3>abt us {{$title}}</h3>
@endsection
Posts
php artisan make:controller PostsController
With the following basic content<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class Postscontroller extends Controller
{
//
}
Creates controller and empty methods in it (with appropriate comments)php artisan make:controller PostsController --resource
The methods are:
index()
- Displays a list of the resource.
create()
- Shows the form for creating a new resource
store(Request $request)
- Stores a newly created resource in storage
show($id)
- Displays the specified resource
edit($id)
- Shows the form for editing the specified resource
update(Request $request, $id)
- Updates the specified resource in storage
destroy($id)
- Removes the specified resource from storagePost
php artisan make:model Post
With the following basic content<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
}
Creates Post
model and also migrations to create table for the modelphp artisan make:model Post -m
The migration file location isdatabase/migrations/2020_09_02_041219_create_posts_table.php
.env
file contains database config information that needs to be changed
to use the databaseDB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=lara4db
DB_USERNAME=root
DB_PASSWORD=
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->mediumText('content');
$table->timestamps();
});
$table->id()
- creates bigint(20)
unsigned auto-increment field
$table->string('title')
- creates varchar(255)
utf8mb4_unicode_ci field
$table->mediumText('content')
- creates mediumtext
utf8mb4_unicode_ci field
$table->timestamps()
- creates created_at
and updated_at
timestamps php artisan route:list
Route::resource('posts', 'PostsController');
These routes are:
GET
at posts
to list all the posts
POST
at posts
to store new post
GET
at posts/create
to show form for creating new post
GET
at posts/{post}
to show the post
PUT
at posts/{post}
to update the post
DELETE
at posts/{post}
to delete the post
GET
at posts/{post}/edit
to show edit formcreated_at
with descending
order, to see the newly created posts at the top