- How to create a new Laravel project?
- How to create a page?
- How to create a route?
- How to create a controller?
- How to create a view?
- How to create users from tinker?
- How to read a record?
- How to read records?
- How to create pagination?
- How to create a form?
- How to validate form using Request?
- How to validate form in Controller?
- How to insert a new record?
- How to update a record?
- How to create method spoofing?
- How to get error messages from validators?
- How to create a model and migration script together?
- How to create a model factory?
- How to create seeder file?
- How to call model factory in seeder file?
- How to call seeder file in
DatabaseSeeder.php
? - How to seed data?
Set Route - routes/web.php
Route::get('about-us','PageController@aboutUs')
Create a controller - php artisan make:controller PageController --resource
- and create a method aboutUs
returning view from pages.index
.
public function aboutUs()
{
return view('pages.index');
}
Create the view - create new folder in resources/views/
named pages
. In pages
, create aboutUs.blade.php
. Add the following content:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<h1>About Us</h1>
</div>
</div>
@endsection
Run php artisan tinker
.
Then run factory(\App\User::class, 100)->create();
. This will create 100 users.
You need to method spoofing when calling PUT
and DELETE
method in Laravel since HTTP call does not capture other that GET
and POST
. Following are the sample code when you need to call a PUT
or DELETE
URI using Ajax.
function confirmDelete(id)
{
if(confirm('Are you sure'))
{
var data = {
_token : window.Laravel.csrfToken,
_method : 'delete'
}
$.post('/users/' + id, data, function(data, textStatus, xhr) {
// do something
});
}
}
Create custom request by running php artisan make:request UserRequest
.
Open UserRequest.php
located at app/Http/Requests
.
Add validation rules for your form at rules()
method:
return [
'name' => 'required|min:6|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
];
Next, include UserRequest
namespace in UserController
- use App\Http\Requests\UserRequest;
.
Now in UserController
's store(Request $request)
method, replace store(Request $request)
with store(UserRequest $request)
.
Following are the sample to display an error message
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
You may return to true
in UserRequest
's authorize()
method if there's no particular authorization required.
public function authorize()
{
return true;
}
$this->validate($request, [
'name' => 'required|min:6|max:255',
'password' => 'required|min:6|confirmed',
]);
Just run following command:
php artisan make:model Post -m
This will create a model named Post.php
in app
folder and a migration script timestamp_create_posts_table.php
in database\migrations
folder.
Read more about Creating Columns
After done adding columns, run php artisan migrate
to run the migration script and have your tables in database.
Open up database\factories\ModelFactory.php
and copy the user's factory and update as following:
$factory->define(App\Post::class, function (Faker\Generator $faker) {
return [
'title' => $faker->sentence,
'description' => $faker->text
];
});
Create a seeder file using following command:
php artisan make:seeder PostTableSeeder
Open PostTableSeeder.php
located at database\seeds
folder and call the factory for Post
, as following in run
method:
factory(\App\Post::class, 100)->create();
Open up database\seeds\DatabaseSeeder.php
and call the PostTableSeeder
as following:
public function run()
{
$this->call(PostTableSeeder::class);
}
Run php artisan db:seed