PHP array items - php

There is an issue in the answer section of this code. The question section works fine, it is just the question.. when dd($questionnaire) answer array is there, but no items are shown.
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ $questionnaire->title }}</div>
<div class="card-body">
<a class="btn btn-dark" href="/questionnaires/{{ $questionnaire->id }}/questions/create">Create Question</a>
<a class="btn btn-dark" href="/surveys/{{ $questionnaire->id }}-{{ Str::slug($questionnaire->title) }}">Complete Questionnaire</a>
</div>
</div>
#foreach($questionnaire->questions as $question)
<div class="card mt-3">
<div class="card-header">{{ $question->question }}</div>
<div class="card-body">
<ul class="list-group">
#foreach($question->answers as $answer)
<li class="list-group-item">{{ $answer->answers }}</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>
</div>
</div>
#endsection

dd Function means die and debug so your script stops at this point. Nothing else runs after dd

Related

Undefined variable in phpstorm using laravel . Controller & blade template are provided

I'm getting this error message for my code:
Undefined variable: questions (View:
home/xxxx/PhpstormProjects/xxxxx/resources/views/home.blade.php)
Please, try to shed some light on what could be wrong.
I have provided my QuestionController & question template. Not sure, what my code is missing. Here is my code:
QuestionController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Question;
class QuestionController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function show(Question $question)
{
return view('question')->with('question', $question);
}
question.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row ">
<div class="col-md-8">
<div class="card">
<div class="card-header">Question</div>
<div class="card-body">
{{$question->body}}
</div>
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
Edit Question
</a>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header"><a class="btn btn-primary `float-left"`
href="#">
Answer Question
</a></div>
<div class="card-body">
#forelse($question->answers as $answer)
<div class="card">
<div class="card-body">{{$answer->body}}`</div>`
<div class="card-footer">
<a class="btn btn-primary float-right"
href="#">
View
</a>
</div>
</div>
#empty
<div class="card">
<div class="card-body"> No Answers</div>
</div>
#endforelse
</div>
</div>
</div>
#endsection
home.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Questions
<div class="card-body">
<div class="card-deck">
#foreach($questions as $question)
<div class="col-sm-4 d-flex align-items-stretch">
<div class="card mb-3 ">
<div class="card-header">
<small class="text-muted">
Updated: {{ $question->created_at->diffForHumans() }}
Answers: {{ $question->answers()->count() }}
</small>
</div>
<div class="card-body">
<p class="card-text">{{$question->body}}</p>
</div>
<div class="card-footer">
<p class="card-text">
<a class="btn btn-primary float-right" href="{{ route('question.show', ['id' => $question->id]) }}">
View
</a>
</p>
</div>
</div>
</div>
#endforeach
</div>
</div>
<div class="card-footer">
<div class="float-right">
{{ $questions->links() }}
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
The first think that comes to my mind is that you are giving a variable named question to your view in your controller and you are trying to use $questions in your foreach when it must be $question as $q
The other thing is that you may want to pass all Questions rows you have to you view, it can be accomplished (this may not be the best solution) by editing your AppServiceProvider.php
You have to add this.
use View;
use App\Question;
public function boot()
{
//
View::share('questions',Question::all());
}
But if you do this, it will share the rows obtained with all your views.
hope this helps.

Retrieve Data in dynamic tabs

here i'm trying to click on any tabs to retrieve it's own data i did it well and it really retrieves the data of one raw only ?! any help?
<div class="col-lg-12">
<div class="tabs-container">
<ul class="nav nav-tabs">
#foreach($departments as $department)
<li class=""><a data-toggle="tab" href="#tab-{{ $department->id }}" aria-expanded="false">{{ $department->name }}</a></li>
#endforeach
</ul>
<div class="tab-content">
#foreach($works as $work)
<div id="tab-{{ $work->department_id }}" class="tab-pane">
<div class="panel-body">
<div class="row employees">
<div class="col-md-3">
<div class="thumbnail">
<div class="image view view-first"> <img class="img-responsive" src="" name="imaged" alt="image"> </div>
<div class="caption">
<h4>{{ $work->address }} </h4>
<p>{{ $work->body }}</p>
</div>
</div>
<a class="btn btn-primary" href="edit-portfolio.html"> تعديل</a>
<button class="btn btn-danger demo4"> حذف
</button>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
<!-- -->
</div>
i don't need to make it ajax if anyone have a good reason for why it give me only one record for each department?
The reason why you get one record for department is that your loop create one tab panel per work, for example if you have 2 works (1,2) belongs to one department (1) the result would be 2 tabs with the same id set to 1:
departments:
id
-----
1
works:
id department_id
---------------------
1 1
2 1
Html result:
<div id="tab-1" class="tab-pane">
...
</div>
<div id="tab-1" class="tab-pane">
...
</div>
note that both tabs have the same id tab-1.
Solution
group works by department_id, something like this:
$works = Work::all()->groupBy('department_id');
and then in your view:
<div class="tab-content">
#foreach($works as $department_id => $department_works)
<div id="tab-{{ $department_id }}" class="tab-pane">
<div class="panel-body">
<div class="row employees">
#foreach($department_works as $work)
<div class="col-md-3">
<div class="thumbnail">
<div class="image view view-first"> <img class="img-responsive" src="" name="imaged" alt="image"> </div>
<div class="caption">
<h4>{{ $work->address }} </h4>
<p>{{ $work->body }}</p>
</div>
</div>
<a class="btn btn-primary" href="edit-portfolio.html"> تعديل</a>
<button class="btn btn-danger demo4"> حذف </button>
</div>
#endforeach
</div>
</div>
</div>
#endforeach
</div>
Hope this helps

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 foreach database issue

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);
}

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