Developer Snippet Diary

Blade, Layouts, script, Subviews and other functions

resources>layouts/app.blade.php (work as masterpage)

it will contain all reusable code

<title>@yield('title')</title>  <!-- Child views can inject their own page title here @section('title', 'Home Page') -->
<div class="main">

@yield('content')  <!-- childview inject it-->
</div>

@include ('layouts.scripts') //this will contain all scripts tags

resources/views/child.blade.php

@extends('layouts.app')
@section('title','Home page')

<h1> I am sidebar </h1>

@section('content')
<h1> I am content</h1>
@endsection

_______________________

>> How to add scripts in ordered way laravel

app.blade.php

<script src="bootstrap.bundle.min.js"></script>
@stack('scripts') <!--any child template put his scripts here -->

child.blade.php

@push('scripts')
<script src="{{asset('dashboard.js')}}"></script> <!--ok am pushing it into app.blade.php -->
@endpush

>> HOW TO add css , js files in views >> it will see public/assets/css/style.css

<link rel="stylesheet" href="{{ asset('assets/css/style.css') }}">

>> How to echo Get request in blade ($_GET['monthYear'])

<input type="month" name="monthYear" value="{{app('request')->input('monthYear')}}">

>> How to echo variable 

<p>{{$msg}} </p>
<p>{{ "<h1>".$msg."</h1>"}} </p> // h1 tags not render
<p>{!! "<h1>".$msg."</h1>" !!} </p> // h1 rendered

>>comments

{{-- <p>comment</p> --}}

>> conditions

@if(!empty($msg))
<p> msg is not empty </p>
@else
<p>msg is empty</p>
@endif

@for($i=0;$i<=5;$i++)
<p>loop</p>
@endfor

By default its add htmlspecialchars() but if use {{!! $variable !!}} then data is not escaped

Hello, {!! $name !!}.

Pass json to javascript

<script> var app = {{ Js::from($array) }}; </script>

NOT sign in

@unless (Auth::check())
    You are not signed in.
@endunless

Authenticated user or not

@auth  // The user is authenticated... @endauth
 
@guest // The user is not authenticated... @endguest

Isset and empty

@isset($records)   // $records is defined and is not null... @endisset
 
@empty($records)   // $records is "empty"... @endempty

Switch

@switch($i)
    @case(1)
        First case...
     @break

    @case(2)
        Second case...
     @break
 
    @default
        Default case...
@endswitch

LOOP

@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor
 
@foreach ($users as $user)
    <p>This is user {{ $user->id }}</p>
@endforeach
 
@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse
 
@while (true)
    <p>I'm looping forever.</p>
@endwhile

LOOP variables

@foreach ($users as $user)
    @if ($loop->first) //This is the first iteration. @endif
    @if ($loop->last) //This is the last iteration. @endif
    <p>This is user {{ $user->id }}</p>
@endforeach

/*
$loop->index	 //(starts at 0).
$loop->iteration  //(starts at 1).
$loop->remaining	//The iterations remaining in the loop.
$loop->count
$loop->first	//when first iteration through the loop.
$loop->last	//last iteration
$loop->even	//even iteration
$loop->odd	//odd iteration 
$loop->depth	//The nesting level of the current loop.
$loop->parent //When in a nested loop, the parent's loop variable.
*/

PHP

@php
    $isActive = false;
    $hasError = true;
@endphp

Sub View add

@include('shared.errors')
@include('view.name', ['status' => 'complete']) //with data 
@includeIf('view.name', ['status' => 'complete']) //if view exist, in @include if not exist its throw error
@includeWhen($boolean, 'view.name', ['status' => 'complete']) //conditional include
COMPONENTS:

To use code again and again we create components , below command create app/http/views/Alert.php class and resources/views/components/alert.blade.php

php artisan make:component Alert

alert.blade.php

<div>
    Its Alert
</div>

To render in view we need ie home.blade.php, x represent component and alert is component name

<x-alert/>

 

To make component dynamic 

1. app/http/views/Alert.php

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Alert extends Component
{
    public $message;
    public $class;
    public function __construct($message,$class)
    {
        $this->message = $message;
        $this->class = $class;
    }
    public function render()
    {
        return view('components.alert');
    }
}

component view

<div class="{{$class}}">
    Its Alert with message {{ $message }}
</div>

in blade

<x-alert message="Hello world" class="danger" />
Posted by: R GONDAL
Email: rizikmw@gmail.com