route
||
controller <====> model
||
view
php, mysql, composer need to be installed in your mac, pc or linux system before creating project in laravel
composer create-project laravel/laravel YourProjectName
app/Http/Controllers/Yourcontroller.php
app/yourmodel.php
resoures/views/Yourview.php
database/factories/yourfactory.php
database/migrations/yourmigrationfile.php
routes/web.php
database/seeds/databaseSeeder.php
factories and seeds only require when we work with dummy/test data
php artisan make:model YourModel
php artisan make:controller YourController
php artisan make:migration create_your_table_name
php artisan make:factory YourModel -m "app\yourmodelname"
here php artisan make:controller YourController
will give me basic controller if we add resource flag after this command it will make a resource controller
php artisan make:controller YourController -r
we can make all 4 file using single command by adding a flag
php artisan make:model YourModel -a
connecting with dabase inside .env file . Whenever we change .env file we have to restart our artisan server
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD
Schema::defaultStringLength(191);
$table->string('title');
$table->text('content');
php artisan migrate
'title' => $faker->sentence,
'content' => $faker->paragraph(6)
# inside run method
factory(Post::class, 30)->create();
Here I generating 30 dummy post
php artisan db:seed
Route::resource('posts', 'PostController');
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
| | GET|HEAD | posts | posts.index | App\Http\Controllers\PostController@index | web |
| | POST | posts | posts.store | App\Http\Controllers\PostController@store | web |
| | GET|HEAD | posts/create | posts.create | App\Http\Controllers\PostController@create | web |
| | GET|HEAD | posts/{post} | posts.show | App\Http\Controllers\PostController@show | web |
| | PUT|PATCH | posts/{post} | posts.update | App\Http\Controllers\PostController@update | web |
| | DELETE | posts/{post} | posts.destroy | App\Http\Controllers\PostController@destroy | web |
| | GET|HEAD | posts/{post}/edit | posts.edit | App\Http\Controllers\PostController@edit | web |
+--------+-----------+-------------------+---------------+---------------------------------------------+--------------+
Route::get('/', 'YourController@method');
public function index () {
return view ('posts.index');
# or
return view ('posts/index');
}
$posts = Post::all();
return view ('posts.index', ['posts' => $posts]);
# or
return view ('posts.index', compact('posts'));
$this->validate($request, [
'title' => 'required|min:3',
'content' => 'required|min:10'
]);
@if($errors->all())
<div class="alert alert-danger">
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</div>
@endif
# cross site request forgery
@csrf
# for update
@method('put')
# for delete
@method('delete')
# inside master.blade.php
@yield('content')
# inside index.balde.php
@extends('master')
@section('content')
@endsection
@foreach($posts as $post)
{{$post->title}}
@endforeach
## inside controller
$posts = Post::orderBy('id', 'desc')->paginate(10);
## inside view
$posts->links()
redirect('/posts');
redirect(route('login'));
redirect()->back();
## for fetching posts from db
$posts = Post::all();
$posts = Post::orderBy('id', 'desc')->paginate(10);
## for creating new record
Post::create([
'title' => $request->title,
'content' => $request->content,
]);
## for deleting
$post->delete();
php artisan make:auth
public function __construct () {
$this->middleware('auth')->except(['index', 'show']);
}
@auth
<form class="d-inline-block float-right" action="{{route('logout')}}" method="post">
@csrf
<button class="btn btn-secondary">{{auth()->user()->name}} | Logout</button>
</form>
@else
<a href="{{route('login')}}" class="btn btn-secondary">Login</a>
@endauth
auth()->user()->name
# RegisterController.php
# LoginController.php
protected $redirectTo = '/posts';
# installing vendor
composer install
# clearing old config
php artisan config:clear
php artisan cache:clear
# making .env file
copy .env.example file to .env file and changed database credentials
# generating unique key for laravel app
php artisan key:generate
# migrate to database
php artisan migrate
# seed with dummy data
php artisan db:seed
# run local development server
php artisan serve
watch laracast jeffrey way's tutorial. He is the best web trainer in the planet. You will agree eventually after watching his tutorial.
My name is Shibu deb polo
Thanks for watching
Take care