using sidebar in different blades - php

I'm a newbie in laravel and actually I have to write the sidebar in every blade of the application to make it work, but I'd like using a different sidebar depending on the section of the site where I am.
So this is what I'm trying to do:
EDIT 1
layouts/main.blade.php
<div class="wrapper">
<div class="sidebar" data-color="brown" data-active-color="danger">
<div class="logo">
<!-- Content -->
</div>
<!-- Sidebar -->
#if(request()->is("{{ url('/')}}/{operator}"))
#include('operator.sidebar')
#else
#include('stduser.sidebar')
#endif
<!-- End sidebar -->
</div>
<div class="main-panel">
<!-- Navbar -->
<nav></nav>
<!-- End navbar -->
<!-- Main content section -->
#yield('main-panel')
<!-- End main content section -->
<!-- Footer -->
<footer></footer>
<!-- End footer -->
</div>
</div>
stduser/dashboard.blade.php
#extends('layouts.main')
#section('main-panel')
<!-- Main panel contents -->
#endsection
#section('extrajs')
<!-- script contents -->
#endsection
stduser/sidebar.blade.php
<div class="sidebar-wrapper">
<div class="user btn-rotate">
<div class="photo">
<i class="fa fa-user-circle-o fa-2x" aria-hidden="true" style="color:#fff"></i>
</div>
<div class="info">
<a href="{{ url('/user') }}/profile">
<span>
{{ Auth::user()->name }}
</span>
</a>
<div class="clearfix"></div>
</div>
</div>
<ul class="nav">
<li class="active btn-rotate">
<a href="{{ url('/') }}">
<i class="nc-icon nc-bank"></i>
<p>Companies</p>
</a>
</li>
</ul>
</div>
operator/sidebar.blade.php
<div class="sidebar-wrapper">
<div class="user btn-rotate">
<div class="photo">
<i class="fa fa-user-circle-o fa-2x" aria-hidden="true" style="color:#fff"></i>
</div>
<div class="info">
<a href="{{ url('/user') }}/profile">
<span>
{{ Auth::user()->name }}
</span>
</a>
<div class="clearfix"></div>
</div>
</div>
<ul class="nav">
<li class="active btn-rotate">
<a href="{{ url('/') }}/{{ $operator->id }}/about">
<i class="fa fa-tachometer" aria-hidden="true"></i>
<p>DashBoard</p>
</a>
</li>
<li class="btn-rotate">
<a href="{{ url('/')}}/{{ $operator->id}}/suppliers">
<i class="fa fa-link" aria-hidden="true"></i>
<p>Suppliers</p>
</a>
</li>
<li class="btn-rotate">
<a href="{{ url('/')}}/{{ $operator->id}}/products">
<i class="fa fa-product-hunt" aria-hidden="true"></i>
<p>Products</p>
</a>
</li>
</ul>
</div>
This is how my views are structured:
Is there a way to make it work?

you can include the blade file like so #include('layouts/sidebar_' . $sidebarName) and if you want to avoid errors when include doesnt exist you can use #includeIf('view.name', ['some' => 'data'])
So you have just the include statement and the sidebar content only once

Based on your feedback to addi2113's answer, it seems like you're wanting to switch out the sidebar include based on which page you're on. There are several ways to do this. The simplest (yet least flexible) way to do this would be to show a certain sidebar based on the route. For instance, if you have a predictable route structure for all "operator" pages, such as example.com/operator/*, you could do the following by using an #if statement in your blade view. Like this:
#if(request()->is("/unique/url/pattern"))
#include('operator.sidebar')
#else
#include('stduser.sidebar')
#endif
Obviously, you can edit this to use any logic you want, but this is a somewhat simple way to handle this.
EDIT: Try this in your main.blade instead of using a section and yield:
<div class="wrapper">
<div class="sidebar" data-color="brown" data-active-color="danger">
<div class="logo">
<!-- Content -->
</div>
<!-- Sidebar -->
<div class="sidebar-wrapper">
<div class="user btn-rotate">
<div class="photo">
<i class="fa fa-user-circle-o fa-2x" aria-hidden="true" style="color:#fff"></i>
</div>
<div class="info">
<a href="{{ url('/') }}/profile">
<span>
{{ Auth::user()->name }}
</span>
</a>
<div class="clearfix"></div>
</div>
</div>
#if(request()->is('/unique/url/pattern'))
#include('operator.sidebar')
#else
#include('stduser.sidebar')
#endif
</div>
</div>
</div>
EDIT 2:
Since it appears you are using a dynamic URL for the operator pages, you have two options. The first option is to make your operator routes more unique than they currently are so that you can use an asterisk to denote all routes of a current pattern. For instance, in routes/web.php, change your routes for operator pages to this type of pattern:
Route::get('/operator/{operator}/about','OperatorController#about')->name('operator-about');
By adding the operator slug into the url, you now have a UNIQUE path that you can reference. Then, in your main blade, you would reference all of the operator routes together like this:
#if(request()->is('/operator/*'))
#include('operator.sidebar')
By making the URL unique, you have made a very simple way to reference all routes where you want to show the operator sidebar.
Another option, which is not as robust in my opinion, is to refer to the specific routes by naming. For instance, using the route I defined up above with the name of "operator-about", I could show the operator sidebar like this:
#if(Route::currentRouteName()=="operator-about")
#include('operator.sidebar')
You would then expand upon this by explicitly defining all named routes that you would want to show the operator sidebar for. As you can probably tell, this will get messy if there are a lot of routes you want to include. I don't recommend to do it this way, but you can feel free to solve the problem however you want.

Related

Why did laravel wrap my sidebar in an em tag?

**Using Laravel 5.8.31 & Bootstrap 4. Browser: Chrome v. 76.0.3 and Opera 62.0.33 **
I was testing my CRUD functions by deleting a post. After deletion, the controller redirects back to my post index page. When the page loaded, the bootstrap side bar wrapped down below the other posts, rather than being on the right. Was fine before the post deletion.
I tried altering the cols and floating elements to no avail. Using Chrome dev tools, I see that the sidebar is nested in an em tag that isn't in the code. Tried clearing application cache. No effect.
I suspect this has something to do with the blade templating language and parsing the html, although if it is, I have no idea how to fix this other than manually installing the sidebar on every page.
How do I fix this??
Code can be found here: https://github.com/gkennedy87/Hillcrest/tree/master/resources/views
(It seems that laravel is also wrapping the footer in an em tag. Although this is a non-issue at the moment)
// views/posts/index.blade.php
#extends('layouts.app')
#section('content')
#include('inc.navbar')
<div class="container">
<div class="row">
<div class="col-md-8 col-lg-8">
<h1>Our Blog</h1>
#if (count($posts) > 0)
#foreach ($posts as $post)
<div class="card mb-4">
<img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
<div class="card-body">
<h2 class="card-title">{{$post->title}}</h2>
<p class="card-text">{!! str_limit($post->body,200,'...')!!}</p>
Read More →
</div>
<div class="card-footer text-muted">
Posted on {{$post->created_at}} by
Start Bootstrap
</div>
</div>
#endforeach
{{$posts->links()}}
#else
<p>No Posts Found</p>
#endif
</div>
#include('inc.sidebar')
<!--end row -->
</div>
<!--end container -->
</div>
#include('inc.footer')
#endsection
// views/inc/sidebar.blade.php
<!--sidebar start -->
<div class="col-md-4 col-lg-4">
<div class="sticky">
<!-- Search Widget -->
<div class="card my-4">
<h5 class="card-header">Search</h5>
<div class="card-body">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for...">
<span class="input-group-btn">
<button class="btn btn-secondary" type="button">Go!</button>
</span>
</div>
</div>
</div>
<!-- Categories Widget -->
<div class="card my-4">
<h5 class="card-header">Categories</h5>
<div class="card-body">
<div class="row">
<div class="col-lg-6">
<ul class="list-unstyled mb-0">
<li>
Web Design
</li>
<li>
HTML
</li>
<li>
Freebies
</li>
</ul>
</div>
<div class="col-lg-6">
<ul class="list-unstyled mb-0">
<li>
JavaScript
</li>
<li>
CSS
</li>
<li>
Tutorials
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- Side Widget -->
<div class="card my-4">
<h5 class="card-header">Side Widget</h5>
<div class="card-body">
You can put anything you want inside of these side widgets. They are easy to use, and feature the new Bootstrap 4 card containers!
</div>
</div>
</div>
</div>
<!-- sidebar end -->
I setup the app on a local environment, the sidebar always stays on the right even after deleting a post. The response also doesn't contain any em tags. Could you try clearing the cache:
php artisan cache:clear
php artisan view:clear
php artisan config:clear

Laravel 5.5 Method Links does not exist (Pagination) when applied to user model

I am trying to figure out why my pagination stops working as soon as I apply it to a user model. Here is the scenario: I am making a website which is a note takes, a user can take notes save them and all that. On a page /notes all notes made can be seen used for me to see weather each user is able to save notes. Pagination works there without a problem. Once I made a user Dashboard and I show notes they made with their user id I get this error:
Method links does not exist
…\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php
96
I have no clue why this is happening. When I delete the code from view: {{$notebooks->links()}} - the error goes away and so does pagination. The moment I put it in the error happens all over again.
Here is the code from my view and controllers.
DashboardController:
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
$notebooks = DB::table('notebooks', $user->notebooks)->paginate(4);
return view('dashboard')->with('notebooks', $user->notebooks);
}
}
Dashboard View (with paginate links code):
#extends('layouts.app')
#section('content')
<!-- Main component for call to action -->
<div class="container text-center">
<!-- heading -->
<h1 class="pull-xs-left">Your Dashboard</h1>
<br>
<div class="pull-xs-right">
<a class="btn btn-primary" href="/notebook/create" role="button"> New Note +</a>
</div>
<div class="pull-xs-right">
<a class="btn btn-primary" href="/contacts/create" role="button"> New Contact +</a>
</div>
<div class="pull-xs-right">
<!--the dropdown menu -->
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Sort Notebooks By
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="{{action('NotesController#ascendingId')}}">Note Number A - Z</a>
<a class="dropdown-item" href="{{action('NotesController#descendingId')}}">Note Number Z - A</a>
<a class="dropdown-item" href="{{action('NotesController#descendingTime')}}">Latest Time Posted</a>
<a class="dropdown-item" href="{{action('NotesController#ascendingTime')}}">Oldest Time Posted</a>
</div>
</div>
</div>
<div class="clearfix">
</div>
<br>
#if(count($notebooks) > 0)
#foreach ($notebooks as $notebook)
<div class="col-sm-3 col-md-3 col-sm-6">
<div class="card" style="width: 26rem;">
<div class="card-header" >
Client: {{$notebook->client_name}}
</div>
<div class="card-block" >
<h4 class="card-title " style="height: 5rem;"><strong><a href="/notebook/{{$notebook->id}}" >{{$notebook->name}}</a></strong></h4>
<p class="card-text" style="height: 7rem;">{{$notebook->note_description}}</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item"><b>Last Updated:</b> {{$notebook->updated_at->format('d-m-Y')}}</li>
<li class="list-group-item">
Edit Note
</li>
</ul>
<div class="card-block">
{!!Form::open(['action' => ['NotesController#destroy', $notebook->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
</div>
</div>
</div>
#endforeach
</div>
<!-- /container -->
<hr>
<div class="container text-center">
{{$notebooks->links()}}
</div>
</div>
#else
<div>
<div class="col-lg-12 no-notes">
<p>No notes found, go ahead and make your first note! :)</p>
<a class="btn btn-primary" href="/notebook/create" role="button"> Make My First Note</a>
</div>
</div>
#endif
#endsection
Just a reminder this works perfectly without the user model. I am lost as how to fit it. Using Laravel 5.5.26.
Change the code to:
$notebooks = auth()->user()->notebooks()->paginate(4);
return view('dashboard')->with('notebooks', $notebooks);
Also, you do not need to get a user from DB since you can access auth()->user() instance in a view.

Organise parent and child navigation into global layout

Let me just give you a brief introduction to the question, I'm going to be having about 20 pages on my website once a user logs in, I need to organise these pages to use the same header, so I've come to the decision to use a layout as I'm using Laravel framework, it seemed stupid to have the same header spread across the 20 pages, if I wanted to make a change it would be hell.
In Bootstrap, I didn't add any kind of active class to any of the selected pages, it was just a navigation bar, I've recently upgraded to the Bulma framework where it requires an active class on the parent tab to show the child tabs, sort of a parent and sub categorys system going on.
I'm not sure how to handle this in the Layout, I need to add an active class and I also need to choose which sub navigation to show.
I've added my code below, I was wondering if anyone could tell me how I can approach this?
The below code is my whole page, including the header (the <section class="hero is-danger inside-header">)
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
<title>{{ config('app.name') }} - Login</title>
<link rel="stylesheet" href="assets/public/1.0/frontend/css/bulma.css?id={{ time() }}" type="text/css">
<link rel="stylesheet" href="assets/public/1.0/frontend/css/override.css?id={{ time() }}" type="text/css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body class="inside">
<section class="hero is-danger inside-header">
<div class="hero-body">
<div class="container">
<div class="columns is-vcentered">
<div class="column is-5">
<p class="title header-title">Introducing {{ config('app.name') }}</p>
<p class="subtitle">Interacting with others...</p>
</div>
<div class="column is-3"></div>
<div class="column is-4">
<a class="button is-danger is-large is-disabled join-game-button">
<i class="fa fa-sign-in"></i> Join Game
</a>
<a class="button is-success is-large is-disabled users-online-button">
<i class="fa fa-users"></i> 10
</a>
<br><br>
<a class="button is-success is-large is-disabled platform-button">Platform: Closed Beta</a>
</div>
</div>
</div>
</div>
<div class="hero-foot">
<div class="container">
<nav class="tabs is-boxed">
<ul>
<li class="is-active">
<a href="/documentation/overview/start/">
<i class="fa fa-user"></i> {{ Auth::user()->username }}
</a>
</li>
<li>
<a href="http://bulma.io/documentation/modifiers/syntax">
<i class="fa fa-university"></i> Business
</a>
</li>
<li>
<a href="http://bulma.io/documentation/columns/basics">
<i class="fa fa-user-secret"></i> Gangs
</a>
</li>
<li>
<a href="http://bulma.io/documentation/elements/box/">
<i class="fa fa-users"></i> Community
</a>
</li>
<li>
<a href="http://bulma.io/documentation/components/breadcrumb/">
<i class="fa fa-shopping-cart"></i> Store
</a>
</li>
</ul>
</nav>
</div>
</div>
</section>
<nav class="navbar has-shadow">
<div class="container">
<div class="navbar-tabs">
<a class="navbar-item is-tab is-active" href="http://bulma.io/documentation/overview/start/">Home</a> <a class="navbar-item is-tab" href="http://bulma.io/documentation/overview/start/">Profile</a> <a class="navbar-item is-tab" href="http://bulma.io/documentation/overview/customize/">Education</a> <a class="navbar-item is-tab" href="http://bulma.io/documentation/overview/classes/">Skills</a> <a class="navbar-item is-tab" href="http://bulma.io/documentation/overview/modular/">Housing</a> <a class="navbar-item is-tab" href="http://bulma.io/documentation/overview/modular/">Security</a>
</div>
</div>
</nav>
<div class="container holorp-container-fixed">
<br>
<div class="columns is-desktop">
<div class="column is-8">
<div class="message is-danger">
<div class="message-body">
<p>not sure what can even go here...</p>
</div>
</div>
</div>
<div class="column is-4">
<div class="message is-success">
<p class="message-header">Change Log <span class="is-pulled-right"><i class="fa fa-user"></i></span></p>
<div class="message-body">
<span class="tag is-dark">26/09/17</span> <code>Updates the platform with a fresh design</code><br>
<span class="tag is-dark">26/09/17</span> <code>Upgraded from Laravel 5.4 to 5.5</code><br>
<span class="tag is-dark">26/09/17</span> <code>Added core features to the admin panel</code><br>
<span class="tag is-dark">26/09/17</span> <code>Did something, can't even remember</code><br>
<br>
<p><a class="modal-button" data-target="#modal-forgotPassword" id="forgot-pw-modal">View all recent changes</a></p>
</div>
</div>
</div>
</div>
</div>
</body>
If I understand well, you need "the same header" on every page. Laravel comes with Blade Template Engine, it has a set of methods to help us.
Using the template engine, you can create your app template in resources/views/layouts/app.blade.php
using #yield you can inform what part is dynamic:
<div class="container">
#yield('content')
</div>
To reuse the template in others views, for example using two pages:
The content of file resources/views/layouts/pages/one.blade.php
#extends('layouts.app')
#section('content')
<div class="row">
<h1>Page 1</h1>
</div>
#endsection
The content of file resources/views/layouts/pages/two.blade.php
#extends('layouts.app')
#section('content')
<div class="row">
<h1>Page 2</h1>
</div>
#endsection
Using #yield, #extends and #section you can reuse any HTML template in your Laravel Application.
We can create any #yield that we need:
<div class="container">
#yield('content')
</div>
#yield('scripts')
And reuse in our pages:
#extends('layouts.app')
#section('content')
<div class="row">
<h1>More than one #yield</h1>
</div>
#endsection
#section('scripts')
<script type="text/javascript" />
console.log('using Blade Template Engine');
</script>
#endsection
There is the #include method, that includes the content of another file if you need.
#extends('layouts.app')
#section('content')
<div class="row">
<h1>Page 2</h1>
#include('forms.form2')
</div>
#endsection
UPDATE:
Knowing these things above, we can do:
#extends('layouts.app')
#section('content')
<div class="row">
<h1>You are in Item B</h1>
<ul>
<li id="liA">Item A</li>
<li id="liB">Item B</li>
<li id="liC">Item C</li>
</ul>
</div>
#endsection
#section('scripts')
<script type="text/javascript" />
var d = document.getElementById("liB");
d.className += " is-active";
</script>
#endsection
This will add the CSS class in the element with id "liB", we need to use something to identify the element, I'm using "id" property but you can use anyone, just need to check what JS method to use to get it in "d" variable. You can check this for more information Element.className
I don't use Bulma, but I believe that will do what you want.

laravel 4 - how to show and hide components in blade?

I have breadcrumbs in my master.blade.php file and i want the breadcrumbs to be used everywhere BUT the homepage.blade.php.
right now this is how i add links to the breadcrumbs in other pages like "About".
about.blade.php:
#section('breadcrumbs')
#parent
<li class="last-breadcrumb">
About
</li>
#stop
in the master.blade.php:
<div class="container">
<div class="breadcrumb-container">
<ul class="breadcrumb">
#section('breadcrumbs')
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
#show
</ul>
</div>
</div>
but i don't want the breadcrumbs code to display at all when homepage.blade been used.
copying the code for each about.blade/X.blade files seems like a bad idea..
Your can set a value in your controller that you pass with the make/redirect like $data['breadcrumb'] = true and wrap your breadcrumb code in an conditional. This kind of system also works well for error and success messages since you can alse send content from your controller. Then you would send an array with values instead of true.
Blade template
<div class="container">
#if($breadcrumb)
<div class="breadcrumb-container">
<ul class="breadcrumb">
#section('breadcrumbs')
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
#show
</ul>
</div>
#endif
</div>
You can check the URI to see if you want to display it or not. It's logic in your view which is usually frowned upon, but it's the easiest way I can come up with.
<div class="container">
<div class="breadcrumb-container">
<ul class="breadcrumb">
#if(Route::uri() == '/')
<li class="last-breadcrumb">
About
</li>
#endif
<li>
<a href="/homepage/" title="homepage">
homepage
</a>
</li>
#show
</ul>
</div>
</div>
Depending on the URI you are using for your home page, you may need to tweak the condition.
If you use #yield in your master template then it will only display content when the section has been defined. #content requires the section to be always defined.

Strange Pagination issues adding /index/ between links - Expression Engine

I seem to be having an issue with the pagination on the news section appending an /index/ when using the {pagination_links} tag.
My news page has a template path of news/index and the posts template is news/post.
I am using structure with the news/post as a listing attached to the news/index page, to add|edit posts.
If you have a look at the website in question: http://www.wilbyltd.co.uk/news scroll to the bottom you will see the pagination, if you click either 1|2|3 or next page or last page you will get the page requested, the url looks like /news/P6, however now on this next page if you go to the pagination again and click any of them you will notice it has gone back to page 1 and the url has /news/index it seems to be appending or inserting between links /index/.
I have tried to the paginate_base="" in the channel entry but adding a base stops the categories from having a working pagination, categories also add /index/ between the links?
I have thought of hacking the core but seems the wrong approach just in case it ever gets updated.
I have tried .htaccess to remove the index, which didn't work.
RewriteRule ^/news/index/(.+)$ /news/$1 [L]
I have looked at the config for index.php which has been taken off and .htaccess has been used.
I have looked at the channel settings.
I have tried dynamic="off"|dynamic="on"
I understand that news/index it the correct path for the page to view, but if this is the case why doesn't it pick up the pagination?
If anyone could shed some light in to this I would be really grateful, here is the code containing the pagination.
{exp:channel:entries channel="posts" limit="6" dynamic="on" paginate="bottom" orderby="entry_date" sort="desc"}
<div class="news-snippet span9">
<a href="{url_title_path=">
<div class="date-published textalign-center">
<span class="day">{entry_date format="%d"}</span><span class="day-suffix">{entry_date format="%S"}</span>
<span class="month">{entry_date format="%F"}</span>
</div>
</a>
<div class="news-snippet-body pull-right">
<div class="news-snippet-top-shadow">
<div class="news-snippet-bottom-shadow">
<a href="{url_title_path=">
<div class="news-snippet-content clearfix">
<div class="title">
<h3>{title}</h3>
</div>
{if news_feature_image}
<div class="clearfix image">
<img src="{news_feature_image}" />
</div>
{/if}
<p>{news_short_description}</p>
</div><!-- end content -->
</a>
<div class="news-snippet-options clearfix">
<div class="news-tags pull-left">
<i class="icon-tags"></i>
{exp:tagger:tags entry_id="{entry_id}" }
<span class="label label-inverse tags">{tagger:tag_name}</span>
{/exp:tagger:tags}
</div>
<div class="social-share pull-right textalign-center">
<i class="icon-random"></i>
<a class="addthis_button"url="{url_title_path="title="{title}" href="http://www.addthis.com/bookmark.php?v=300&pubid=ra-5141a60a37fa6e4e">Share</a>
</div>
</div>
</div><!-- end bottom-shadow -->
</div><!-- end top-shadow -->
</div><!-- end snippet-body -->
</div><!-- end news-snippet -->
{paginate}
<div class="clearfix paginate">
{pagination_links}
<div class="total-pages pull-left">
<p>Page {current_page} of {total_pages} pages</p>
</div>
<div class="pagination pagination-mini pull-right">
<ul>
{first_page}
<li>First Page</li>
{/first_page}
{previous_page}
<li>Previous Page</li>
{/previous_page}
{page}
<li>{pagination_page_number}</li>
{/page}
{next_page}
<li>Next Page</li>
{/next_page}
{last_page}
<li>Last Page</li>
{/last_page}
</ul>
</div>
{/pagination_links}
</div><!-- end clearfix -->
{/paginate}
{/exp:channel:entries}
I have also had this on Ellis Labs forum for a couple of weeks: http://ellislab.com/forums/viewthread/237601/
Try to override the {paginate_base} parameter in your channel entries tag
{exp:channel:entries channel="posts" paginate_base="news/" ...}
http://ellislab.com/expressionengine/user-guide/modules/channel/channel_entries.html#paginate-base

Categories