Laravel get data from 2 tables where not null - php

I'm trying to get some data from two different tables,
So it does look like this now:
But I don't want the empty fields (Globale-Moderator, Moderator and Proef-Moderator) to be shown. How can I do this whit this code;
Controller:
public function ForumTeam()
{
$roles = Role::where('id', '>', '4')->orderBy('id', 'DESC')->get();
return View::make('team')->with('roles', $roles);
}
View:
<div class="panel-group col-sm-offset-1 col-sm-10">
#foreach($roles as $role)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($role->user as $user)
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $user->username }}" width="75" height="75">
<h4 style="color:{{ $role->colour }};"><strong>{{ $user->username }}</strong></h4>
<p>{{ $user->usertitle }}</p>
</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>
So I want to hide the not filled in fields, how should I do dis propperly?
My other option is to show some text inside of the 'empty' field. Like 'There is nobody with this rank'.
Thanks!

You can/should be able to use:
#foreach($roles as $role)
#if($role->user != null || !empty($role->user->name))
#endif
#endforeach
You can then check to see if the role->user is NOT NULL OR the name of the role is not empty.WHERE name is the name of the role, i.e. "Admin", "Moderator"
Alternatively, try:
#foreach($roles as $role)
#if(count($role->user) >= 1)
// Do stuff
#endif
#endforeach
Since you are getting User which has a one-to-many relationship, therefore there will be more than one user.

You could add an if statement after the first #foreach, to check if the role
has users,
#foreach($roles as $role)
#if(isset($role->user))
...
#endif
#endforeach

Related

How to link comments to each post by id with Laravel Blade

I have created a Post and Comment application. I am trying to link the comments to the id of the post. I am trying to use PHP in a Blade template to do this. I get the error that the variable post_id does not exist. I want to use the #if and #foreach of Blade. The problem seems to be in the #if statement.
Here is my HTML:
<div class="col-md-9">
<div class="row">
#foreach($posts as $post)
<div class="col-md-12 post" id="post{{ $post->id }}">
<div class="row">
<div class="col-md-12">
<h4>
{{ $post->title }}
</h4>
</div>
</div>
<div class="row">
<div class="col-md-12 post-header-line">
<span class="glyphicon glyphicon-user"></span> by {{ $post->user->firstName }} {{ $post->user->lastName }} | <span class="glyphicon glyphicon-calendar">
</span> {{ $post->created_at }} | <span class="glyphicon glyphicon-comment"></span><a href="#">
3 Comments</a>
</div>
</div>
<div class="row post-content">
<div class="col-md-2">
<a href="#">
<img src="/images/random/postimg.jpg" alt="" class="img-responsive postImg">
</a>
</div>
<div class="col-md-10">
<p>
{{ $post->post }}
</p>
</div>
</div>
<div class="row add-comment">
<div class="form-group">
<input type="text" name="addComment" placeholder="Add your comment" class="form-control" v-model="comment">
</div>
<input id="addComment" type="submit" name="submitComment" value="Submit Comment" class="btn btn-default" v-on:click="submitComment" data-id="{{ $post->id }}">
</div>
#if($post->comment->post_id == $post->id)
#foreach($post->comment as $comment)
<div class="row">
<div class="col-md-12 mb-r">
<div class="card example-1 scrollbar-ripe-malinka">
<div class="card-body">
<h4 id="section1"><strong>By: {{ $comment->user->firstName }} {{ $comment->user->lastName }}</strong></h4>
<p>{{ $comment->comment }}</p>
</div>
</div>
</div>
</div>
#endforeach
#endif
</div>
#endforeach
</div>
</div>
Here is my PostController:
public function index(){
$posts = Post::with('comment', 'user')->orderBy('id', 'desc')->limit(20)->get();
return view('post.posts')->with('posts', $posts);
}
You don't need #if statement, remove it. $post->comment is a collection, that 's why your are not getting post_id. if you want to check, then check inside foreach.
#foreach($post->comment as $comment)
<div class="row">
<div class="col-md-12 mb-r">
<div class="card example-1 scrollbar-ripe-malinka">
<div class="card-body">
<h4 id="section1"><strong>By: {{ $comment->user->firstName }} {{ $comment->user->lastName }}</strong></h4>
<p>{{ $comment->comment }}</p>
</div>
</div>
</div>
</div>
#endforeach
You need to add post_id to the comments table and use the relationship:
public function comments()
{
return $this->hasMany(Comment::class);
}
Then you'll be able to load related comments:
Post::with('comments', ....
And iterate over posts and their comments:
#foreach ($posts as $post)
#foreach ($post->comments as $comment)
{{ $comment->content }}
#endforeach
#endforeach
I think if you create relationship between Post model and Comment model (post table and comments table), your #if is useless. I recommend you to check your corresponding models.
Your code should be like this,
Post.php
public function comments(){
return $this->morphMany('App\Comments','commentable');
}
Comment.php
public function commentable(){
return $this->morphTo();
}
in my app Comment model is shared by some other models also.
Eg :- User’s Questions also have comments.
Find more details in laravel:Eloquent page

Echoing data if it exists

I want to display default data if the table is empty. I searched in the laravel documentation and found this.
In my controller i send data to my show.blade.php
public function show($id)
{
//Get profile data from the user
$user = User::find($id);
return view( 'gebruiker/show',[
'user' => $user,
] );
}
And in my blade I display the data
#foreach ($user->profile as $profile)
<div class="col-md-12 col-xs-12" align="center">
<h1>{{ $profile->user->name or 'Geen naam' }}
</h1>
<h3>{{ $profile->age or 'Geen leeftijd' }}</h3>
<span>{{ $profile->country or 'Geen land' }}</span>
</div>
<div class="col-md-6 col-xs-6 follow line" align="center">
<h3>
Bezoekers <br/> <span>{{ $profile->visitors or 'Geen bezoekers' }}</span>
</h3>
</div>
<div class="col-md-12 col-xs-12 login_control text-center">
<br>
{{ $profile->profile or 'Geen beschrijving' }}
</div>
#endforeach
When $profile->age is not empty it shows data but when there is no data it just shows nothing. What am I doing wrong?
In blade "or" checks only if variable exists.
Lest try something like that:
#if(!empty($profile->age))
{{ $profile->age }}
#else
Geen leeftijd
#endif
You can also try this:
#if($profile->age !='' && $profile->age !='0')
{{ $profile->age }}
#else
Geen leeftijd
#endif
Depending on your needs you can create an accessor in your model:
public function getAgeAttribute($value)
{
return !empty($value) ? $value : 'Geen leeftijd';
}
Ideally this should live in a presenter. Too bad {{ $profile->age or 'Geen leeftijd' }} doesn't work, it should. Probably someone will do a PR for it soon enough.

Laravel get multiple objects in the same panel

I'm trying to get a list of the staffs on my application.
So now I got this view:
#foreach($getteam as $team)
<!-- Begin row -->
<div class="panel-group col-sm-offset-1 col-sm-10">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $team->role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $team->username }}" width="75" height="75">
<h4 style="color:{{ $team->role->colour }};">{{ $team->username }}</h4>
<p>Een klein beetje info over wie ik ben enzo... :p</p>
</li>
</ul>
</div>
</div>
</div>
<!-- End row -->
#endforeach
With this query:
SELECT * FROM `user`
INNER JOIN `role` ON `user`.`role_id` = `role`.`id`
WHERE `role_id` >= '4'
ORDER BY `role`.`id` DESC
or
User::with('Role')
->join('role', 'user.role_id', '=', 'role.id')
->where('role_id', '>=', '4')
->orderBy('role.id', 'DESC')
->get();
Now, so for so good,
This works to get the users exept this is the result:
http://prntscr.com/8a6v60
That is not what I want.
This is what I want: http://prntscr.com/8a6vgo
So when the groups are the same, they need to be in the same UL.
Edit
This is my view now:
<!-- Begin row -->
<div class="panel-group col-sm-offset-1 col-sm-10">
#foreach($roles as $role)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($role->user() as $user)
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $user->username }}" width="75" height="75">
<h4 style="color:{{ $role->colour }};">{{ $user->username }}</h4>
<p>Een klein beetje info over wie ik ben enzo... :p</p>
</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>
<!-- End row -->
This is the controller:
public function ForumTeam()
{
$roles = Role::all();
$getTeam = User::with('Role')->join('role', 'user.role_id', '=', 'role.id')->where('role_id', '>=', '4')->orderBy('role.id', 'DESC')->get();
return View::make('team')->with('getteam', $getTeam)->with('roles', $roles);
}
And this one is the model.
<?php
class Role extends Eloquent
{
protected $table = 'role';
public function user()
{
return $this->belongsTo(User::class);
}
}
Now I only get all the role names, not the correct stuff.
Use the reverse of the relationship between users and roles. Instead of fetching the users, get the roles and for each role you can get the corresponding users (this of course requires the Role model to have a users() method with a relation defined:
// You can of course use conditions here to filter out what roles you want loaded
$roles = Role::all();
Then iterate through the roles, and for each role iterate through its users:
<div class="panel-group col-sm-offset-1 col-sm-10">
#foreach($roles as $role)
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ $role->name }}</h3>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($role->users as $user)
<li class="list-group-item">
<img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $user->username }}" width="75" height="75">
<h4 style="color:{{ $role->colour }};">{{ $user->username }}</h4>
<p>Een klein beetje info over wie ik ben enzo... :p</p>
</li>
#endforeach
</ul>
</div>
</div>
#endforeach
</div>

Laravel 5: Trying to get property of non-object

I'm get:
Trying to get property of non-object (View: C:\xampp\htdocs\resources\views\posts.blade.php)
Error in these lines:
#if($post->id === Auth::user()->id)
<br>Redaguoti
#endif
All code:
#extends('layouts.main')
#section('content')
<div class="media">
<div class="media-body" rel="#author{{ $topics->author_id }}">
<h4 class="media-heading">{{ $topics->title }}</h4>
</div>
#foreach($posts as $post)
<div class="media">
<div class="media-left">
<a href="{{ generateProfileURL($post->name, $post->id) }}">
<img class="media-object" src="http://localhost/uploads/avatars/{{ $post->avatar_id }}.{{ $post->avatar_end }}" style="width: 64px">
</a>
</div>
<div class="media-body" rel="#post{{ $post->pid }}">
{{ $post->name }}<br>{{ $post->text }}
#if($post->id == Auth::user()->id)
<br>Edit
#endif
</div>
</div>
#endforeach
</div>
{!! $posts->render() !!}
#stop
Thanks in advance ;)
EDIT That's because I'm not logged in :D
Either $post isn't an object, or the user isn't logged in, so Auth::user() is returning null.
To remove the error, check first if the user is logged in before running that section of code:
#if (Auth::check())
// put auth-only code here
#if($post->id === Auth::user()->id)
<br>Redaguoti
#endif
#endif
There are several instances you can get this error,
When your response needs an authenticated user to access as your case might be and
When you are returning an object but you're accessing it as an array either using foreach, or forelse.
You have to check with all diligence what data you're returning and the respective methods accordingly

Undefined variable: $ Laravel 5

This is the code from CategoriesController:
public function index()
{
$categories = Category::all();
return view('/home', compact('categories'));
}
And this is the code that I'm writing to the home.blade.php
#foreach($categories as $cat)
#endforeach
And then in home.blade.php
I'm getting this error: Undefined variable: categories
Why this happening ? Everything works fine with Articles but not categories and the code is the same.
Home.blade.php
#extends('app')
#section('content')
<div class="col-md-4 pull-right">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Categories</h3>
</div>
<div class="panel-body">
#foreach($categories as $cat)
<div class="col-lg-6">
<ul class="list-unstyled">
<li>News
</li>
</ul>
</div>
#endforeach
</div>
</div>
</div>
#if(count($articles))
#foreach($articles as $article)
<div class="panel panel-default col-md-8">
<div class="panel-heading">
<h3>{{ $article->title }}</h3>
<small>posted by {{ $article->author->name }} {{ $article->created_at }}</small>
</div>
<div class="panel-body">
{!! str_limit($article->body, 1000) !!}
<hr>
Read More
</div>
</div>
#endforeach
#else
<h1>No articles at the moment.</h1>
#endif
<br>
{!! $articles->render() !!}
#stop
You have two routes
Route::get('/home', 'ArticlesController#index');
Route::get('/home', 'CategoriesController#index');
Which means when you visit /home, only ArticlesController#index is fired, not both.
This means that the $categories variable used in the view is not populated because your index() method in CategoriesController is intended to do this but is never called.
Therefore you need to do something like this
Route::get('/home', 'HomeController#index');
public function index()
{
$categories = Category::all();
$articles = Article::all();
return view('/home', compact('articles', 'categories'));
}
try to use
return View::make('/home')->with(compact('categories'));
instead of
return view('/home', compact('categories'));
I insert this in the top of my #foreach
<?php
use App\models\PersonalContact;
$data = PersonalContact::all();
?>
or this before #foreach add #if statement like this
#if (count($data) > 0)
#foreach ($data as $bloginfo)
<tr role="row" class="odd">
<td class="sorting_1">{{$bloginfo->name}}</td>
<td>{{$bloginfo->description}}</td>
<td>{{$bloginfo->email}}</td>
<td>{{$bloginfo->phone}}</td>
<td><img src="{{url('images/'.$bloginfo->image)}}" alt="" width="100px" height="100px"></td>
<td><i class="fas fa-edit"></i>Add | <i class="fas fa-edit"></i>Edit | <a class="btn btn-app"><i class="fas fa-edit"></i>Delete</a></td>
</tr>
#endforeach
#endif
return view('/home', compact('categories'));
The problem was $categories on compact method, you must use like a string

Categories