PHP or Blade - Organized Columns depending on the number of data - php

I'm blocked for organize my sub-menu of my menu. As you can see,
it is decomposed into several blocks (i've use paint for delimited the block in black).
currently my code is:
<li class="dropdown yamm-fw">
{{ $category->nom }} <b class="caret"></b>
<ul class="dropdown-menu">
<li>
<div class="yamm-content">
<div class="row">
<div class="col-sm-3">
<h5>test</h5>
<ul>
<?php $count=0 ?>
#foreach ($types->whereIn('id', $category->produit->unique('type_id')->pluck('type_id')) as $type)
#if ($count <= 6)
<li>{{$type->nom}}</li>
<?php $count++ ?>
#endif
#endforeach
</ul>
</div>
<div class="col-sm-3">
<h5>teste 2</h5>
<ul>
<li>Trainers</li>
<li>Sandals</li>
<li>Hiking shoes</li>
<li>Casual</li>
</ul>
</div>
I do not know how to do that after 7 items in the actual div col-sm-3, create another div to continue the foreach and so on.

You can use the $loop variable:
#if ($loop->iteration === 7)
// Create another div here
#endif

Related

Laravel: Dynamic tabs don't show the correct data?

I am trying to use dynamic tabs in laravel and iam kinda new to it. Iam stuck in it for like 2 days.
I have two variables that getting data from tables in mysql.
$stations = Station::select('id', 'name')->get();
// return $staions;
$queues = Queue::withTrashed()
->oldest()
->get();
// return $queues;
The data in $schedules:
[
{
"id":1,
"name":"siinay"
},
{
"id":2,
"name":"jigjiga"
},
{
"id":3,
"name":"Idaacada"
},
{
"id":4,
"name":"Xero Awr"
}
]
And the data in the $queues:
So the problem is here in dynmaic tabs: I have wanted to display station names in the nav-tabs and queues in the tab-content. So i did it like this.
<ul class="nav nav-tabs">
#forelse ($stations as $station)
<li >
<a data-toggle="tab" href="#tab-{{ $station->id }}" >
{!! $station->name !!}
</a>
</li>
#empty
#endforelse
</ul>
<div style="margin-top: 15%" class="tab-content">
#foreach($queues as $queue)
#if($queue)
<div id="tab-{{ $queue->station_id}}" class="tab-pane fade">
<p>{!! $queue->bus_number !!}</p>
</div>
#else
<div>empty</div>
#endif
#endforeach
</div>
when i run the above code, it only showing the last queue bus_number per each sttions. every stations queue
Oldest is indeed the oldest, it's the opposite to first. Try this to get a collection:
$queues = Queue::withTrashed()
->orderBy('id', 'desc')
->get();
In the view (simplified):
<ul class="nav nav-tabs">
#foreach ($stations as $station)
<li>
<a data-toggle="tab" href="#tab-{{ $station->id }}">
{{ $station->name }}
</a>
</li>
#endforeach
</ul>
<div style="margin-top: 15%" class="tab-content">
#foreach($queues as $queue)
<div id="tab-{{ $queue->station_id }}" class="tab-pane fade">
<p>{{ $queue->bus_number }}</p>
</div>
#endforeach
</div>
No need to check for empty. Each one is a table record. Unless you want to check on bus_number or station_id.

Trying to get property of non-object in aside

I´m trying to do a simple blog with Laravel 5.3. Index page include aside with categories and tags, and we can filter different post with categories or tags names.
In routes I have this.
Route::get('categories/{name}',[
'as'=>'front.search.category',
'uses'=>'FrontController#searchCategory'
]);
Route::get('tags/{name}',[
'as'=>'front.search.tag',
'uses'=>'FrontController#searchTag'
]);
In Article
public function scopeSearchCategory($query,$name){
$query->where('name','=',$name);
}
In Tag
public function scopeSearchTag($query,$name){
$query->where('name','=',$name);
}
In FrontController.
public function searchCategory($name){
$tags=Tag::all();
$sliders=Slider::all();
$categories = Category::SearchCategory($name)->first();
$articles = $categories->articles()->paginate(5);
$articles->each(function($articles){
$articles->category;
$articles->images;
});
return view('front.index')->with('articles', $articles)->with('sliders', $sliders)->with('categories', $categories)->with('tags',$tags);
}
public function searchTag($name){
$categories=Category::all();
$sliders=Slider::all();
$tags = Tag::SearchTag($name)->first();
$articles = $tags->articles()->paginate(5);
$articles->each(function($articles){
$articles->category;
$articles->images;
});
return view('front.index')->with('articles', $articles)->with('sliders', $sliders)->with('categories', $categories)->with('tags',$tags);
}
In aside
<aside class="panel panel-primary">
<div class="panel-heading">
<h2 class="panel-title">{{trans('app.title_categories')}}</h2>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($categories as $category)
<a href="{{route('front.search.category',$category->name)}}">
<li class="list-group-item">
<span class="badge">{{$category->articles->count()}}</span>
{{$category->name}}
</li>
</a>
#endforeach
</ul>
</div>
</aside>
<aside class="panel panel-success">
<div class="panel-heading">
<h2 class="panel-title">Tags</h2>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($tags as $tag)
<a href="{{route('front.search.tag',$tag->name)}}">
<li class="list-group-item">
<span class="badge">{{$tag->articles->count()}}</span>
{{$tag->name}}
</li>
</a>
#endforeach
</ul>
</div>
</aside>
When a click on aside href to filter tags or categories return 'Trying to get property of non-object'. If I remove scopeSearchs and put in FrontController functions $Tag:all() or Category:all() href works, but filter fail, return all post. ¿Any idea why return this error? ¿Can´t do foreach if return one object?
You should use
$tags = Tag::searchTag($name)->first();
When you are using scope function it should always be camelCase
so searchTag() should be start with small s not capital s
You should remove foreach
use this
<a href="{{route('front.search.tag',$tags->name)}}">
<li class="list-group-item">
<span class="badge">{{$tags->articles->count()}}</span>
{{$tags->name}}
</li>
</a>

fetching data in while loop in list tag

while($recor1=mysql_fetch_assoc($res1))
{
?>
<ul>
<li class="col-md-3 col-sm-3"><div class="dest-list"><?php echo $recor1['sub_destination']; ?></div> </li>
</ul>
<div class="clearfix"></div>
<?php
}
it displays the result horizontally i want it vertically please give me soluation
there are a number of things wrong with your code, why are you adding a new list for every item with only one list item, that makes no sense. Also do not add block elements inside inline elements (div inside a). Also if you what the list items under each other then why add the col-... class?
<ul>
<?php while($recor1=mysql_fetch_assoc($res1)) {?>
<li class="">
<a href="#">
<?php echo $recor1['sub_destination']; ?>
</a>
</li>
<?php } ?>
</ul>
this gives you the right html structure, and you can then style it as you see fit
if you dont want to display a list horizontally remove col-sm-3 class of your li, which changes your code like below
while($recor1=mysql_fetch_assoc($res1)) { ?>
<div class="col-md-3 col-sm-3">
<ul>
<li >
<a href="#">
<div class="dest-list">
<?php echo $recor1['sub_destination']; ?>
</div>
</a>
</li>
</ul>
</div>
<div class="clearfix"></div>
<?php
}
take a look at this example

Laravel/PHP - loop through items in a view

i have the following html / blade template:
{{--show all projects--}}
#if(!empty($projects))
#foreach($projects as $project)
<div class="slide">
<ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
<li>
<div class="thumb">
<h3 class="thumbTitle">{{$project->title}}</h3>
{{--laat maar 1 image zien --}}
#if(!empty($project->project_images))
{{ HTML::image('useruploadedimages/'.$project->project_images[0]->url) }}
#endif
</div>
</li>
</div>
#endforeach
#endif
</div>
what i want is the following: 8 projects displayed within a div with the .slide class, multiple divs with the .slide class for ervery 8 projects: for example i have 25 projects: than i want to have 4 .slide divs 3 with 8 projects and 1 with a single project...
is this even possible?
You may use array_chunk like this:
#foreach(array_chunck($projects->toArray(), 8) as $projects)
<div class="slide">
<ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
#foreach($projects as $project)
<li>
<div class="thumb">
<h3 class="thumbTitle">{{$project['title']}}</h3>
#if(!empty($project['project_images']))
{{HTML::image('....)}}
#endif
</div>
</li>
#endforeach
</ul>
</div>
#endforeach
Notice the array notation [] instead of object notation ->.
You can try this peace of code
{{--show all projects--}}
#if(!empty($projects))
$i = 8;
#foreach($projects as $project)
#if($i%8 == 0) // checks for multiples if 8 and wraps your content with div and ul
<div class="slide">
<ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
#endif
<li>
<div class="thumb">
<h3 class="thumbTitle">{{$project->title}}</h3>
{{--laat maar 1 image zien --}}
#if(!empty($project->project_images))
{{ HTML::image('useruploadedimages/'.$project->project_images[0]->url) }}
#endif
</div>
</li>
#if($i++%8 == 0) // checks for multiples if 8 and increments i value
</ul> // ends the ul and div
</div>
#endif
#endforeach
#endif
</div>
hope it helps
You can close and open a new .slide div for every 8 projects. I supose the ul has to restart in every div as well?
#if(!empty($projects))
<div class="slide">
<ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
#for($i = 0; $i < count($projects); $i++)
<?php $project = $projects[$i]; ?>
<li>
<div class="thumb">
<h3 class="thumbTitle">{{$project->title}}</h3>
{{-- only show one image --}}
#if(!empty($project->project_images))
{ HTML::image('useruploadedimages/'.$project->project_images[0]->url) }}
#endif
</div>
</li>
{{-- If there were 8 projects, start a new .slide --}}
#if ($i % 8 == 7)
</ul></div><div class="slide"><ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
#endif
#endfor
</ul>
</div>
#endif

Laravel and nested foreach of database query error

I am building a menu on laravel.
In blade layout i have:
#foreach ($menu1 as $menu1)
{{$menu1->nome}}
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
<span class="glyphicon glyphicon-chevron-down"></span>
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<ul class="list-unstyled">
#foreach ($menu2 as $menu2)
<li> {{$menu2->nome}} </li>
#endforeach
</ul>
</div>
</div>
</div>
#endforeach
in my controller i pass 2 query.
public function menu($lingua)
{
return View::make('index', ['menu1'=>DB::table('cat_nome')->join('lingua', 'cat_nome.id_lingua', '=', 'lingua.id') ->where('lingua.lingua','=',$lingua)->get(array('cat_nome.nome')),
'menu2' => DB::table('campo_nome')->join('lingua', 'campo_nome.id_lingua', '=', 'lingua.id') ->where('lingua.lingua','=',$lingua)->get(array('campo_nome.nome'))]
);
}
I tried to UNnest the foreach and everything works fine. When i try to nest the foreach i get
Trying to get property of non-object
#foreach ($menu2 as $menu2)
That line is most likely the culprit. You cannot use the same variable name twice in a foreach loop like that and expect good results. The second one is supposed to be what each entry in $menu2 will be scoped to during each iteration of the loop.
See the foreach documentation
Try changing the loop to look more like this:
#foreach ($menu2 as $innerMenu)
<li> {{$innerMenu->nome}} </li>
#endforeach
I would suggest doing the same thing for your outer menu as well.

Categories