Laravel Cheatsheet

Laravel Cheatsheet

Laravel cheatsheet.

Installation

sudo apt install mysql-server php-xml php-mysql php-bcmath php-mbstring

Removephp-bcmathif not going to use Telescope

Install composer and set path

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Assuming composer is instaled in/home/ravindra/Installation/Composerthen path values should be as follows.

PATH=$PATH:/home/ravindra/Installation/Composer
PATH=$PATH:$HOME/.config/composer/vendor/bin

Install laravel installer

composer global require laravel/installer

Model factory

Generate 20 fake users

$users = factory(App\User::class, 20)->create();

Generate 10 fake users with account_type variable set to admin

$user = factory(App\User::class, 5)->create([
	'account_type' => 'admin'
]);

Authorization

  • Abort if user doesn’t own project (In Controller)
public function show(Project $project){
	abort_if ($project->owner_id != auth()->id(), 403);
}
  • Adding a method to user model (In Controller)
public function show(Project $project){
	abort_if (! auth()->user->owns($project), 403);
}

or

public function show(Project $project){
	abort_unless (auth()->user->owns($project), 403);
}

Using Policy

  • Create a policy for project model (In Terminal)
php artisan make:policy -m Project ProjectPolicy

or

php artisan make:policy ProjectPolicy --model=Project
  • Register a policy (In AuthServiceProvider)

Openapp/Providers/AuthServiceProvider.phpand change$policiesarray according to your need

protected $policies = [
	'App\Project' => 'App\Policies\ProjectPolicy',
]
  • Abort if user doesn’t own the project (In Policy)
public function view(User $user, Project $project){
	return project->owner_id == $user->id;
}
  • Using without project (In Policy)
public function create(User $user){
	return $user->account_type == 'Admin';
}
  • Using policy with project (In Controller)
public function show(Project $project){
	$this->authorize('view', $project);
}
  • Using policy without project (In Controller)
public function create(){
	$this->authorize('create', Project::class);
}

Using Gate

  • Using gate (In Controller)
public function update(Project $project){
	abort_if(\Gate::denies('update', $project), 403);
}

or

public function update(Project $project){
	abort_unless(\Gate::allows('update', $project), 403);
}

Using Routes

  • Using middleware on a resource (In web.php)
Route::resource('project', 'ProjectController')
->middleware('can:update, project');

Using Views

  • Using blade files (random.blade.php)
@can('update', $project)
	update related stuff
@endcan

Admin Override

Openapp/Providers/AuthServiceProvider.php

\Illuminate\Auth

public function boot(Gate $gate){
	$this->registerPolicies();
	
	$gate->before(function ($user){
		return $user->isAdmin();
	});
}

Setting Laravel Passport

  • Install Passport via composer:composer require laravel/passport
  • Migrate Database:php artisan migrate
  • Install to generate keys:php artisan passport:installand save output somewhere or don’t
  • Extend User Model by adding HasApiTokens Trait ```php use Laravel\Passport\HasApiTokens;

class User extends Authenticatable { use HasApiTokens, Notifiable; }

- Register routes in `AuthServiceProvider`
```php
use Laravel\Passport\Passport;
class AuthServiceProvider extends ServiceProvider
{

    public function boot()
    {
        $this->registerPolicies();
        Passport::routes();
    }
}
  • Config auth driver:config/auth.phpset driver and guard to passport
      'guards' => [
              'web' => [
                      'driver' => 'session',
                      'provider' => 'users',
              ],
    
              'api' => [
                      'driver' => 'passport', //here
                      'provider' => 'users',
              ],
      ],
    
  • Pull Vue Components:php artisan vendor:publish --tag=passport-components
  • Register components: ```js Vue.component( ‘passport-clients’, require(‘./components/passport/Clients.vue’).default );

Vue.component( ‘passport-authorized-clients’, require(‘./components/passport/AuthorizedClients.vue’).default );

Vue.component( ‘passport-personal-access-tokens’, require(‘./components/passport/PersonalAccessTokens.vue’).default );


# Deploy
- `php artisan passport:keys`
- `php artisan passport:client`


### Tinker Tricks
## Truncate a table
```php
	User::truncate();

Original: Updated: