Laravel blog rating 2 migrations - laravel

i made a blog in laravel and now created a new migration for a rating system. i call it with
#foreach($blogs as $key => $blog)
$blog->title....
#foreach($ratings as $key => $rating)
#if( $rating->blog_id == $blog->id)
<br> rating found!
<br> {{ $rating->rating_total }}
<br> {{ $rating->rating_amount }}
#endif
#endforeach
#endforeach
it works but it doesnt seem right to run a for loop on every blog post in order to get the rating.
how can i solve this better?
thanks

You could use one that show this. Like this: <input id="input-1" name="input-1" class="rating rating-loading" data-min="0" data-max="5" data-step="0.1" value="{{ $post->averageRating }}" data-size="xs" disabled="">

Since you have the relationship setup from Blog to Rating you can eager load the 'rating' relationship for each Blog post:
$blogs = Blog::with('rating')->...->get();
Then in Blade when you are looping your $blogs you will have access to it's rating:
#foreach($blogs as $blog)
{{ $blog->title }}
#if($blog->rating)
{{ $blog->rating->rating_total }}
{{ $blog->rating->rating_amount }}
#endif
#endforeach

Related

Laravel best way to call a controller inside a view?

I have a page who contains children blocs.
Each blocs need to have a specific render (with specific template).
For this i had to use #php in my blade template.
This is my code :
PageController.php
public function edit(Page $page)
{
return view('pages.edit', compact('page'));
}
View page/edit.blade.php
<section id="contents" class="contents ui-sortable">
#foreach ($page->blocs as $bloc)
#php
echo $bloc->id;
echo App\Http\Controllers\BlocController::renderBloc($bloc);
#endphp
#endforeach
</section>
BlocController.php
public static function renderBloc(Bloc $bloc) {
echo $bloc->id;
return view('blocs.show.' . $bloc->bloc_type, [
'bloc' => $bloc,
'data' => json_decode($bloc->data)
]);
}
And then an exemple of bloc
resources/views/blocs/show/text.blade.php
#extends('blocs.show')
#section('bloc')
{{ $bloc->id }}
#endsection
resources/views/blocs/show.blade.php
<section class="bloc bloc_{{ $bloc->bloc_type }}" data-bid="{{ $bloc->id }}">
{{$bloc->id}}
#yield('bloc')
</section>
I have 2 problems with this :
I think it's not really a good way to do ? I don't like to use #php in template. I would love to have an opinion about this ? Maybe i need to use a Service Provider ?
The $bloc->id inside the template (resources/views/blocs/show/text.blade.php) is wrong (it shows the id of the first child bloc in the foreach, even if all my echo $bloc->id before display the good id (page/edit/blade.php, BlocController.php, resources/view/blocs/show.blade.php). This is an other proof i'm doing something wrong i guess ?
Thanks
If you have to use controller in your views, it means only that you have not so good architecture. Laravel's Blade easily can do what you try to solve via controller.
You can use #include with parameters and get rid of #php:
resources/views/pages/edit.blade.php
#foreach ($page->blocs as $bloc)
#include('blocs.show', ['bloc' => $bloc])
#endforeach
resources/views/blocs/show.blade.php
<section class="bloc bloc_{{ $bloc->bloc_type }}" data-bid="{{ $bloc->id }}">
{{$bloc->id}}
#include('blocs.show.' . $bloc->bloc_type, [
'bloc' => $bloc,
'data' => json_decode($bloc->data)
])
</section>
resources/views/blocs/show/text.blade.php
Bloc ID = {{ $bloc->id }}
Bloc Text = {{ $data->text }}

Laravel blade regex image and output it?

Hi I have twitter api working to output details on the page however, this
field:
{{ $item['text'] }}
Gives me something like this:
RT #JacksSmokehouse: Don't forget #HappyHour tomorrow until 6pm - #loveJacks #live_oldham #OldhamHour #YouDontKnowJack https://twitter.com/shorturl, (with t.co)
So the image https:// I want to actually display it? is it possible to write in blade template a regex, something like this:
for each {{$item['text']}} take the url if there is any and output it below the text? can be a different variable.
Here's my full code:
<div>
#if(!empty($twitterItems))
#foreach($twitterItems as $key => $item)
{{ $item['text'] }}
#if(!empty($item['extended_entities']['media']))
#foreach($item['extended_entities']['media'] as $image)
<img src="{{ $image['media_url_https'] }}" style="width:100px;">
#endforeach
#endif
{{ $item['favorite_count'] }}
{{ $item['retweet_count'] }}
{{ $item['created_at'] }}
#endforeach
#else
There are no data.
#endif

Accessing data separately from multiple tables in single view in blade template laravel

I have three different tables: persons, students and teachers. I was able to join three tables and get data as follows:
public function viewProduct()
{
$persons = Person::with(['students','teachers'])->get();
return view('master.viewPeoples', compact('persons'));
}
I want to access data in my blade. How can I get data of students and teachers table in my blade template? I have the columns rollNum, year, and semester in the students table and the columns salary and courses in the teachers table.
The following source code returns data from the person table only.
#foreach($persons as $person)
{{ $person->name }}
{{ $person->universityNum }}
{{ $person->rollNum }} //returns blank
{{ $person->year }} //returns blank
...
#endforeach
I get all data for students and teachers when I do this:
#foreach($persons as $person)
{{ $person->students }}
{{ $person->teachers }}
#endforeach
But I want to access data of each column of students and teachers table separately like$students->rollNum
try this
#foreach($persons as $person)
{{ $person->name }}
#foreach($person->students as $student)
{{ $student->rollNum }}
{{ $student->year }}
#endforeach
// Do same for teachers here
#endforeach
Hope it helps
Try this
#foreach($persons as $person)
{{ $person->students->rollNum }}
{{ $person->teachers->rollNum }}
#endforeach
or
#foreach($persons as $person)
{{ $person->students_rollNum }}
{{ $person->teachers_rollNum }}
#endforeach

Laravel 4 adding numbers from foreach loop when count variable is supplied?

I am passing the array $cats to my laravel template view. It is a multidimensional array from a database transaction, containing category data. So it would contain data like:
$cat[0]['id'] = 1;
$cat[0]['name'] = 'First Category';
And so on. In my blade template I have the following code:
{{ $i=0 }}
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
{{ $i++ }}
#endforeach
Which outputs:
0 First Category
1 Second Category
2 Third Category
Notice the numbers preceding the category name. Where are they coming from? Is this some clever Laravel trick? It seems that when you include a counter variable, they are automatically added. I can't find any mention of it anywhere, and I don't want them! How do I get rid of them?
Thanks.
You just need to use the plain php translation:
#foreach ($collection as $index => $element)
{{$index}} - {{$element['name']}}
#endforeach
EDIT:
Note the $index will start from 0, So it should be {{ $index+1 }}
The {{ }} syntax in blade essentially means echo. You are echoing out $i++ in each iteration of your loop. if you dont want this value to echo you should instead wrap in php tags. e.g.:
<?php $i=0 ?>
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
<?php $i++ ?>
#endforeach
As an additional note, if you choose to work in arrays then thats your call but unless you have a specific reason to do so I would encourage you to work with object syntax, eloquent collection objects in laravel can be iterated over just like arrays but give you a whole lot of extra sugar once you get used to it.
#foreach($cats as $cat)
{{ (isset($i))?$i++:($i = 0) }} - {{$cat['name']}}
#endforeach
<? php $i = 0 ?>
#foreach ( $variable_name as $value )
{{ $ value }}<br />
< ? php $i++ ?>
#endforeach
if your $k is integer you can use {{ $k+1 }} or isn't integer you can use $loop->iteration
// for laravel version 4 and after
#foreach ($posts as $k => $post)
{{ $loop->iteration }}. {{ $post->name }}
#endforeach
You can actually use a built in helper for this: {{ $cat->incrementing }}.

Handling conditions in blade templates on laravel 4 in a form input

I would like to test the existence of value in blade on Laravel 4.
Like:
{{ Form::text('name', #if(isset($value)) {{$value}} #endif; }}
I tried this:
{{ Form::text('name', #if(isset($value)) $value #endif; }}
You can do it in one line.
I'd prefer a better approach, using condition?if:else , which also I use in my daily projects:
{{ Form::text('name',isset($value)?$value:'') }}
or even:
{{ Form::text('name',isset($value)?$value:null) }}
Anywhere before that, you can add the line <?php if(!isset($value)) { $value= ''; } ?>. Then you can proceed as usual {{ Form::text('name', $value) }}

Resources