{"flag":true,"single":true,"pageTitle":"Blade, Layouts, script, Subviews and other functions","post":{"id":3,"user_id":"1","slug":"blade-template-create-layouts-scripts-add-comments-conditions-array-to-js-auth-check-isset-empty-switch-loop-variables-include-subviews--odsu","title":"Blade, Layouts, script, Subviews and other functions","body":"<p><strong>resources&gt;layouts\/app.blade.php<\/strong> (work as masterpage)<\/p>\r\n<p>it will contain all reusable code<\/p>\r\n<pre class=\"language-markup\"><code>&lt;title&gt;@yield('title')&lt;\/title&gt;  &lt;!-- Child views can inject their own page title here @section('title', 'Home Page') --&gt;\r\n&lt;div class=\"main\"&gt;\r\n\r\n@yield('content')  &lt;!-- childview inject it--&gt;\r\n&lt;\/div&gt;\r\n\r\n@include ('layouts.scripts') \/\/this will contain all scripts tags<\/code><\/pre>\r\n<p><strong>resources\/views\/child.blade.php<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@extends('layouts.app')\r\n@section('title','Home page')\r\n\r\n&lt;h1&gt; I am sidebar &lt;\/h1&gt;\r\n\r\n@section('content')\r\n&lt;h1&gt; I am content&lt;\/h1&gt;\r\n@endsection<\/code><\/pre>\r\n<p>_______________________<\/p>\r\n<p><strong>&gt;&gt; How to add scripts in ordered way laravel<\/strong><\/p>\r\n<p>app.blade.php<\/p>\r\n<pre class=\"language-markup\"><code>&lt;script src=\"bootstrap.bundle.min.js\"&gt;&lt;\/script&gt;\r\n@stack('scripts') &lt;!--any child template put his scripts here --&gt;\r\n<\/code><\/pre>\r\n<p>child.blade.php<\/p>\r\n<pre class=\"language-markup\"><code>@push('scripts')\r\n&lt;script src=\"{{asset('dashboard.js')}}\"&gt;&lt;\/script&gt; &lt;!--ok am pushing it into app.blade.php --&gt;\r\n@endpush<\/code><\/pre>\r\n<p><strong>&gt;&gt; HOW TO add css , js files in views &gt;&gt; it will see public\/assets\/css\/style.css<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;link rel=\"stylesheet\" href=\"{{ asset('assets\/css\/style.css') }}\"&gt;<\/code><\/pre>\r\n<p><strong>&gt;&gt; How to echo Get request in blade ($_GET['monthYear'])<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;input type=\"month\" name=\"monthYear\" value=\"{{app('request')-&gt;input('monthYear')}}\"&gt;<\/code><\/pre>\r\n<p><strong>&gt;&gt; How to echo variable&nbsp;<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;p&gt;{{$msg}} &lt;\/p&gt;\r\n&lt;p&gt;{{ \"&lt;h1&gt;\".$msg.\"&lt;\/h1&gt;\"}} &lt;\/p&gt; \/\/ h1 tags not render\r\n&lt;p&gt;{!! \"&lt;h1&gt;\".$msg.\"&lt;\/h1&gt;\" !!} &lt;\/p&gt; \/\/ h1 rendered<\/code><\/pre>\r\n<p><strong>&gt;&gt;comments<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>{{-- &lt;p&gt;comment&lt;\/p&gt; --}}<\/code><\/pre>\r\n<p><strong>&gt;&gt; conditions<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@if(!empty($msg))\r\n&lt;p&gt; msg is not empty &lt;\/p&gt;\r\n@else\r\n&lt;p&gt;msg is empty&lt;\/p&gt;\r\n@endif\r\n\r\n@for($i=0;$i&lt;=5;$i++)\r\n&lt;p&gt;loop&lt;\/p&gt;\r\n@endfor<\/code><\/pre>\r\n<p>By default its add<strong> htmlspecialchars() <\/strong>but if use<strong> {{!! $variable !!}}<\/strong> then data is not&nbsp;escaped<\/p>\r\n<pre class=\"language-markup\"><code>Hello, {!! $name !!}.<\/code><\/pre>\r\n<p><strong>Pass json to javascript<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;script&gt; var app = {{ Js::from($array) }}; &lt;\/script&gt;<\/code><\/pre>\r\n<p><strong>NOT sign in<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@unless (Auth::check())\r\n    You are not signed in.\r\n@endunless<\/code><\/pre>\r\n<p><strong>Authenticated user or not<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@auth  \/\/ The user is authenticated... @endauth\r\n \r\n@guest \/\/ The user is not authenticated... @endguest<\/code><\/pre>\r\n<p><strong>Isset and empty<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@isset($records)   \/\/ $records is defined and is not null... @endisset\r\n \r\n@empty($records)   \/\/ $records is \"empty\"... @endempty<\/code><\/pre>\r\n<p><strong>Switch<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@switch($i)\r\n    @case(1)\r\n        First case...\r\n     @break\r\n\r\n    @case(2)\r\n        Second case...\r\n     @break\r\n \r\n    @default\r\n        Default case...\r\n@endswitch<\/code><\/pre>\r\n<p><strong>LOOP<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@for ($i = 0; $i &lt; 10; $i++)\r\n    The current value is {{ $i }}\r\n@endfor\r\n \r\n@foreach ($users as $user)\r\n    &lt;p&gt;This is user {{ $user-&gt;id }}&lt;\/p&gt;\r\n@endforeach\r\n \r\n@forelse ($users as $user)\r\n    &lt;li&gt;{{ $user-&gt;name }}&lt;\/li&gt;\r\n@empty\r\n    &lt;p&gt;No users&lt;\/p&gt;\r\n@endforelse\r\n \r\n@while (true)\r\n    &lt;p&gt;I'm looping forever.&lt;\/p&gt;\r\n@endwhile<\/code><\/pre>\r\n<p><strong>LOOP variables<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@foreach ($users as $user)\r\n    @if ($loop-&gt;first) \/\/This is the first iteration. @endif\r\n    @if ($loop-&gt;last) \/\/This is the last iteration. @endif\r\n    &lt;p&gt;This is user {{ $user-&gt;id }}&lt;\/p&gt;\r\n@endforeach\r\n\r\n\/*\r\n$loop-&gt;index\t \/\/(starts at 0).\r\n$loop-&gt;iteration  \/\/(starts at 1).\r\n$loop-&gt;remaining\t\/\/The iterations remaining in the loop.\r\n$loop-&gt;count\r\n$loop-&gt;first\t\/\/when first iteration through the loop.\r\n$loop-&gt;last\t\/\/last iteration\r\n$loop-&gt;even\t\/\/even iteration\r\n$loop-&gt;odd\t\/\/odd iteration \r\n$loop-&gt;depth\t\/\/The nesting level of the current loop.\r\n$loop-&gt;parent \/\/When in a nested loop, the parent's loop variable.\r\n*\/<\/code><\/pre>\r\n<p><strong>PHP<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@php\r\n    $isActive = false;\r\n    $hasError = true;\r\n@endphp<\/code><\/pre>\r\n<p><strong>Sub View add<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>@include('shared.errors')\r\n@include('view.name', ['status' =&gt; 'complete']) \/\/with data \r\n@includeIf('view.name', ['status' =&gt; 'complete']) \/\/if view exist, in @include if not exist its throw error\r\n@includeWhen($boolean, 'view.name', ['status' =&gt; 'complete']) \/\/conditional include\r\n<\/code><\/pre>\r\n<h5><span style=\"font-size: 14pt;\"><strong>COMPONENTS:<\/strong><\/span><\/h5>\r\n<p><span style=\"font-size: 12pt;\">To use code again and again we create components , below command create <strong>app\/http\/views\/Alert.php<\/strong> class and <strong>resources\/views\/components\/alert.blade.php<\/strong><\/span><\/p>\r\n<pre class=\"language-markup\"><code>php artisan make:component Alert<\/code><\/pre>\r\n<p><strong>alert.blade.php<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;div&gt;\r\n    Its Alert\r\n&lt;\/div&gt;<\/code><\/pre>\r\n<p>To render in view we need ie home.blade.php, x represent component and alert is component name<\/p>\r\n<pre class=\"language-markup\"><code>&lt;x-alert\/&gt;<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>To make component dynamic&nbsp;<\/p>\r\n<p>1. a<strong style=\"font-size: 16px;\">pp\/http\/views\/Alert.php<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\n\r\nnamespace App\\View\\Components;\r\n\r\nuse Illuminate\\View\\Component;\r\n\r\nclass Alert extends Component\r\n{\r\n    public $message;\r\n    public $class;\r\n    public function __construct($message,$class)\r\n    {\r\n        $this-&gt;message = $message;\r\n        $this-&gt;class = $class;\r\n    }\r\n    public function render()\r\n    {\r\n        return view('components.alert');\r\n    }\r\n}\r\n<\/code><\/pre>\r\n<p>component view<\/p>\r\n<pre class=\"language-markup\"><code>&lt;div class=\"{{$class}}\"&gt;\r\n    Its Alert with message {{ $message }}\r\n&lt;\/div&gt;<\/code><\/pre>\r\n<p>in blade<\/p>\r\n<pre class=\"language-markup\"><code>&lt;x-alert message=\"Hello world\" class=\"danger\" \/&gt;<\/code><\/pre>","category_id":"2","is_private":"0","created_at":"2023-03-14T04:11:43.000000Z","updated_at":"2026-02-05T06:05:38.000000Z","category":{"id":2,"user_id":"1","name":"Laravel Core","slug":"laravel-nhyt","parent_id":"1","created_at":"2023-03-14T03:58:27.000000Z","updated_at":"2023-03-20T11:30:50.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"resources&gt;layouts\/app.blade.php (work as masterpage) it will contain all reusable code &lt;title&gt;@yield('title')&lt;\/title&gt;  &lt;!- - Blade, Layouts, script, Subviews and other functions (Updated: February 5, 2026) - Read more about Blade, Layouts, script, Subviews and other functions at my programming site [SITE]","categories":[]}