transform collection into a list in blade - php

anyone know how i transform this into a list in blade
[{"applicant": "Juan"}, {"applicant": "Pedro"}]
something like:
-Juan
-Pedro
currently that collection is an item of an array
{{$ applicant [$ i]}} contains [{"applicant": "Juan"}, {"applicant": "Pedro"}]
blade file
<td>
{{$applicant[$i]}}
</td>
Controller
for ($i=0; $i <= 59; $i++) {
$materias[$i]->name;
$applicant[$i] = DB::table('projections')
->where('s1',$materias[$i]->name)
->orWhere('s2',$materias[$i]->name)
->orWhere('s3',$materias[$i]->name)
->orWhere('s4',$materias[$i]->name)
->orWhere('s5',$materias[$i]->name)
->orWhere('s6',$materias[$i]->name)
->orWhere('s7',$materias[$i]->name)
->orWhere('s8',$materias[$i]->name)
->select('applicant')->get();
}
Result
image whit the result of the view

In your Blade you need to make something like that :
Check this documentation about loop in blade
<td>
<ul>
#foreach($applicant[$i] as $appli)
<li> {{ $appli }} </li>
#endforeach
</ul>
</td>

Related

Object of class Illuminate\Database\Eloquent\Collection could not be converted to int Laravel 5.4

I have an old database filled with info and now i want to display Category names from that DB and getting this error.
Here is my controller
public function forums(){
$cats = Forum_cats::all();
return view ('lapas.forums.index')->with('cats', $cats);
}
}
here is my view
#if(count($cats >1))
#foreach($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#endforeach
#else
#endif
and here is screen of DB structure
http://prntscr.com/mg5nk1
Ask for more info if needed!
It looks like your operator is misplaced:
#if(count($cats) > 1)
#foreach($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#endforeach
#else
#endif
It looks like you're trying to loop through these $cats in a Blade template. You might also try forelse:
#forelse($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#empty
{{-- Action if there are none --}}
#endforelse
Edit: Docs here: https://laravel.com/docs/5.4/blade#loops
Your if is wrong. Try :
#if($cats->count() > 1)

Laravel for loop an indexed array from a single column in my database inside a forelse in blade template

I had an for loop inside that will display an indexed array from the results column that saves an array in a DB, inside a forelse loop.
This is how I store the array
$final_result = ResultRecordFile::create([
'request_id' => $request->id,
'date_released' => $date,
'lab_access_code' => $input['lab_access_code'],
'remarks' => $input['remarks'],
'results' => json_encode($input['results']) // Stores array in the column
]);
Now I will retrieve all these data in my view using blade.
The array from the db is exactly like this when I use json_decode for the results field
[
"Result1",
"Result2",
]
This is what I have done so far.
#forelse ($result->request->methodology->submethods as $submethod)
<ul>
<li>
<b>{{ $submethod->name }}</b> result is
#foreach(json_decode($result->results) as $value)
{{ $value }}
#endforeach
</li>
</ul>
#empty
<p>This request has no submethods</p>
#endforelse
But it returns me an output of this in the view.
Test result for this sub method result is Result1 Result2
Test result for this sub method result is Result1 Result2
I have also tried this code below:
#forelse ($result->request->methodology->submethods as $submethod)
<ul>
<li>
<b>{{ $submethod->name }}</b> result is
#for($i=0; $i < count($result->results); $i++)
{{ $result->results[$i]}}
#endfor
</li>
</ul>
#empty
<p>This request has no submethods</p>
#endforelse
But now it returns me this in my view
Test result for this sub method result is [
Test result for this sub method result is [
The output is supposed to be like these:
Test result for this sub method result is Result1
Test result for this sub method result is Result2
The problem here is that it returns me the whole value of the array that it should return each value of the array stored in the database.
Appreciate if someone could help.
Thanks in advance.
This might work for you
$result_datas = json_decode($result->results);
$index = 0;
#forelse ($result->request->methodology->submethods as $submethod)
<ul>
<li>
<b>{{ $submethod->name }}</b> result is
#if(isset($result_datas[$index]))
{{ $result_datas[$index] }}
#endif
<?php $index++;?>
</li>
</ul>
#empty
<p>This request has no submethods</p>
#endforelse
Try this:
#forelse ($result->request->methodology->submethods as $submethod)
<ul>
#foreach(json_decode($result->results) as $value)
<li>
<b>{{ $submethod->name }}</b> result is
{{ $value }}
</li>
#endforeach
</ul>
#empty
<p>This request has no submethods</p>
#endforelse

foreach in blade template not working correctly

I'm having issues on displaying my data from foreach loop. I have 400+ thumbs on my database but laravel doesn't not work correctly, and my footer template didn't display too. I will put my code below.
#foreach($thumbs as $thumb)
{{$thumb['name']}}
{{$thumb['desc']}}
{{$thumb['place']}}
#endforeach
myfooter code goes here.
from my controllers
$data['thumbs'] = thumb::all();
return View('tubetour/home',$data);
but when I tried to var_dump or return the value of thumbs on my controller
it displays all my 400+ data.
$data['thumbs'] = thumb::all();
return $data['thumbs'];
You need to pass an associative array as a second argument to the view function:
// Controller
$thumbs = Thumb::all();
return \View::make('tubetour.home', ['thumbs' => $thumbs]);
// View
#foreach($thumbs as $thumb)
{{ $thumb->name }}
{{ $thumb->desc }}
{{ $thumb->place }}
#endforeach
Edit: if you are using laravel 4 you will need to use View::make()
If Thumb is an Eloquent model, then the Thumb::all() will return an Eloquent collection, not an array. In that case you have to update your blade template like so:
#foreach($thumbs as $thumb)
{{ $thumb->name }}
{{ $thumb->desc }}
{{ $thumb->place }}
#endforeach
Hope this solve your issue.
UPDATE
Pass the $thumbs as an array and display it as table.
Update your controller like this:
$data['thumbs'] = thumb::all()->toArray();
return View('tubetour/home', $data);
And your view like this, see how many rows being displayed.
<table>
<thead>
<tr>Id</tr>
<tr>Name</tr>
<td>Desc</td>
<td>Place</td>
</thead>
<tbody>
#for ($i = 0; $i < count($thumbs); $i++)
<tr>
<td>{{ $i }}</td>
<td>{{ $thumbs[$i]['name'] }}</td>
<td>{{ $thumbs[$i]['desc'] }}</td>
<td>{{ $thumbs[$i]['place'] }}</td>
</tr>
#endfor
</tbody>
</table>
UPDATE 2
Blade template example with bootstrap grid:
<div class="row">
#for ($i = 0; $i < count($thumbs); $i++)
<div class="col-md-4">
<img src="img/sample.jpg">
<h3>{{ $thumbs[$i]['name'] }}</h3>
{{ $thumbs[$i]['desc'] }}
</div>
#endfor
</div>
try this
#foreach($data as $thumb)
{{$thumb['name']}}
{{$thumb['desc']}}
{{$thumb['place']}}
#endforeach
Update your controller
$data = Thumb::all();
return View::make('tubetour/home')->with("thumbs",$data)
->render();
Then your view with this:
#foreach($thumbs as $thumb)
{{$thumb['name']}}
{{$thumb['desc']}}
{{$thumb['place']}}
#endforeach

Get property of an object

I have a one to many model.
An WorkoutDetails which has many Exercises.
I was able to display all the data for the Exercises, but I need just the property 'Title'.
In controller I have
public function getDetails($id){
$details = WorkoutDetails::where('parent_id','=',$id)->get();
return View::make('menu_renderer.partials.details')->with('details', $details);
}
And in HTML I have
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises}}
</ul>
</div>
#endforeach
And the output is:
[{"id":1,"type_id":0,"title":"Exercise one","details":"Details
exercise one","media":"5","created_at":"-0001-11-30
00:00:00","updated_at":"-0001-11-30 00:00:00"}]
[{"id":2,"type_id":0,"title":"Exercise two","details":"Details
exercise two","media":"2","created_at":"-0001-11-30
00:00:00","updated_at":"-0001-11-30 00:00:00"}]
[{"id":3,"type_id":0,"title":"Exercise three","details":"Details
Exercise three","media":"6","created_at":"-0001-11-30
00:00:00","updated_at":"-0001-11-30 00:00:00"}]
[{"id":4,"type_id":0,"title":"Exercise four","details":"Details
Exercise four","media":"6","created_at":"-0001-11-30
00:00:00","updated_at":"-0001-11-30 00:00:00"}]
Which is not bad, but I need just the title or details.
I tried
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises->title}}
</ul>
</div>
#endforeach
or
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises.title}}
</ul>
</div>
#endforeach
or
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises[title]}}
</ul>
</div>
#endforeach
But nothing...
Can anyone give me a hint how can I display just the title or details of each Exercise which belongs to WorkoutDetails model ?
Thank you!
From the dump provided, it seems another array is holding the inner data.
hence try :
#foreach($details as $detail)
<div>
<ul>
{{$detail->exercises[0][title]}}
// or :
{{$detail->exercises[0]->title}}
</ul>
</div>
#endforeach
You can use laravel query function first() to pickup the first object.
#foreach($details as $detail)
<div>
<ul>
{{ $detail->exercises->first()->title }}
</ul>
</div>
#endforeach

Iterating over PHP array with Laravel Blade, and then adding in HTML for columns

I have an array with 38 records.
After iterating over the first 10 I want it to start on a new column.
This is very simple and an example is below, however, I need to add in HTML which makes it difficult:
#for($i = 0; $i < count($records); $i++)
#if($i % 10 == 0)
//start new column
#endif
<li>{{ $records[$i]['name'] }}</li>
#endfor
What the HTML looks like without looping and what it should look like after looping properly:
<li class="col-sm-3">
<li class="dropdown-header">
Record Set
</li>
<li>Record Name</li>
<li>Record Name</li>
<li>Record Name</li>
</li>
Problem is, after 10 records I need it to break out of col-sm-3 and start a new col-sm-3 without the dropdown-header but with each iteration's record name.
How can this be done? Please ask questions if clarification is needed.
If it's an array then you may use array_chunk:
#foreach(array_chunk($records, 10) as $ten_arrays)
<li class="col-sm-3">
#foreach($ten_arrays as $record)
{{ $record['field_name'] }}
#endforeach
</li>
#endforeach
This will output lis like this:
<li class="col-sm-3">
<!-- Ten Items -->
</li>
<li class="col-sm-3">
<!-- Ten Items -->
</li>

Categories