Shawdow's avatar

how to acess the data without using foreach loop in blade file

i don't know whether i can access the data without for each loop of blade file using laravel but still i need this for not repeating the values in the blade file because i am trying to add the filters heading properties like Color,Weight etc..

public function addProducts() {

$attributename = DB::table('attributes')->select('attribute_1',
               'attribute_2','attribute_3','attribute_4','category_id')->get();

return view('showfilter',compact('attributename'));

}

in the blade file

@foreach($attributename as $attributenames)

{{ $attributenames->attribute_1 }}

{{ $attributenames->attribute_2 }}

@endforeach

0 likes
5 replies
kishanbhatt's avatar
Level 2

First of all your query will return multiple rows in $attributename. So you have to use foreach loop for that in the blade.

You must use ->first() to get only single record using query. or you can pass where condition in it.

Then you can be able to use $attributename->attribute_1, $attributename->attribute_2 likewise. without foreach loop

Hope it will help

1 like
babonday's avatar

So is the answer, you do always use a foreach on the view? . And use the backend to spit out what you want.

I just wanted the first item on a list on 1 part of the page and foreach the rest of the list on another part of the page. Seems alot of work to have to create a new query to just get the first item

Shaden's avatar

$attributename[0]

Would get the first Item in the list; to loop through the rest you unset $attributename[0]; leaving you with the rest of the list for loop.

Please or to participate in this conversation.