Adding in code snippets/partials on a page - php

How do I include snippets, or partials in pages in Laravel 5? In node/angular, it's quite easy to simply load different modules and such on a page.
For example, on my home page, I'm looping through some data:
<h1>Home</h1>
#if (count($practice))
<ul>
#foreach($practice as $val)
<li>{{ $val }}</li>
#endforeach
</ul>
#endif
If I include the login snippet on the page, it covers up the rest of the data:
<h1>Home</h1>
#if (count($practice))
<ul>
#foreach($practice as $val)
<li>{{ $val }}</li>
#endforeach
</ul>
#endif
#include('auth.login')

Try to include it in a new row :
<h1>Home</h1>
#if (count($practice))
<div class="row">
<div class="col-md-12">
<ul>
#foreach($practice as $val)
<li>{{ $val }}</li>
#endforeach
</ul>
</div>
</div>
#endif
<div class="row">
<div class="col-md-12">
#include('auth.login')
</div>
</div>

Related

ErrorException Undefined variable $name

I'm getting this error when clicking a button on my dashboard
And this error only happens in the customization part, in other sections everything works fine
It gives me the solution below but I don't know how to apply
$name is undefined
Make the variable optional in the blade template. Replace {{ $name }} with {{ $name ?? '' }}
Undefined variable $name (View: /home/dir/mysite.com/resources/views/admin/settings/personalization.blade.php)
#push('styles_top')
#endpush
#section('content')
<section class="section">
<div class="section-header">
<h1>{{ trans('admin/main.personalization') }} {{ trans('admin/main.settings') }}</h1>
<div class="section-header-breadcrumb">
<div class="breadcrumb-item active">{{ trans('admin/main.dashboard') }}</div>
<div class="breadcrumb-item active">{{ trans('admin/main.settings') }}</div>
<div class="breadcrumb-item ">{{ trans('admin/main.personalization') }}</div>
</div>
</div>
<div class="section-body">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
#php
$items = ['page_background','home_sections','home_hero','home_hero2','home_video_or_image_box',
'panel_sidebar','find_instructors','reward_program','become_instructor_section',
'theme_colors', 'theme_fonts', 'forums_section', 'navbar_button','cookie_settings','mobile_app',
'others_personalization'
]
#endphp
<ul class="nav nav-pills" id="myTab3" role="tablist">
#foreach($items as $item)
<li class="nav-item">
<a class="nav-link {{ ($item == $name) ? 'active' : '' }}" href="/admin/settings/personalization/{{ $item }}">{{ trans('admin/main.'.$item) }}</a>
</li>
#endforeach
</ul>
<div class="tab-content">
#include('admin.settings.personalization.'.$name,['itemValue' => (!empty($values)) ? $values : ''])
</div>
</div>
</div>
</div>
</div>
</div>
</section>
#endsection
#push('scripts_bottom')
#endpush
I've tried several solutions suggested in other posts, but none worked.
What's wrong?
IN:
/home/dir/mysite.com/resources/views/admin/settings/personalization.blade.php)
error: You have not any variable $name.
Solution:
check that into controller, you are sending variable with view file?
return view('file_path')->with("name", $valueVariable);
return view("file_path".["name" => $valueVariable]);

How to customize default pagination in laravel 8?

I'm building an application where some page needs pagination. I already made a design for my application's pagination. But how can I use that with laravel paginate?
I did this for paginate in the controller
$products= Products::paginate(9);
In my blade, this is my desired HTML for pagination
<div class="row pt-3 pb-5">
<div class="col-12 d-flex justify-content-end">
<div class="pagination-btn-box">
<button class="active-pagination-btn">1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>...</button>
<button>Next</button>
</div>
</div>
</div>
Now, How can I implement this with laravel for paginating?
In controller return view
return view('blade file name',compact('products'))
in blade do like below
<div class="container">
#foreach ($products as $product)
{{ $product->fieldname}}
#endforeach
</div>
{{ $products->links() }}
$products->links() will generate default pagination.
if you want to customize pagination template then run following command
php artisan vendor:publish --tag=laravel-pagination
this will generate folder in views/vendor/pagination
default.blad.php look like below
#if ($paginator->hasPages())
<nav>
<ul class="pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled" aria-disabled="true" aria-label="#lang('pagination.previous')">
<span aria-hidden="true">‹</span>
</li>
#else
<li>
‹
</li>
#endif
{{-- Pagination Elements --}}
#foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
#if (is_string($element))
<li class="disabled" aria-disabled="true"><span>{{ $element }}</span></li>
#endif
{{-- Array Of Links --}}
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page == $paginator->currentPage())
<li class="active" aria-current="page"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach
#endif
#endforeach
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li>
›
</li>
#else
<li class="disabled" aria-disabled="true" aria-label="#lang('pagination.next')">
<span aria-hidden="true">›</span>
</li>
#endif
</ul>
</nav>
#endif
Still if you want to use your own pagination view then its possible .First you need to register your custom view in Service Provider
<?php
namespace App\Providers;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//defaultStringLength changed to 191 to support mysql below 5.7
Schema::defaultStringLength(191);
Paginator::defaultView('custom-pagination');
Paginator::defaultSimpleView('simple-pagination');
}
}
so here in my example i have created two blade template called custom-pagination.blade.php and simple-pagination.blade.php
Ref:https://laravel.com/docs/8.x/pagination#displaying-pagination-results

How can I get information about errors in laravel 6.0?

When I used Laravel 5.8 I could get information about errors like this :
#if(count($errors > 0))
<ul class="error-list" >
#foreach($errors->all() as $error)
<li class="error-list-element">{{$error}}</li>
#endforeach
</ul>
#endif
It worked completly fine.
Since Laravel 6.0 was released the code above results in error :
Object of class Illuminate\Support\ViewErrorBag could not be converted to int
So how can I get information about errors in Laravel 6.0?
Here is code :
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Hope this will help :)
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

laravel breadcrumbs not rendering html in Laravel 5.4

Within a Layout
#section('breadcrumbs', Breadcrumbs::render('messages'))
#section('content')
#include('layouts.breadcrumbs')
breadcrumbs.blade.php
<div class="fluid-container">
<div class="container">
<div class="row">
<div class="col-md-12">
#yield('breadcrumbs')
</div>
</div>
</div>
</div>
Standard BS3 view with DaveJamesMillar Breadcrumbs
#if ($breadcrumbs)
<ol class="breadcrumb">
#foreach ($breadcrumbs as $breadcrumb)
#if ($breadcrumb->url && !$breadcrumb->last)
<li>{{ $breadcrumb->title }}</li>
#else
<li class="active">{{ $breadcrumb->title }}</li>
#endif
#endforeach
</ol>
#endif
Appeared to be working fine until upgrading to L5.4, now rather than displaying the breadcrumbs it displays non-processed HTML
<ol class="breadcrumb"> <li>Home</li> <li> class="active">Messages</li></ol>
After reading the latest docs for davejamesmillar laravel-breadcrumbs with support for L5.4 https://media.readthedocs.org/pdf/laravel-breadcrumbs/latest/laravel-breadcrumbs.pdf with reference to 1.4.2 using Blade Sections nothing appears to have changed in the way this needs to be coded. Unsure why the HTML is not being processed to display as a link.
RAR, hours later! Appears Laravel 5.4 runs a htmlentities when injecting a variable into a #section
I changed
#section('breadcrumbs', Breadcrumbs::render('messages'))
to
#section('breadcrumbs') {!! Breadcrumbs::Render('messages') !!} #endsection
And the html is now being processed and displayed as it should.

Laravel 4 Trying to get property of non-object error

I was working on my website and I logged out to test it and it gave me this error:
Trying to get property of non-object (View: C:\xampp\htdocs\mom\a\app\views\home.blade.php)
Here is my home.blade.php:
#extends('layout.main')
#section('title') Home #stop
#section('content')
<!-- User signed in -->
#if(Auth::check())
<!-- User is not admin -->
<p>Welcome back, {{ Auth::user()->username }}.</p>
#else
<!-- Nothing -->
#endif
#if($posts->count())
#foreach($posts as $post)
<article class="col-md-8 col-md-push-2 well well-white home-posts">
<h2 class="title">{{ $post->title }}</h2>
<hr class="post-break">
<h4 class="home-content">{{ Markdown::parse(Str::limit($post->body, 300)) }}</h4>
<div class="post-footer">
<ul class="footer-group">
<ul>
<li>Published {{ $post->created_at->diffForHumans() }}</li>
<li> Read more →</li>
</ul>
#if(Auth::user()->group ==1)
<ul class="footer-right">
<li class="red"><i class="fa fa-trash"></i> Delete</li>
<li><i class="fa fa-gear"></i> Edit</li>
</ul>
#else
<!-- Nothing -->
#endif
</ul>
</div>
</article>
#endforeach
#endif
<div class="col-md-8 col-md-push-2 ">{{ $posts->appends(array())->links() }}</div>
#stop
If someone could help me find out what property is of the non object is being called and help me fix it. That would be great. Much appreciated.
You should put this line
<div class="col-md-8 col-md-push-2 ">{{ $posts->appends(array())->links() }}</div>
in your if statement
EDIT:
Try to change line:
#if(Auth::user()->group ==1)
to
#if(Auth::check() && Auth::user()->group ==1)

Categories