Echoing data if it exists - php

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.

Related

Display data in the blade from the database

How could I display data from the database in certain tables in my view? Without foreach, because it would overlap me. I want to take an id and that will be displayed and if I click on another id, it will display what I want from the other id. Here's how I did it.
Controller:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::all();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
Blade:
<div class="card-body p-0">
<div class="mailbox-read-info">
<h5 align="center">{{ $posts->title }}</h5>
<h6> From userID: {{ $posts->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $posts->created_at }}</span></h6>
</div>
<div class="mailbox-read-message">
<p>{{ $posts->content }}</p>
</div>
I must say that I have looked at other issues like this, but I can't get a tip?
#foreach($posts as $post)
<div class="card-body p-0">
<div class="mailbox-read-info">
<h5 align="center">{{ $post->title }}</h5>
<h6> From userID: {{ $post->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $post->created_at }}</span></h6>
</div>
<div class="mailbox-read-message">
<p>{{ $post->content }}</p>
</div>
</div>
#endforeach
EDIT: If you want to return only 1 post, use this in your controller:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::where('id', 1)->first();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
$posts is a collection so you have to iterate over it - example:
<div class="card-body p-0">
#foreach($posts as $post)
<div class="mailbox-read-info">
<h5 align="center">{{ $post->title }}</h5>
<h6> From userID: {{ $post->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $post->created_at }}</span></h6>
</div>
<div class="mailbox-read-message">
<p>{{ $post->content }}</p>
</div>
#endforeach
</div>

Missing required parameters for [Route: ticket.edit] [URI: ticket_ads/edit/{ad}]

I am trying to edit a record in a table. I have created a route and the form, but I can't get past this error. I have figured out the problem but I can't find a fix. Am I correct in thinking that the edit.blade.php file needs the $ad->id passing?
The $ad->id is an ID of a specific add in a List View. The list view has all the tickets displayed from a table, and the below link is meant to edit that one item.
The edit route is accessed using following code:
Edit
I have one route that is supposed to open up the edit view form:
Route::get('/ticket_ads/edit/{ad}', 'TicketAdsController#editTicketAdForm')->name('ticket.edit');
The above route points to this in the controller:
public function editTicketAdForm($id)
{
//$ad = DB::table('ticket_ads')->where('id', $id)->value('id');
return view('Ads.edit')->with('id', $id);
}
This is the view called by the above function:
#extends('Shared.Layouts.MasterWithoutMenus')
#section('title')
Edit a ticket ad
#stop
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h2>Edit your ticket ad</h2></div> <br/>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title', Input::old('title'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('comment', 'Comment') }}
{{ Form::text('comment', Input::old('comment'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
#endsection
This is the line that throws the error
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
The ID displays normally in the URL as ticket_ads/edit/7 for example.
How do I get past this?
Change this line:
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
to this:
{{Form::open(array('route' => array('ticket.edit', $id)))}}
This
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
is wrong. Correct syntax is:
{{ Form::open(['route' => ['ticket.edit', $id]]) }}
also you should safely ditch using array() in favor of [] syntax as Laravel requires PHP 5.4+ anyway (unless you are using ancient version of Laravel, like v4?)
The correct syntax for calling route is.
{{ route('/cardetails', ['121','cars'] ) }}
In URL it will be like this below line.
127.0.0.1:8000/cardetails/121/cars

How can i have the form text empty?

I dont know the way to leave the the form text empty is there is no result.I need something like this.....
If there is no userCreator = form text empty
My code
<div class="col-md-4">
<div class="form-group">
{{ Form::label('userCreator','UserCreator') }}
#if(isset($userCreator))
#foreach($userCreator as $cs)
{{ Form::text('userCreator',$cs->home_lastname,['class'=>'form-control']) }}
#endforeach
#else{ {{-- What should i put inside here ? --}}
}
#endif
</div>
</div>
#if(isset($userCreator) && count($userCreator) > 0)
<div class="col-md-4">
<div class="form-group">
{{ Form::label('userCreator','UserCreator') }}
#foreach($userCreator as $cs)
{{ Form::text('userCreator',$cs->home_lastname,['class'=>'form-control']) }}
#endforeach
</div>
</div>
#endif
try this ...
<div class="col-md-4">
<div class="form-group">
{{ Form::label('userCreator','UserCreator') }}
#if(!empty($userCreator))
#foreach($userCreator as $cs)
{{ Form::text('userCreator',$cs->home_lastname,['class'=>'form-control']) }}
#endforeach
#else
{{ Form::text('userCreator','', ['class'=>'form-control']) }}
#endif
</div>
</div>

Laravel get data from 2 tables where not null

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

Laravel Commenting System Only Working on most recent Post

I recently created a commenting system for a blog. When there is one post in the blog, the commenting sytem works fine. However, when there are two posts on the blog, and I comment on the FIRST post, the comment shows up on the SECOND post. Similarily, if I add another post, so there are three posts in my blog, when I comment on the first post, the comment shows up under the third post. In the same situation, when I comment on the second post, the comment shows up under the third post. Here is my code:
/index.blade.php
#section('content')
<div class="col-md-8 col-md-offset-2">
<hr>
#foreach($posts as $post)
<h3>{{ $post->title}}</h3>
<p>by {{ $post->user->name }}</p>
<img src='{{ asset($post->image_path) }}' class="img-responsive" id="blogpic"/>
<p>{{ Str::words($post->body) }}</p>
<p>Read More...</p>
<hr>
{{ Form::open(array('action' => 'CommentController#newComment')) }}
{{ Form::hidden('post_id', $post->id) }}
<div class="row">
<div class="col-md-10">
{{ Form::text('body', null, array('class'=>'form-control')) }}
</div>
<div class="col-md-2">
{{ Form::submit('Post', array('class'=>'btn btn-info')) }}
</div>
</div>
<br>
#foreach($post->comments as $comment)
<div class="comment">
<p><span class="bold">{{ $comment->user->name }}</span>: {{ $comment->body }}</p>
</div>
#endforeach
#endforeach
</div>
#stop
Here is my new comment controller:
public function newComment()
{
$comment = New Comment();
$comment->user_id = Auth::user()->id;
$comment->post_id = $post_id = Input::get('post_id');
$comment->body = Input::get('body');
Post::find($post_id)->comments()->save($comment);
return Redirect::action('BlogController#index');
}
I am pretty sure the problem is in my index file, and I can provide more code if needed. My comment only seems to get the most recent id in this line
{{ Form::hidden('post_id', $post->id) }}
What could be going wrong?
Thanks in advance

Categories