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
Related
I am issuing a problem with the following:
I foreach-ed my categories from index 1 to 3 in my blade file.
Code
#foreach($categories->take(3) as $category)
<ul>
<li>{{$category->name}}</li>
</ul>
#endforeach
But now I want to foreach second half of my categories from my database on the other ul list.
How to do that..?
Thanks:)
You can use splice method
to popout the first 3 elements and then use another foreach to create another ul list from the other categories like below:
<?php
#foreach($categories->splice(0, 3) as $category)
<ul>
<li><a>{{ $category->name }}</a>
</ul>
#endforeach
#foreach($categories as $category)
<ul>
<li><a>{{ $category->name }}</a>
</ul>
#endforeach
I need your help. Lets say I have array with 10 elements and every record to be shown on every iteration and I want to be done with Livewire, let me share with you some code and will tell you what I have tried till now.
public $content;
public $array = ['first', 'second', 'third' ,'fourth'];
foreach ($array as $item) {
sleep(1);
$this->content[] = "Element ${item}";
}
<div class="modal-body">
#if ($content)
<ul class="listree">
#foreach ($content as $element)
<li>
<div class="listree-submenu-heading">
{!! $element['title'] !!}
</div>
<ul class="listree-submenu-items">
#foreach ($element['elements'] as $sub)
<li>
<div class="listree-submenu-heading">
{!! $sub['title'] !!}
</div>
</li>
#endforeach
</ul>
</li>
#endforeach
</ul>
#endif
</div>
My idea is display first record, wait 1 second, display first and second record, wait 1 second, display first, second and third records and so on... How I can do this with livewire. The problem is that $content is filled with the information after all iteration and then the component is refreshed.
I tried on every iteration to send custom event which will call refresh method, but without succes. I will appreaciate any advice, and if you need more information, I will provide it.
Assumming you're also using alpinejs, this can be done pretty easily.
<x-app-layout>
<div class="text-gray-800 ml-10">
<ul class="bg-green-200">
<!-- The main foreach loop. -->
#foreach (range('a', 'z') as $element)
<!-- render al <li> tags with display:none. -->
<!-- Show the 1st after 0s, the 2nd after 1s, the 3rd after 2s, ... -->
<li class="bg-blue-200 m-5"
x-data="{show: false, index: {{ $loop->index }} }"
x-init="setTimeout(() => show = true, index * 1000)">
<div x-show="show">
{{ $element }}
...
</div>
</li>
#endforeach
</ul>
</div>
</x-app-layout>
$loop is a special object you can access within a #foreach block.
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
I have 2 models 'Trips.php'
public function region()
{
return $this->belongsTo('App\Region');
}
Region.php
public function trips()
{
return $this->hasMany('App\Trip');
}
I'm trying to show the trips that are in one one specific region. For example: I have a region named Lake Side and I want to show all trips that are in Lake Side region in list.
I tried the following:
In controller:
$trips = Trip::all();
In view:
#foreach($trips as $trip)
<li><a href="#">
{{$trip->region->name}}</a>
<ul class="dropdown">
<li><a href="#">
{{$trip->title}}</a>
</li>
</ul>
</li>
#endforeach
This gives me region name and trip name but repeats region name if more than one trip is made in same region.
And tried another way around (inverse):
<ul class="dropdown">
#foreach($regions as $region)
<li><a href="#">
{{$region->tour->title}}</a>
</li>
#endforeach
</ul>
And getting error Trying to get property of non-object
One way to do that is to use whereHas():
$trips = Trip::whereHas('region', function($q) use ($regionName) {
$q->where('name', $regionName);
})->get();
Pass $trips and $regionName to the view:
#foreach ($trips as $trip)
<li>{{ $regionName }}
<ul class="dropdown">
<li><a href="#">
{{ $trip->title }}</a>
</li>
</ul>
</li>
#endforeach
Alternatively, you can use eager loading:
$region = Region::where('name', $regionName)->with('trips')->first();
And in the view:
#foreach ($region->trips as $trip)
<li>{{ $region->name }}
<ul class="dropdown">
<li><a href="#">
{{ $trip->title }}</a>
</li>
</ul>
</li>
#endforeach
By getting trips with regions, what you're essentially getting is every trip with a region property.
Instead do the reverse Region::with('trips')->where('id', $regionId)->get() and you'll get regions with their trips. So now every region result has a trips property which contains many trips.
Alternatively don't use eager load. So Region::firstOrFail($regionId) then just use $region->trips.
And you can loop through them like
// Can print region here
#foreach($region->trips as $trip)
// Can print region's trips here
#endforeach
I want to be able to display infinitive ammount of subcategories in my view for example I have in my blade view
#if (count($projects) > 0)
<ul>
#foreach ($projects as $project)
#include('partials.project', $project)
#endforeach
</ul>
#else
#include('partials.projects-none')
#endif
partials.project
<li>{{ $project['name'] }}</li>
#if (count($project['children']) > 0)
<ul>
#foreach($project['children'] as $project)
#include('partials.project', $project)
#endforeach
</ul>
#endif
projects-none
You have no projects!
but after I enter I get error message
Undefined variable: projects but when I dd($projects) i get data from database
Controller code
$projects= Projects::all(); (I tried it even with pluck but it didn't work)
return view('projects')->with('projects', $projects);
I have added dd($projects) after if loop in view to see if I'm receiving it all and I was receiving it;
I. With your "messy way"
- Change
#include('partials.project', $project)
- To
#include('partials.project', ['project'=>$project])
II. Try this better way. I though
- All in partials.projects
<ul>
#each ('partials.project', $projects, 'project', 'partials.projects-none')
</ul>
Similar to partials.project. I lot of code will be saved.