How to implement chunks - php

I am not very experienced and I run into problem. I can't figure out how to make this function use chunks. Tried all but get errors, I think I am misunderstanding something.
$companies_has = Company::whereHas('websites', function ($q) {
$q->where( 'updated_at', '<=', Carbon::now()->subMonth());
})->get();
$companies_doesnt_has = Company::whereDoesntHave('websites')->get();
$companies = $companies_has->merge($companies_doesnt_has);
foreach ($companies as $company) {
$this->getWebsite($company); }

#foreach ($companies->chunk(3) as $chunk)
<div class="row">
#foreach ($chunk as $product)
<div class="col-xs-4">{{ $product->name }}</div>
#endforeach
</div>
#endforeach

Related

Hiding results titles if there were no results

I have the following code that shows the output of a search for users, groups and posts :
<header><h1>Users results</h1></header>
#foreach ($users as $user)
#include('user.userblock')
#endforeach
<header><h1>Groups results</h1></header>
#foreach ($groups as $group)
#include('group.gruopblock')
#endforeach
<header><h1>Posts results</h1></header>
#foreach ($posts as $post)
#include('post.postblock')
#endforeach
How can I hide the <header> if one of them doesn't have results ?
Using eloquent #if and #endif with it s count() function:
#if(count($users))
<header><h1>Users results</h1></header>
#foreach ($users as $user)
#include('user.userblock')
#endforeach
#endif
#if(count($groups))
<header><h1>Groups results</h1></header>
#foreach ($groups as $group)
#include('group.gruopblock')
#endforeach
#endif
#if(count($posts))
<header><h1>Posts results</h1></header>
#foreach ($posts as $post)
#include('post.postblock')
#endforeach
#endif
#if (count($users))
<header><h1>Users results</h1></header>
#foreach ($users as $user)
#include('user.userblock')
#endforeach
#endif
and so on...
You've got several choices on how to do it thanks to the nature of the Laravel collection. But an if check conditional on either side will work fine:
if (empty($users)) { // Your header and foreach}
if ($users->first()) { // Your header and foreach}
if (!$users->isEmpty()) { // Your header and foreach}
if ($users->count()) { // Your header and foreach}

Blade: Undefined Variable Inside Foreach Loop

I have a controller which gets an array of a User's diaries from my database and passes them to my view:
<?php
public function readDiaries($hash)
{
$user = User::where('hash', $hash)->first();
$diaries = Diary::where('user_id', $user->id)->get();
return view('app.diary.readDiaries', ['diaries' => $diaries]);
}
In my view, I am looping through the diaries using a #foreach loop.
<div id="diaries" class="card-columns">
#if (count($diaries) > 0)
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
#endif
</div>
But I am getting the following undefined variable error...
Undefined variable: diary (View: C:\xampp\htdocs\personal_projects\Active\diary_app\resources\views\app\diary\readDiaries.blade.php)
Why is my $diary variable undefined inside the #foreach loop?
You have a typo
change #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)
and it should work!
There is some typo in var_dump use {{ var_dump($dairy) }}
Try to use compact method for pass data to view as shown below
//return view('app.diary.readDiaries', compact('diaries'));
public function readDiaries($hash)
{
$user = User::where('hash', $hash)
->first();
$diaries = Diary::where('user_id', $user->id)
->get();
return view('app.diary.readDiaries', compact('diaries'));
}
It is just a simple spelling mistake,
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
in your foreach you are passing $dairy and dumping var name is $diary
chane it to
#foreach ($diaries as $dairy)
{{ var_dump($dairy) }}
#endforeach
Habbit of copy paste is sometimes good for us...
:)
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
you used ($diaries as $dairy) in foreach
but in foreach you used ($diary)
you want edit this {{ var_dump($diary) }} to {{ var_dump($dairy) }}
or you want edit #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)

How can I define variable array on the laravel blade view?

I try like this :
<div class="media-body">
#foreach($categories as $category)
#php $category[] = $category->name #endphp
#endforeach
{{ implode(",", $category) }}
</div>
If the code executed, there exist error :
undefine variable category
How can I solve it?
You can simply use Laravel Collection
{{ $categories->pluck('name')->implode(', ') }}
Or if you wanna do this in foreach then
#php ($names = [])
#foreach ($categories as $category)
#php ($names[] = $category->name)
#endforeach
{{ implode(', ', $names) }}
You have to declare an array within <?php ... ?> block and then use the same in a {{blade}} block.

Laravel 5.3 : Error while fetch data in view using foreach

i got error while i'm trying to fetch my data in view. This is my code :
controller
public function create()
{
$categories = Category::all();
$tag = Tag::groupBy('name')->pluck('name');
$tags = json_encode($tag);
return view('backend.articles.create', compact('categories', 'tags'));
}
view
fetch data categories
<div class="panel-body">
#foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
#endforeach
</div>
fetch data tags
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
#if (count(old('tags')))
value: [
#foreach (old('tags') as $tag)
'{{$tag}}',
#endforeach
]
#endif
});
});
I dont know why i got this error message : Invalid argument supplied for foreach(). Could anybody to help me to fix this problem ?
view
<div class="panel-body">
#if($categories)
#foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
#endforeach
#endif
script
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
#if (is_array(old('tags')))
value: [
#foreach (old('tags') as $tag)
'{{$tag}}',
#endforeach
]
#endif
});
});
Try to use is_array to make sure old('tags') returning array, I believe the problem because the old('tags') is not returning array.

Laravel pagination Links not working on queries

I have this search engine with some advanced search options but I get errors when I use advance options.
When I go to the next pages I see errors.
Here is my code:
Controller:
$Input = Input::all();
$makethis = Input::flash();
$items = Gamefarm::where('roost_hen', '=', Input::get('sex'))
->paginate(6);
return View::make('gamefarms/index', compact('items', 'makethis'));
For my views
#foreach(array_chunk($items->all(), 3) as $row)
<div class="row">
#foreach ($row as $item)
<div class="col-md-4">
<h2>{{ $item->bname}}</h2>
<img src="{{$item->img_loc}}">
<div>{{$item->desc}}</div>
</div>
#endforeach
</div>
#endforeach
{{ $items->appends(Request::except('page'))->links() }}
return View::make('gamefarms/index',compact('items','makethis'))->with('items',$items);

Categories