How can I access the data of a PHP Laravel Collection - php

402/5000
I have this collection of data, from a table called "Actividades", that has a many to many relationship with another table called "Estrategias".
My data collection has 3 Actividades that share a Estrategia.
So my problem is how I can show in the html the attribute "descripcion" of the strategy that is in the three Actividades.
the image shows the data collection
This is what I have in the controller
$actividades = Actividad::orderby('id', 'ASC')->with('estrategias')->where('evidencia_id', $evidenciaid)->get();
dd($actividades);
This is what I tried to do in the html
#foreach($actividades as $actividad)
<div>
{{$actividad->estrategias->descripcion}}}
</div>
#endforeach

estrategias is an array of instances of Estrategia
#foreach($actividades as $actividad)
<div>
#if(isset($actividad->estrategias[0]){{$actividad->estrategias[0]->descripcion}}}#endif
</div>
#endforeach

Looking at the picture you have posted, it looks like you need to loop over the estragias collection as well
#foreach($actividades as $actividad)
<div>
#foreach($actividad->estrategias as $estragias)
{{ estrategias->descripcion }}
#endforeach
</div>
#endforeach
This should fix it for you

If has one relationship
#foreach($actividades as $actividad)
<div>
{{$actividad->estrategias->descripcion }}
</div>
#endforeach
If has many relationship
#foreach($actividades as $actividad)
<div>
#foreach($actividad->estrategias as $estragias)
{{ estrategias->descripcion }}
#endforeach
</div>
#endforeach

Related

Add a block field from a specific article

I have an article, each article has article_blocks, the blocks have a video_link field
And I need to display this field in the list of articles
This is how the article list output is roughly implemented
<div class="blog-list">
#foreach($articles as $article)
<div class="blog-article">
<div class="video-button video-modal-button-blog" data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}">
<span>Watch video</span>
</div>
<h2 class="blog-article__title">{{ $article->title }}</h2>
<span>{{ date('d F Y', strtotime($article->published_at)) }}</span>
<span>{{ $article->getTotalViews() }} Views</span>
</div>
#endforeach
</div>
Each article has a button to open a video, but this video field itself is not in the article itself, but in the article blocks
I now take this field from the block and output it, it turns out like this
<?php
use App\Models\ArticleBlock;
$article_block = ArticleBlock::whereNotNull('video_link')->first();
?>
<div class="blog-list">
#foreach($articles as $article)
<div class="blog-article">
#if ($article_block->video_link !== 'null')
<div class="video-button video-modal-button-blog" data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}">
<span>Watch video</span>
</div>
#endif
<h2 class="blog-article__title">{{ $article->title }}</h2>
<span>{{ date('d F Y', strtotime($article->published_at)) }}</span>
<span>{{ $article->getTotalViews() }} Views</span>
</div>
#endforeach
</div>
As a result, I get this field with video and it is displayed, but the same video is displayed for all articles in the list, and each article should have its own field and its own video. How can this be fixed?
I probably need to look for something like blocks by article id
$article_block = ArticleBlock::where('article_id', $article->id)->whereNotNull('video_link')->first();
But I can't get id
Cant write comment, under 25 rep :(
data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}"
This looks wrong. You should eager load article blocks (eager loading is generally faster than lazy loading) for all articles with a with statement in your eloquent query see: https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
Then if everything is correct you should be able to do just {{ $article->block->video_link }} or something similar.
Above is right if article has only one article block (one to one relationship).
But it seems that article has multiple blocks (one to many relationship) so you would need to iterate over each of the article block as well with another #foreach statement.
#foreach($articles as $article)
#foreach($article->article_blocks as $block)
<p>URL of video is: https://www.youtube.com/embed/{{ $block->video_link }}</p>
#endforeach
#endforeach
Now lets answer your question why video is same everywhere? Because you've coded it as so. You should NEVER write custom queries in your blade files.
Here i am referring to this part of your code:
<?php
use App\Models\ArticleBlock;
$article_block = ArticleBlock::whereNotNull('video_link')->first();
?>
Queries should be either in controllers/services or repositories.
$article_block = ArticleBlock::whereNotNull('video_link')->first();
will just give you first article block defined in ur database where video_link is not null. It won't guarantee that ArticleBlock belongs to given Article.
Lets hope that i put you on the right path ^.^

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance

I have a Many to Many relationship between Wallet & User Models:
User.php:
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');
}
Wallet.php:
public function users()
{
return $this->belongsToMany(User::class,'user_wallet','wallet_id','user_id')->withPivot('balance');
}
And the pivot table user_wallet goes like this:
Now I need to get the name of wallets and the balance of that wallet in a table at Blade.
So at the Controller, I added this:
public function index($id)
{
// $id is the user id
$userWallet = Wallet::with("users")->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();
}
And then at the Blade:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users()->balance }}
</div>
#empty
Empty
#endforelse
But I get this error:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance
So what's going wrong here? How can I properly get the balance at the pivot table?
You have to loop user
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
#if(isset($wallet->users)&&count((array)$wallet->users))
#foreach($wallet->users as $user)
{{$user->pivot->balance }}
#endforeach
#endif
</div>
#empty
Empty
#endforelse
Your query should be
$userWallet = Wallet::with(["users"=>function ($query)use($id){
$query->where('user_id',$id);
}])->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();
That's because you use the relation's query builder object with users() but you need user property to get the collection:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->balance }}
</div>
#empty
Empty
#endforelse
But even the collection instance doesn't have a balance property as long as it is not a custom collection. You probably need to loop over the collection or use pluck to get the model's balance property:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->pluck('balance')->join(', ') }}
</div>
#empty
Empty
#endforelse

Call to undefined method error when I try to use links()

When I try to use $student->links() I see this error :
Facade\Ignition\Exceptions\ViewException
Call to undefined method App\Student::links()
I checked the controller, model etc but all of them seem OK... How can I fix this?
(I tried this code both on my Macbook and VPS -CentOS7- but same problem occurs)
That part of my view looks like this:
</tr>
#endforeach
</tbody>
</table>
{{ $student->links() }}
</div>
#endsection
Change
{{ $student->links() }}
to
{{ $students->links() }}
(use plural form).
You need to paginate in your backend code. $students = App\Student::paginate(15);
And then you can access the links()
<div class="container">
#foreach ($students as $student)
{{ $student->name }}
#endforeach
</div>
{{ $students->links() }}

Pagination Link

What is the correct way to call the "Links" function after this "Foreach"?
I don't know how to handle the variable to put in function.
#inject('usuarios', 'App\User')
#foreach($usuarios->getIndicados() as $user)
#endforeach
<div class="row">
<div class="col-12 text-center">
{{ $usuarios->getIndicados()->links() }}
</div>
</div>
Maybe it's just an editing error, but in your output the -tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.
In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.
This is what laravel document provides. You need to add links in the collection.
<div class="container">
#foreach ($users as $user)
{{ $user->name }}
#endforeach
{{ $users->links() }}

PHP Laravel Pagination: Method links does not exist

Hi I'm trying to setup pagination for a simple comments page.
I'm getting the error Method links does not exist.
Is it due to the comments being referenced from it's relationship to the post class?
Can't work out what to do here...
CommentController.php
public function show($id)
{
$comments = Comment::find($id)->paginate(5);
return view('posts.show')->with('comments', $comments);
}
show.blade.php
#foreach ($post->comments as $comment)
<li>
User Name: {{ $comment->user_name }} <br>
Comment: {{ $comment->comment }} <br>
</li><br>
#endforeach
{{ $post->comments->links() }}
find() function find only one record and paginate workes with query builder or an Eloquent query, so you can use
$comments= Comment::where('post_id', $id)->paginate(5);
and replace $post->comments to $comments, it should be
#foreach ($comments as $comment)
<li>
User Name: {{$comment->user_name}} <br>
Comment: {{$comment->comment}} <br>
</li><br>
#endforeach
{{$comments->links()}}
In your controller method you are paginating $comments but use $post->comments in your view. Replace your code with:
#foreach ($comments as $comment)
<li>
User Name: {{$comment->user_name}} <br>
Comment: {{$comment->comment}} <br>
</li><br>
#endforeach
{{$comments->links()}}
create the pagination links using the render method:
#foreach ($post->comments as $comment)
<li>
User Name: {{$comment->user_name}} <br>
Comment: {{$comment->comment}} <br>
</li><br>
#endforeach
{!! $comments->render() !!}

Categories