Laravel foreach database issue - php

I am making a forum and when the user clicks on a theme, a page shows up with every topic that belongs to that theme. The problem is, how do I do this?
I made a for each loop which shows ALL the topics from the database instead of the topics that belong to that theme I clicked on. How can I do this?
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{id}', 'ThemesController#show')->name('showtheme');
ThemesController.php
I only show the show method because I believe that's the only one necessary here
public function show($id)
{
$topics = Topic::all($);
return view('themes/topics')->with('topics', $topics);
}
topics.blade.php
#extends('layouts.default')
#section('content')
<div class="container main-content">
<div class="row first-row">
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 100, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Gepost door: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Laatste reply</h6>
<p class="center-align">Naam</p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
</div>
</div>
#endsection
I believe I'm doing something obviously wrong but I can't see what.
Help will be appreciated. Thanks in advance
If I miss some code, Please inform me.

How are you defining the relationship between a theme and it's topics? You'll have to have something like a one to many or many to many established in your database for you to be able to query correctly. Do you have anything like that yet?
****edit****
public function show($id)
{
$topics = Theme::find($id)->topics;
return view('themes/topics')->with('topics', $topics);
}

Related

laravel function which loops all duplicate project_id's and only shows last created on show.blade.php

hello i have a difficult function i need to solve:
i need a function which filters out the projects with the same project_id and only takes the project with the latest created_at data.
i currently have nothing because i dont know where to start but i am pretty sure i need to write it in my index function of my ProjectController.
overzicht.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="well">
<div class="card mt-5 mb-5">
<h2 class="card-header">{{$projectitem->project_name}></h2>
<div class="card-body">
<h2 class="card-text mt-4 mb-2"><b>Beschrijving:</b></h2><br>
{!!$projectitem->description!!}
<h2 class="mt-4 mb-2"><b>Tijdsduur(in uren):</b> {{$projectitem->time_span}} Uren</h2><br>
<h2 class="mt-2 mb-2"><b>Opdrachtgever:</b> {{$projectitem->client}}</h2>
<h2 class="mt-4 mb-2"><b>Text verslag:</b></h2><br>
{!!$projectitem->text_report!!}
<h2 class="mt-4 mb-2"><b>Foto's vooraf: </b></h2><br>
{!!$projectitem->images_before!!}
<h2 class="mt-4 mb-2"><b>Foto's achteraf: </b></h2><br>
{!!$projectitem->images_after!!}
<div class="row">
<div class="col-md-6">
<h2 class="mt-4"><b>Beschikbaar op: </b></h2>
<h4>Facebook: <?php if($projectitem->facebook == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Instagram: <?php if($projectitem->instagram == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Linkedin: <?php if($projectitem->linkedin == 1){echo "wel";}else{echo "niet";}?></h4>
<h4>Website: <?php if($projectitem->website == 1){echo "wel";}else{echo "niet";}?></h4>
</div>
</div>
<h4 class="mt-4 mb-2"><b>Aangemaakt door:</b> {{$projectitem->created_by}}</h4><br>
<div class="row">
<div class="col-md-6">
<h4 class="mt-4 mb-2 floatl "><b>Aangemaakt op:</b> {{$projectitem->created_at}}</h4><br>
</div>
<div class="col-md-6">
<h4 class="mt-4 mb-2 ml-1 mr-1 floatr "><b>Laatst gewijzigd op:</b> {{$projectitem->updated_at}}</h4><br>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
index function in Project Controller
$projects = Project::where('user_id', auth()->user()->id)->get();
$projectitem = Projectitem::all();//->orderBy('created_at')->limit(1)
return view('overzicht', [
'projects' => $projects,
'projectitem' => $projectitem,
]);
EDIT: screenshot
all help is appreciated
i dont know if it can be right for you or not but you might get it with one query
and by the way you dont need to define the project id as you get it from the project object
Project::where('project_id', project->id)->orderBy('created_at', 'desc')->first();

How to display data from database in laravel

Am trying to display all the data on the admin end but i have failed. How
can i display it?
This is the method that i want to display all the data(Workflows):
public function getUserWorkflows(){
$user = $this->userRepo->users();
$active_module = AppConstants::$ACTIVE_MOD_USERS;
$appraisals = Appraisal::with(
['academicBackgrounds','keyDuties','additionalAssignments',
'competences','workflow', 'assignments','keyDuties'] )->
where('user_id','=',$user->id)->orderBy('created_at','desc')->get();
$summaries = DataGenerator::getAppraisalSummaries($appraisals);
$user = User::with(['department'])->find($user->id);
$appraisalsAssigned = DataGenerator::getAssingedAppraisals($user->id);
$assignedSummaries = DataGenerator::getAppraisalSummaries($appraisalsAssigned);
return view('user.tasks',compact('appraisals','users','active_module','summaries','appraisalsAssigned','assignedSummaries'));
}
Below is the code snippet from the view where i want the data to displayed. But it has totally failed to display. Because i think there is something that am not getting right:
<div class="col s12">
<div class="col s12">
<ul id="issues-collection" class="collection">
<li class="collection-item avatar">
<i class="material-icons circle blue darken-4">folder</i>
<span class="collection-header">Workflow</span>
</li>
#if(isset($appraisals) && count($appraisals) > 0)
#foreach($appraisals as $appraisal)
#if($loop->iteration <= 3)
<li class="collection-item">
<div class="row row-custom-modal-footer collections-title">
<div class="col s12"><strong>{{$loop->iteration}}. </strong>
{{str_replace('_',' ',$appraisal->document_name)}}</div>
</div>
<div class="row row-custom-modal-footer valign-wrapper">
<div class="col s3"><p>Date: <span class="text-primary">{{$appraisal->created_at}}</span></p></div>
<div class="col s5">
<span class="task-cat blue-stepper">
#if(isset($summaries))
#foreach($summaries as $summary)
#if($summary['id'] == $appraisal->id)
{{$summary['stage']}}
#endif
#endforeach
#endif
</span>
</div>
<div class="col s2">
<div class="progress">
<div class="determinate blue darken-4"
#if(isset($summaries))
#foreach($summaries as $summary)
#if($summary['id'] == $appraisal->id)
style="width: {{$summary['percentage']}};"
#endif
#endforeach
#endif></div>
</div>
</div>
<div class="col s1">
<p class=" camel-case"><a class="text-primary bold-text" href="{{route('view_appraisal',[$appraisal->id])}}">View</a></p>
</div>
<div class="col s1">
<p class=" camel-case"><a class="text-primary bold-text" href="{{route('view_appraisal',[$appraisal->id])}}">Stop Workflow</a></p>
</div>
</div>
</li>
#endif
#endforeach
<li class="collection-item">
<div class="row">
<div class="col s12 spacer-top">
View all your tasks
</div>
</div>
</li>
#else
<li class="collection-item">
<div class="col s12 bold-text">
<p>There are no pending tasks</p>
</div>
</li>
#endif
</ul>
</div>
</div>
I want when the person logs in as an admin, they are able to see all the data from the database once they access the view. But it only displays the data when the person logs in as a user.
I have found the solution hence i just needed to make these edits in my controller method:
public function getUserWorkflows(){
$user = $this->userRepo->users();
$active_module = AppConstants::$ACTIVE_MOD_USERS;
// Edited this line of code
$appraisals = Appraisal::with(
['academicBackgrounds','keyDuties','additionalAssignments',
'competences','workflow', 'assignments','keyDuties'] )->
orderBy('created_at','desc')->get();
$summaries = DataGenerator::getAppraisalSummaries($appraisals);
$user = User::with(['department'])->find($user->id);
$appraisalsAssigned = DataGenerator::getAssingedAppraisals($user->id);
$assignedSummaries = DataGenerator::getAppraisalSummaries($appraisalsAssigned);
return view('user.tasks',compact('appraisals','users','active_module','summaries','appraisalsAssigned','assignedSummaries'));
}
Thanks so much, i have found the solution after carefully going through the controller method's lines of code

laravel foreach broke column

Sorry for title i couldn't think better title!
anyway, this is my blog page code:
#extends('layouts.frontend_index')
#section('title', 'Blog')
#section('content')
<div class="container">
<div class="row" style="margin-bottom:30px;">
<div class="col-md-8">
<div class="row">
#forelse($posts as $post)
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12" style="margin-bottom:15px;">
<div class="card card2">
<div class="cardimg">
<img src="{{ url('images/') }}/{{ $post->image }}" class="card-img-top" style="width:100%; height:auto; max-height:500px;" alt="{{ $post->title }}">
</div>
<div class="card-block">
<h4 class="card-title">{{$post->title}}</h4>
<p class="card-text">{!! str_limit($post->body, 10) !!}</p>
<p><a class="btn btn-block btn-link" href="{{ route('frontshow', $post->slug ) }}">Read More <i class="fa fa-arrow-right"></i></a></p>
</div>
</div>
</div>
#empty
<p>No Post Available yet!</p>
<div class="text-center">
<a class="btn btn-error" href="{{url('/')}}">Back to Main Page</a>
</div>
#endforelse
<div class="col-md-12 text-center">
{{$posts->links()}}
</div>
</div><!--end row-->
</div><!--end col-md-8-->
<div class="col-md-4">
<h3>Get Latest Updates...</h3>
<a target="_blank" href="https://t.me/studiotjd"><div id="telegram"></div></a>
<a target="_blank" href="https://www.facebook.com/studiotjd"><div id="facebook"></div></a>
<a target="_blank" href="https://www.instagram.com/studiotjd/"><div id="instagram"></div></a>
</div><!--end col-md-4-->
</div><!--end row-->
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-4 span3 wow rollIn" style="animation-delay: 1s;"><div id="Library"></div></div><!--end col-md-4-->
<div class="col-md-4 span3 wow bounceInDown center" style="animation-delay: 1s;"><div id="Library2"></div></div><!--end col-md-4-->
<div class="col-md-4 span3 wow bounceInRight" style="animation-delay: 1s;"><div id="Library3"></div></div><!--end col-md-4-->
</div><!--end row-->
</div><!--end col-md-12-->
</div><!--end row-->
</div>
#endsection
here is how it looks like in view:
I tried almost everything to fix this issue but nothing works!
As you can see in my inspect all col-md-4 goes under first col-md-4 and i don't know why
Is there anyone knows how to fix this issue?
try this
{{ strip_tags(str_limit($post->body, 10)) }}
I think you have html element stored in your database, and you are just using str_limit so the tags in your body is not closed.
problem is a height of div, check it on the inspector code. 1 px or more is

Laravel 5.2 Undefined variable theme

I'm making a forum and the if the user clicks on a theme, he gets redirected to the page where all the topics are that belong to that theme. What I'm trying to do, is that the user is able to click on the topic and gets redirected to it.
The problem is, is that I get an error saying Undefined variable: theme
As you can see in the code below, I have a foreach loop in my theme.blade.php which loops all the topics that belong to the theme he/she clicked on. In the beginning of the loop there is a <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}"1 which is supposed to bring the user to the topic that he/she clicked on. The URL that is supposed to show is forum.dev/theme/{theme_id}/topics/{topic_id} But that doesn't work.
And it gives me that error code. All the code is below, If i miss some code, Please inform me about it and i'll update the question. Thanks in advance
theme.blade.php
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 125, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Posted by: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Last reply</h6>
<p class="center-align"></p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{theme_id}/topics', 'ThemesController#show')->name('showtheme');
Route::get('/theme/{theme_id}/topics/{topic_id}', 'TopicsController#topic')->name('showtopic');
Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function() {
//THEMES
Route::get('/theme/{theme_id}/edit', 'ThemesController#edit')->name('edittheme');
Route::patch('/theme/{theme_id}/edit', 'ThemesController#update')->name('updatetheme');
Route::get('/theme/create', 'ThemesController#create')->name('createtheme');
Route::post('/theme/create', 'ThemesController#save')->name('savetheme');
Route::delete('/theme/{theme_id}/delete', 'ThemesController#destroy')->name('deletetheme');
//TOPICS
Route::get('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#edit')->name('edittopic');
Route::patch('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#update')->name('updatetopic');
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController#save')->name('savetopic');
Route::delete('/theme/{theme_id}/topics/{topic_id}/delete', 'TopicsController#destroy')->name('deletetopic');
});
Route::get('user/profile', 'UserController#profile')->name('showprofile');
Route::post('user/profile', 'UserController#update_avatar');
TopicsController.php (Show method)
public function show($id)
{
$theme = Theme::find($id);
$topics = Topic::find($id);
return view('topics/topic')->with('topics', $topics)->with('theme', $theme);
}
I don't know if you can use the double '->with' option.
What will work is the following:
return view('topics/topic', ['topics' => $topics, 'theme'=> $theme]);
You pass an array as the second argument of the view. You can add as many keys and variables as you like.

Laravel multipule relationships count

I have these models:
ForumCategory
Forum
Topic
Reply
In the main forum page, I want to count and display the number of replies that associated to this forum.
All the models have relationships.
This is the view:
#foreach ($categories as $category)
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default striped">
<div class="panel-heading">
<div class="row">
<div class="col-md-11">{{ $category->name }}</div>
<div class="col-md-1 text-right"><a><span class="fa fa-arrow-down"></span></a></div>
</div>
</div>
#foreach ($category->forums as $forum)
<div class="panel-body">
<div class="row">
<div class="col-md-1 text-right"><span class="fa fa-envelope fa-4x"></span></div>
<div class="col-md-7">
<h4>
{{ $forum->name }}
<br>
<small>{{ $forum->description }}</small>
</h4>
</div>
<div class="col-md-1 post-num">
<h5>{{ $forum->topics->count() }}<br>
<small>Topics</small></h5>
<h5>{{ $forum->topics->replies->count() }}<br>
<small>Replies</small></h5>
</div>
<div class="col-md-3">
<img src="" class="img-circle last-comnt-img">
Last comment<br>
By Username<br>
<span class="text-muted">12:31</span>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
#endforeach
This line won't work:
$forum->topics->replies->count()

Categories