Laravel Blade loop through config array variable - laravel

In my config/locale.php I have an array 'displayLanguage' which contain key=>value pairs
How can I loop through this array in blade ?
I have tried the following
#foreach ( {{ Config::get('app.locale.displayLanguage') }} as $itemKey => $itemVal)
{{ $itemKey }}
#endforeach
I am getting syntax error, unexpected '<'. tried also some other veriation to loop this var without passing it through the controller

If your file is in config/locale.php then you call config('locale.displayLanguage');
#foreach(config('locale.displayLanguage') as $key => $value)
{{ $key }}
#endforeach
I am using the global helper config() in a blade file.
It also appears you have extra curly braces in your foreach loop

Related

foreach() argument must be of type array | object, null given (Laravel Livewire)

This code is working fine
public function render(){
$this->products = ProductModel::get();
return view('livewire.product');
}
But when I am trying to paginate using laravel livewire, it gives me an error
public function render(){
return view('livewire.product', [
'products' => ProductModel::paginate(10)
]);
}
Blade File
#foreach ($products as $product)
{{ $product->name }}
{{ $product->price }}
#endforeach
#if(!empty($products))
{{ $products->links() }}
#endif
import this in Component
use Livewire\WithPagination;
class Product extends Component
{
use WithPagination;
....
}
and add in view
#if(!empty($products))
{{ $products->links() }}
#endif
Ohh I got it.Actually I have already use $products variable as a global variable
and when I change $products to other name it works.
Thanks alot...

how to get array inside for each loop in laravel

In my blade component I am entering this:
{{ $post['tags']->pluck('slug') }}
but getting the following result:
["abc","pqr"]
$post['tags']->slug is giving an error
You need to use foreach :
#foreach($post['tags']->pluck('slug') as $key => $slug)
{{ $slug }}
#endforeach

i am trying to show single data but it is showing" Trying to get property 'id' of non-object"

**after clicking "read more" i want to show a single post**
<a href="{{ URL::to('single/blog/'.$post->id) }}" class="btn btn-primary
float-right">Read More →</a>
** **the route is****
Route::get('single/blog/{id}','Web\Site\HomeController#show');
**the controller is**
public function show($id)
{
$posts = Post::findOrFail($id);
return view('site.home.singleblog',compact('posts'));
}
****the single section is****
#foreach($posts as $post)
<img class="img-responsive" src="{{asset("uploads/posts/$post->id/image/$post->image") }}" alt=""
{{ $post->name }}
{{$post->description}}
#endforeach
Your variable $posts is a single Post instance from Post::findOrFail($id) (where $id is coming from a route parameter, so a single value). You don't want to be iterating an instance of a Model. Use it in your view like a single model instance not a collection.
public function show($id)
{
view('site.home.singleblog', [
'post' => Post::findOrFail($id),
]);
}
Then in the view just remove the #foreach and #endforeach.
Try this:
{{ $post['name'] }} {{$post['description']}}
If you fetched $posts successfully, it should work. The error says $post is not an object but you are using object syntax to get value by key. So use array syntax.

How access to $loop variable in nested foreach blade

#foreach($last_articles->chunk(3) as $row)
#foreach($row as $last_article)
#if($loop->index!=0) {{"in $loop"}} #endif
#endforeach
#if($loop->index!=0) {{"out $loop"}} #endif
#endforeach
how to access the $loop of any of foreach, that was used in the code?
You could use $loop->parent to access the parent loop of the current loop.
In my case (Laravel 6.8) $loop->parent looks like this
[
{"iteration":1,"index":0,"remaining":1,"count":2,"first":true,"last":false,"odd":true,"even":false,"depth":1,"parent":null},
{"iteration":2,"index":1,"remaining":0,"count":2,"first":false,"last":true,"odd":false,"even":true,"depth":1,"parent":null}
]
to access the parent index $loop->parent->index

laravel 5.2 entrust - show roles foreach user

My controller is
public function index()
{
$users = User::paginate(15);
return view('dash.users.index')->with(array('users' => $users));
}
in view i pass: {{ $item->roles }} and return all column as array, if i put {{ $item->roles->get('name') }}the table is blank, why?
I want to show user->roles->name foreach user
If it's an array, you should only simply use this:
{{ $item->roles['name'] }}
solved with nested foreach! #foreach($item->roles as $item) {{ $item['name'] }} #endforeach
inside my #foreach user
Thank you Mojtaba for the help!

Resources