how to get array inside for each loop in laravel - 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

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...

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.

Undefined property: Illuminate\Database\MySqlConnection::$quizUser

I run the laravel code for fetching the data from database but i got the error.
This is controller's code name is UserProfileController.blade.php
public function FetchUserQus()
{
$data = DB::table('userquestion')->where('userEmail', '=', '{{
Auth::user()->email }}');
return view('designpages/userqus', ['data' => $data]);
}
Route::get('designpages/userqus',
'UserProfileController#FetchUserQus')->name('designpages/userqus');
This is view page code saved with name designpages/userqus.blade.php
#foreach($data as $datas)
<p><b>Question: {!! $datas->quizUser !!}</b></p>
<p><b>Answer: </b>{!! $datas->ansAdmin !!}</p>
#endforeach
You cannot use blade syntax in the controller: So change this:
$data = DB::table('userquestion')->where('userEmail', '=', '{{ Auth::user()->email }}');
to this:
$data = DB::table('userquestion')->where('userEmail', '=', auth()->user()->email)->get();
And also I use get() to return a collection, without it it returns a Illuminate\Database\Query\Builder instance.

Laravel Blade loop through config array variable

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

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