I'm having issues displaying a collection in a blade template.
$comments = Comment::all();
return view('comments/index')->with(compact('comments'));
The code for the blade is:
#isset($comments)
#foreach($comments as $comment)
<div>
<p>
<{{ $comment->commentor }}
</p>
</div>
<hr>
#endforeach
#endisset
#empty($comments)
<div>
<p>There were no comments available.</p>
{{ $comments }}
</div>
#endempty
But not sure how to get the data to render in the template. It just renders a blankpage.
Use this instead :
$comments = Comment::all();
return view('comments.index')->with(compact('comments'));
Use dot notation to reference the view folder structure, view('comments.index'). This represents the file resources/views/comments/index.blade.php. Use this.
#forelse ($comments as $comment)
<div>
<p>
{{ $comment->commentor }}
</p>
</div>
<hr/>
#empty
<div>
<p>There were no comments available.</p>
</div>
#endforelse
Related
#foreach ($responseBody as $response)
<div class="row">
<div class="col-4">Official Name</div>
<div class="col-8">{{$response->name->official}}</div>
</div>
<div class="row">
<div class="col-4">Currencies</div>
<div class="col-8">{{ $response->currencies???? }}</div>
</div>
#endforeach
I using Laravel to consume API from https://restcountries.com/. I success to get Name of the country, but I still failed to get the currencies. The "SGD" code is different for each country.
How to get the name of currencies?
You should run another loop for the currencies:
#foreach($response->currencies as $id => $info)
<b>{{ $id }}</b>: {{ $info->name }} {{ $info->symbol }}
#endforeach
I'm setting up a new project and want to retrieve data from passed model binding controller show method but output in show view is null
model :
class Content extends Model
{
protected $fillable = ['title','body'];
}
this is my controller :
public function show(Content $content)
{
return view('content.show',compact('content'));
}
this is my index :
#foreach ($content as $item)
<td>
<a class="btn btn-primary" href="{{ route('Content.show',$item) }}">View</a>
</td>
#endforeach
and this is show method that result is null:
<div class="card-header">
{{ $content->title }}
</div>
<div class="card-body">
{{ $content->body }}
</div>
<div class="card-footer">
{{ $content->created_at }}
/div>
i expect have result in show method because everything is okay but there isn't where is my problem ?
Updated :
dd($content)
because you try to get $content while it is $item now :)
<div class="card-header">
{{ $item->title }}
</div>
<div class="card-body">
{{ $item->body }}
</div>
<div class="card-footer">
{{ $item->created_at }}
</div>
I found the answer
I changed the route resource from resource('Content','ContentController') to
resource('content','ContentController') and the problem has been fixed !
i dont know why !
//used to show a list, like what you are trying to do
public function index()
{
$contents = Content::get();
return view('content.index',compact('contents'));
}
//used to show one item
public function show(Content $content)
{
return view('content.show',compact('content'));
}
What you have for you view works if you are looping over multiple content, otherwise you want:
{{ $content->title }}
</div>
<div class="card-body">
{{ $content->body }}
</div>
<div class="card-footer">
{{ $content->created_at }}
</div>```
Sorry for this Very silly question. But I badly need it. I'm passing post details from the posts table using this in the controller:
$posts = post::where('status',1)->orderBy('created_at','DESC')->paginate(5);
In my posts table there are title,subtitle and body.
#foreach ($posts as $post)
<div class="post-preview">
<a href="{{ route('post',$post->slug) }}">
<h2 class="post-title">
{{ $post->title }}
</h2>
<h3 class="post-subtitle">
{{ $post->subtitle }}
</h3>
</a>
<p class="post-meta">{{$post->created_at->diffForHumans() }} </p>
</div>
#endforeach
so it is showing the title,subtitle and time. but I want to show the 2/3 lines from the body attribute, then want to use "see more" link.
You can use helper class str_limit to limit your text by character count.
To limit in 100 character you can use-
{{ str_limit($post->body, 100) }}
str_limit in laravel doc
got this problem:
I want to show the info from a collection where a publication has a "featured" column in true.
$pub_destacadas = Publicaciones::take(3)->where('destacado', '=', 1)->get();
The problem is that my layout has 3 elements to fill with this information that have different sizes so i wasn't able to solve this using the chunk() method in the foreach in my blade template, so I tried the following:
$individual_destacada = $pub_destacadas->take(1);
$grupo_destacada = $pub_destacadas->take(2);
and show them in the view as follows:
<div class="publicaciones-destacadas">
<div class="container">
<h2>Publicaciones Destacadas</h2>
<div class="row">
#foreach($individual_destacada as $destacada)
<div class="col-md-6">
<div class="item-destacado size-2">
<img class="img-responsive" src="{{ asset('img/publicaciones/' . $destacada->imagen ) }}">
{{ $destacada->titulo }}
{{ $destacada->descripcion }}
</div>
</div>
#endforeach
<div class="col-md-6">
#foreach($grupo_destacada as $destacada)
<div class="row doble-publicacion">
<div class="col-md-12">
<div class="item-destacado size-1">
<img class="img-responsive" src="{{ asset('img/publicaciones/' . $destacada->imagen ) }}">
{{ $destacada->titulo }}
{{ $destacada->descripcion }}
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
So... now it shows it correctly in the layout but the first item that the query gets is shown 2 times in this layout. How can I exclude this first element in the second variable where I get the featured publications?
Hope I explained the problem correctly.
Here's the layout:
You can use splice.
$individual_destacada = $pub_destacadas->splice(1);
$grupo_destacada = $pub_destacadas->splice(2);
I have a function witch is getting all properties joining the images tables with orderBy and need to add paginate
here is my function
public function show_all()
{
$properties = PropertyDetail::where('active', 1)->with('propImages')->paginate(15)
->orderBy('id', 'DESC')
->get();
return View::make('portal.properties.view_all', compact('properties'));
}
in my view I got
call_user_func_array() expects parameter 1 to be a valid callback,
class 'Illuminate\Support\Collection' does not have a method 'orderBy'
another thing when I removed the orderBy('id', 'DESC')->get() and I try
it works but when I try to put {{ $prop->links() }} in the view I got
Call to undefined method Illuminate\Database\Query\Builder::links()
here is how my view looks like
#foreach($properties as $prop)
<div class="property col-xs-12 col-sm-6 col-md-4">
<div class="image">
<div class="content imgContainer">
{{ HTML::link('property-details/'.$prop->id, '') }}
#foreach($prop->propImages->slice(0, 1) as $image)
{{ HTML::image('images/propertyImages/'.$image->image, $prop->title) }}
#endforeach
</div>
<div class="price"> OMR. <span class="priceNumber"> {{ $prop->price }}</span></div>
</div>
<div class="title">
<h2>{{ HTML::link('', $prop->title, array('title'=>'$prop->title')) }}</h2>
</div>
<div class="location">{{ trans('location.'.$prop->propLocation->city) }}</div>
<div class="bathrooms">
<div class="content">{{ $prop->bathroom }}</div>
</div>
<div class="bedrooms">
<div class="content">{{ $prop->bedroom }}</div>
</div><!-- /.bedrooms -->
<div class="receptionRoom">
<div class="content">{{ $prop->dining_room }}</div>
</div>
</div>
#endforeach
{{ $prop->links() }}
The correct syntax is:
$properties = PropertyDetail::where('active', 1)
->with('propImages')
->orderBy('id', 'desc')
->paginate(15);