Results: 71
Creates all 7 routes for the resource to cover CRUD functionality
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 form
Makes controller for
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 storage
Pass parameter to view using
with
method
public 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
public function services() {
        $data = [
            'title' => 'langs',
            'languages' => ['PHP', 'Java', 'Python']
        ];
        return view('pages/services')->with($data);
}
Receive the passed array
@extends('layouts.app')

@section('cntnt')
    <h3>{{$title}}</h3>
    @if(count($languages))
        @foreach($languages as $language)
            <li>{{$language}}</li>
        @endforeach
    @endif
@endsection
Makes model called
Post
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 model
php artisan make:model Post -m
The migration file location is
database/migrations/2020_09_02_041219_create_posts_table.php
controller returns plain text & HTML & view
Returns plain text
public function contact() {
        return 'Under Construction';
}
Returns HTML
public 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');
}
upload multiple files at once and validate file mimes on front / back
In order to upload several files at once we need to specify
multiple
attribute and append
[]
to input name
<input type="file" multiple="multiple" name="files[]" placeholder="Attach file"/>
To validate show errors on client side we have to append input name with
.*
Complete example of the
form-group
<div class="form-group">
    <label for="files">Attach File</label> &nbsp;
    <i id="add_new_file" class="ki ki-plus text-success pointer" title="Add New File"></i>
    <input type="file" class="form-control mb-2 {{ $errors->has('files.*') ? 'is-invalid' : '' }}" multiple="multiple" name="files[]" placeholder="Attach file"/>
    @if($errors->has('files.*'))
        <div class="invalid-feedback">
            <strong>{{ $errors->first('files.*') }}</strong>
        </div>
    @endif
</div>
Server side validation of mime types
public function rules()
{
    return [
        'comment' => 'sometimes|nullable|min:2|max:255',
        'files.*' => 'sometimes|nullable|mimes:pdf,docx,xlsx,pptx,rar,zip,png,jpg',
    ];
}
It's necessary the validator key
files
to be appended with the same symbols
.*
Lists logged in user's posts on dashboard
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('dashboard')->with('posts', $user->posts);
user and role relationship caused problem with Spatie package
The relationship caused problems We should not write this many to many relationship inside User's model if we use Spatie permissions package
public function roles()
{
    return $this->belongsToMany(Role::class, 'model_has_roles', 'model_id', 'role_id');
}
$errors->has() and "is-invalid" related issue
$errors->has('accountable_id')
did not work
@if($errors->has('accountable_id'))
    <div class="invalid-feedback">
        <strong>{{ $errors->first('accountable_id') }}</strong>
    </div>
@endif
Until
is-invalid
class was specified
class="form-control {{ $errors->has('accountable_id') ? 'is-invalid' : '' }}"
The complete example
<div class="form-group">
    <label for="exampleSelect2">Accountable <span class="text-danger">*</span></label>
    <select class="form-control {{ $errors->has('accountable_id') ? 'is-invalid' : '' }}" id="accountable_id" name="accountable_id">
        <option value="">Select Accountable</option>
        @foreach($users as $accountable)
            <option value="{{ $accountable->id }}" {{ $accountable_id == $accountable->id ? 'selected' : '' }}>{{ $accountable->name }}</option>
        @endforeach
    </select>
    @if($errors->has('accountable_id'))
        <div class="invalid-feedback">
            <strong>{{ $errors->first('accountable_id') }}</strong>
        </div>
    @endif
</div>
Results: 71