get unique Tasks From relation - php

I have relation tags that have many tasks whet I get data from foreach I get duplicated values
in example
in my path i have 2 tags PHP ,HTML5
PHP Has [PHP Task_1 , PHP_Task_2]
HTML5 Has [HTml5 Task_1,PHPTask_2]
because task has many tags so i will get to duplicated PHP_Task_2
i need to get every task just one time
My Controller
$posts2 = Path::with(['pathtags' => function ($q) use ($TagArray)
{$q->with(['Tasks' => function ($q) use ($TagArray) {$q->has('tasktags', '=', 2)
->with('tasktags');
}]);
}])->where('id', '=', 1)->get();
My Blade
#foreach ($posts2 as $item)
<h2> {{$item->name}}</h2>
#foreach ($item->pathtags as $Tag)
<li> Path Tag :: {{ $Tag->name }} </li>
#foreach ($Tag->Tasks as $Task)
<li> Task :: {{ $Task->task_name }} </li>
#foreach ($Task->tasktags as $TaskTag)
<li> Task Tags :: {{ $TaskTag->name }} </li>
#endforeach
#endforeach
#endforeach
#endforeach

Related

Laravel How to add orderBy on categorized posts

I have a laravel-application where I have a Blog section. The posts are split up into categories so that the user can switch between each categories. So far so good but since I added the categorization and I click the "All"-tab, the orderBy()-method does not work anymore. Instead, the posts appear under their respective category, ignoring the orderBy-method.
Here is my controller:
$blogs = QueryBuilder::for(Blog::class)
->allowedFilters([
AllowedFilter::exact('category', 'blog_category_id'),
])
->orderBy('publishes_on', 'desc')
->where("publishes_on", "<=", Carbon::now())
->where('blog_status_id', '=', '2')
->with('blog_category', 'employee')
->get();
$blog_categories = BlogCategory::get();
return view('blog.index', compact('blogs', 'blog_categories'));
then in my view:
// the tab menu - click events handled with Javascript
<ul>
<li data-tab-id="all" class="tab all active">All</li>
#foreach ($blog_categories as $category)
<li data-tab-id="{{ strtolower($category->name) }}" class="tab {{ strtolower($category->name) }}">{{ $category->name }}</li>
#endforeach
</ul>
// the posts loop
#foreach($blog_categories as $category)
#foreach($category->blog_post as $blog)
<div class="col-md-4 post {{ strtolower($category->name) }}">
<p>{!! $blog->text !!}</p>
</div>
#endforeach
#endforeach
so, how can I achieve that the orderBy works again as intented?
thanks in advance!

How to Count two array in foreach loop

Hi everybody here I have 2 paths so I return 2 arrays of tasks I need to count each array So in last I get
[6, 4]
#foreach ($path->pathtags as $Tag)
#foreach ($Tag->Tasks as $Task)
#if (!in_array($Task->id,$a))
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endif
#endforeach
#endforeach
Easiest php way of counting arrays is count($array) so if you want to make a new array with the 2 array counts you can do it like this:
$counts = [count($array1), count($array2)];
But if you need to count the records in the database, you will need to change the query instead of ->get() you will need to use ->count().
You should try this:
$path = count($path->pathtags);
$task = count($Tag->Tasks);
Updated answer
$path = count($path->pathtags);
#foreach ($path->pathtags as $Tag)
$task = count($Tag->Tasks);
#foreach ($Tag->Tasks as $Task)
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endforeach
#endforeach

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() !!}

Blade: Undefined Variable Inside Foreach Loop

I have a controller which gets an array of a User's diaries from my database and passes them to my view:
<?php
public function readDiaries($hash)
{
$user = User::where('hash', $hash)->first();
$diaries = Diary::where('user_id', $user->id)->get();
return view('app.diary.readDiaries', ['diaries' => $diaries]);
}
In my view, I am looping through the diaries using a #foreach loop.
<div id="diaries" class="card-columns">
#if (count($diaries) > 0)
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
#endif
</div>
But I am getting the following undefined variable error...
Undefined variable: diary (View: C:\xampp\htdocs\personal_projects\Active\diary_app\resources\views\app\diary\readDiaries.blade.php)
Why is my $diary variable undefined inside the #foreach loop?
You have a typo
change #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)
and it should work!
There is some typo in var_dump use {{ var_dump($dairy) }}
Try to use compact method for pass data to view as shown below
//return view('app.diary.readDiaries', compact('diaries'));
public function readDiaries($hash)
{
$user = User::where('hash', $hash)
->first();
$diaries = Diary::where('user_id', $user->id)
->get();
return view('app.diary.readDiaries', compact('diaries'));
}
It is just a simple spelling mistake,
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
in your foreach you are passing $dairy and dumping var name is $diary
chane it to
#foreach ($diaries as $dairy)
{{ var_dump($dairy) }}
#endforeach
Habbit of copy paste is sometimes good for us...
:)
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
you used ($diaries as $dairy) in foreach
but in foreach you used ($diary)
you want edit this {{ var_dump($diary) }} to {{ var_dump($dairy) }}
or you want edit #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)

Laravel recursive partials view categories

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.

Categories