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

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.

Related

Blade: Invalid argument supplied for foreach()

sometimes $structures array has the null value the same code for other variable is working fine but not in this case.
#foreach($structures ?? [] as $item)
{{ $item }}
#endforeach
I resolved the issue by adding () around $structures ?? [] expression.
#foreach(($structures ?? []) as $item)
{{ $item }}
#endforeach
Use forelse instead of foreach
#forelse ($structures as $item)
{{ $item }}
#empty
No Items found.
#endforelse
The forelse check the condition as below.
#if ($structures->count())
#foreach ($structures as $item)
{{ $item }}
#endforeach
#else
No Items found.
#endif

How to Count two array in foreach loop

Hi everybody here I have 2 paths so I return 2 arrays of tasks I need to count each array So in last I get
[6, 4]
#foreach ($path->pathtags as $Tag)
#foreach ($Tag->Tasks as $Task)
#if (!in_array($Task->id,$a))
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endif
#endforeach
#endforeach
Easiest php way of counting arrays is count($array) so if you want to make a new array with the 2 array counts you can do it like this:
$counts = [count($array1), count($array2)];
But if you need to count the records in the database, you will need to change the query instead of ->get() you will need to use ->count().
You should try this:
$path = count($path->pathtags);
$task = count($Tag->Tasks);
Updated answer
$path = count($path->pathtags);
#foreach ($path->pathtags as $Tag)
$task = count($Tag->Tasks);
#foreach ($Tag->Tasks as $Task)
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endforeach
#endforeach

how to add index in laravel blade html attribute while calling collection

I am calling categories collection from controller and displaying in the blade in foreach loops
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="1">{{ $subcategory->name }}</a>
#endforeach
#endforeach
I need to add index numbers generated in the loop from 1 in data-column value
data-column="1"
data-column="2"
data-column="3"
so on....in
For doing this you need to make a variable for counting after that you should pass that variable to view like below.
I am inside a method
public function getSingle($slug){
$category= Post::where('slug','=',$slug)->first();
if ($post != null) {
$counter = 0;
return view('blog.single')->withCategories($category)->withCounter($counter);
} else {
return view('error.error404');
}
}
After that you should access that Counter variable in view like below
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="{{$counter++}}">{{ $subcategory->name }}</a>
#endforeach
#endforeach
The classic for will do
#foreach ($categories as $category)
#for ($i = 0; $i < count($category->subcategories); $i++)
<a class="a.toggle-vis" data-column="{{$i}}">{{ $category->subcategories[$i]->name }}</a>
#endfor
#endforeach
For a shorter one, I think, we can do something like this. A little bit dirty in view but in the small snippet, it should be okay.
{{ !($index = 1) }}
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="{{ $index++ }}">{{ $subcategory->name }}</a>
#endforeach
#endforeach
Use this simple solution (Laravel method):
$loop->iteration The current loop iteration (starts at 1).
It will automatically increment, in every loop iteration.
Check docs:
The Loop Variable

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)

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.

Categories