Friday, 18 December 2020

Important points to remember

 Use stack for including css and js file..

<head>
    <!-- Head Contents -->
   @stack('css')
    @stack('js')
</head>

In your template file
@push('css')
    <link rel="stylesheet" href="/build/assets/css/laravel.css">
@endpush

@push('css')
    <style> .....</style>
@endpush


@push('js')
    <script src="/example.js"></script>
@endpush

******************** **************************************************

Option for  isset

{{ isset($data) ? $data  : "Not available" }}

{{ $data or "Not available" }}

***********************************************************************

To include a Blade view from within another view. All variables that are available to the parent view will be made available to the included view:

<div>
    @include('shared.errors')
    <form>
        <!-- Form Contents -->
    </form>
</div>

and may also pass an array of extra data to the included view:

@include('view.name', ['some' => 'data'])

*************************************************************************

In env file you can make  production / local / staging

if (App::environment('local', 'staging')) {
    // The environment is either local OR staging...
}

'debug' => env('APP_DEBUG', false),
*************************************************************************
To reduce the length of password in registration. Open the RegisterController and set the new
length.
 
'password' => ['required', 'string', 'min:4', 'confirmed'],
 
*************************************************************************
How To Bypass CSRF Token For A Route In Laravel?
 
Path:- /app/Http/Middleware/VerifyCsrfToken.php

Add Below Code In The Class:-
protected $except = [
         'api/*', //For All Route starting with api.
         'your_url_here/', //For Specific url.
    ];

*************************************************************************

Stopping On First Validation Failure

Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:

$request->validate([
    'title' => 'bail|required|unique:posts|max:255',
    'body' => 'required',
]);

*************************************************************************

 If you do not want the validator to consider null values as invalid. For example:


$request->validate([
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
    'publish_at' => 'nullable|date',
]);

*************************************************************************
Reverse routing
From the controller and action name, convert back to the url.

In the web.php
Route::get('/mypage', 'HomeController@test');
you can change any name in route. You don't have to change in the blade file. The function
remains the same.

<a href="{{action('HomeController@test')}}"> Test Page</a>

HomeController
Class HomeController extends Controller {

function test() {
        return 'Hi, This is test page';
}

}
 *************************************************************************
Query Scope

I want  'active' Users from User table. Currently we are using where queries like this.
$activeUsers = User::where('status', 1)->get();

But we need same thing in any other module also. So we need write again and again this where queries. Now, we will use Scope Query. Create a scopeName function in model. Below is the user model.

public function scopeActive($query,$value)
{
    return $query->where('status', $value);
}

In different module or controller
Use App\user;

In function you can use like this.

$data_array = User::active(0)->get();
 
 *************************************************************************

Life cycle of Request

Cover image for Laravel Request Lifecycle
 

 

 

No comments:

Post a Comment

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...