Trying to read from a database using laravel - php

I am trying to create a blog using Laravel 5.5 but I have received this error.
"Missing required parameters for [Route: posts.show] [URI: posts/{post}]. (View: /home/vagrant/Code/dialhousesetup.test/resources/views/posts/index.blade.php)"
On my postcontroller.php file. I did this so I can output all posts on my database:
public function index()
{
//Create a variable and store all the blog posts in it from the data base
$posts = Post::all();
//Return a view and pass in the above variable
return view('posts.index', array('posts' => $posts));
}
This is the loop I wrote on the /posts page to display all the blog posts.
<div class="events mb-100">
<div class="container">
<div class="row">
#foreach ($posts as $post)
<div class="col-md-6">
<a href="{{ route('posts.show'), $post->id }}">
<div class="single-event text-center">
<img src="http://www.dialhousehotel.com/wp-content/uploads/2018/01/CC_1419-The-Dial-House-website-01-18-04.jpg" alt="" style="border:2px solid #bb9b50;">
<h2>{{$post->title}}</h2>
<p>{{$post->main_body}}</p>
<div class="separator"></div>
<button type="submit" class="button button-simple mt-30">Read More</button>
</div>
</a>
</div>
#endforeach
</div>
</div>
</div>
I honestly don't know what I did wrong.
Update:
People have noticed there was a syntax issue and I have replaced {{ route('posts.show'), $post->id }} with {{ route('posts.show', $post->id) }}
#foreach($posts as $post)
<div class="col-md-6">
<a href="{{ route('posts.show', $post->id) }}">
<div class="single-event text-center">
<img src="http://www.dialhousehotel.com/wp-content/uploads/2018/01/CC_1419-The-Dial-House-website-01-18-04.jpg" alt="" style="border:2px solid #bb9b50;">
<h2>{{$post->title}}</h2>
<p>{{$post->main_body}}</p>
<div class="separator"></div>
<button type="submit" class="button button-simple mt-30">Read More</button>
</div>
</a>
</div>
#endforeach
However, I am now getting an "Invalid argument supplied for foreach() (View: /home/vagrant/Code/dialhousesetup.test/resources/views/posts/index.blade.php)"
<?php $__currentLoopData = $posts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $post): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<div class="col-md-6">
<a href="<?php echo e(route('posts.show', $post->id)); ?>">
<div class="single-event text-center">
<img src="http://www.dialhousehotel.com/wp-content/uploads/2018/01/CC_1419-The-Dial-House-website-01-18-04.jpg" alt="" style="border:2px solid #bb9b50;">
<h2><?php echo e($post->title); ?></h2>
<p><?php echo e($post->main_body); ?></p>
<div class="separator"></div>
<button type="submit" class="button button-simple mt-30">Read More</button>
</div>
</a>
</div>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

You're using the wrong syntax. Change it to:
{{ route('posts.show', $post->id) }}

The error is here.
<a href="{{ route('posts.show'), $post->id }}">
It needs the ID to be part of the route. So it would be
<a href="{{ route('posts.show', $post->id) }}">

Related

Laravel can't find the second passed parameter

I'm passing two parameters to my ShopController. one is a lang parameter that is in a route group as a prefix for all routes and the second one is a slug passed through a link.
Here's my controller:
function show($lang, $slug)
{
$product = Product::where('slug', $slug)->first();
return view('shop_show')->with(['product' => $product]);
}
If I pass only one parameter the function takes it as the prefix of the route group, aka the {lang} parameter and returns this error:
Attempt to read property "name" on null. so I have to pass two.
I die and dumped both $lang and $slug parameters and they are showing the correct values. $lang is "en" and $slug is "laptop-1" or any other slug I pass in. however after going through the function, it returns an error:
Missing required parameter for [Route: shopShow] [URI: {lang}/shop/{product}] [Missing parameter: product].
This is the route:
Route::group(['prefix' => '{lang}'], function () {
Route::get('/shop/{product}', [ShopController::class, 'show'])->name('shopShow');
});
this is the URL of the page:
http://127.0.0.1:8000/en/shop/laptop-9
Any ideas?
UPDATE: Here's my shop_show.blade.php view file:
<x-layout :title="$product->name">
<section class="navbar py-2 border-bottom border-2" style="background-color: #eee; font-weight: 500">
<div class="container w-75">
<ul class="breadcrumb">
<li class="breadcrumb-item">
<a class="text-decoration-none" href="{{ route('landingPage', App::getLocale()) }}">Home</a>
</li>
<li class="breadcrumb-item">
<a class="text-decoration-none" href="{{ route('shopIndex', App::getLocale()) }}">Shop</a>
</li>
<li class="breadcrumb-item">
<a class="text-muted text-decoration-none" href="{{ route('shopShow', ['product' => $product->slug, 'lang' => App::getLocale()]) }}">{{$product->name}}</a>
</li>
</ul>
</div>
</section>
<section class="my-5">
<div class="container w-75 d-flex justify-content-between align-items-start">
<div class="w-50">
<div>
<div>
<img id="currentImage" class="active border border-2 rounded-0 p-5 w-75" src='{{ $product->image && file_exists('storage/' . $product->image) ? asset('storage/' . $product->image) : asset('images/not-found.jpg') }}' alt="">
</div>
<div class="product-images">
<div class="product-thumbnails selected">
<img class="w-75" src='{{ $product->image && file_exists('storage/' . $product->image) ? asset('storage/' . $product->image) : asset('images/not-found.jpg') }}' alt="">
</div>
#if ($product->images)
#foreach (json_decode($product->images, true) as $image)
<div class="d-flex align-items-center product-thumbnails">
<img class="w-75" src="{{ file_exists('storage/' . $image) ? asset('storage/' . $image) : asset('images/not-found.jpg') }}" alt="">
</div>
#endforeach
#endif
</div>
</div>
</div>
<div class="card border-0 w-50">
<div>
<h3 class="mb-5">
{{$product->name}}
</h3>
<span class="text-muted" style="font-weight: 500">
{{ $product->details }}
</span>
<div class="h3" style="font-weight: 700">
${{$product->price / 100}}
</div>
<p class="my-3">
{!! $product->description !!}
</p>
<form action="{{ route('cartStore', App::getLocale()) }}" method="POST">
#csrf
<input type="hidden" name="name" value="{{$product->name}}">
<input type="hidden" name="price" value="{{$product->price}}">
<input type="hidden" name="id" value="{{$product->id}}">
<button class="btn py-2 px-3 border border-2 rounded-0 border-secondary" type="submit">Add to Cart</button>
</form>
</div>
</div>
</div>
</section>
#include('partials/might_also_like')
<script>
const currentImage = document.querySelector('#currentImage');
const images = document.querySelectorAll('.product-thumbnails');
images.forEach((element) => element.addEventListener('click', thumbnailClick));
function thumbnailClick(e) {
currentImage.classList.remove('active');
currentImage.addEventListener('transitionend', () => {
currentImage.src = this.querySelector('img').src;
currentImage.classList.add('active');
})
thumbnails.forEach((element) => element.classList.remove('selected'));
this.classList.add('selected');
}
</script>
SOLVED: There was a link in a component within a component within my layout component (3 times nested) that passed only one param, I fixed it by adding another param.
It's always a better idea to do localization before defining your routes so that you notice an error immediately instead of having to spend hours going through all your view files.

To show the subcategory of the same category on the images page Laravel

I want to show the same category's subcategory on my images page.
But when I am using foreach loop then all subcategory view is happening.
demo- https://oyewallpaper.com/photo/28529/rashmika-mandanna-saree-whatsapp-dp-pics-images
<div class="panel panel-default">
<h4 style="margin-top:-5px;">Related</h4>
<div class="projects-catalog" style=" margin-bottom: -15px;margin-top: -10px;">
<div class="catalog-cover">
<i class="left-button"></i>
<ul class="sliderWrapper1 sliderscroll">
#foreach( App\Models\SubCategories::where('subcategories_id', $response->categories_id)->get() as $subcategories )
<?php
if( $subcategories->thumbnail == '' ) {
$_image = 'default.jpg';
} else {
$_image = $subcategories->thumbnail;
}
?>
<li class="slide">
<a class="slink sq" href="{{ url('s',$subcategories->slug) }}">
<img height="80" width="102" loading="lazy" class="img-circle sqtags" src="{{asset('public/img-category')}}/{{ $_image }}">
<div class="sidekro chapta">
<p>{{ $subcategories->name }}</p>
</div>
</a>
</li>
#endforeach
<a class="slink viewmore" href="{{ url('c', $response->category->slug) }}">
<div class="sidekro viewmore1" style="margin: 4px;">
<p>View</p>
<p>more</p>
</div>
</a>
</ul>
</div>
</div>
</div>

Duplicate classes after using foreach

I use a foreach to print out an attachment_image from a blog post. But for some reason a button duplicates the amount of posts and I dont know what I am doing wrong.
I am using sage 10 with bootstrap 5:
#php
$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
#endphp
<section class="masonry-grid">
<div class="container masonry__container">
<div class="masonry__items">
<div class="item masonry__item">
<div class="masonry__images">
#if ( empty($images) )
#else
#foreach ( array_unique($images) as $attachment_id => $attachment )
<div class="masonry__image">
{!! wp_get_attachment_image( $attachment_id, false ) !!}
</div>
#endforeach
#endif
</div>
<div class="masonry__info">
<div class="masonry__category">
{{ get_the_category( $id )[0]->name }}
</div>
<div class="masonry__title">
<a href="{{ get_permalink() }}">
{!! $title !!}
</a>
</div>
</div>
<div class="masonry__socials">
<div class="masonry__social">
<div classs="masonry__authors">
<i class="fas fa-at"></i><span class="masonry__author">#include('partials/entry-meta')</span>
</div>
<div class="masonry__views">
<i class="fas fa-eye"></i><span class="masonry__author">149 views</span>
</div>
<div class="masonry__likes">
<i class="far fa-thumbs-up"></i><span class="masonry__author">8 likes</span>
</div>
</div>
</div>
</div>
</div>
<div class="masonry__buttons">
<div class="masonry__loadmore">
<button type="button" class="masonry__button btn btn-outline-secondary"><i class="fas fa-spinner"></i>Load more</button>
</div>
</div>
</div>
</section>
I intend to display multiple items and a single button, yet, instead of the behavior I expect, as many buttons are displayed as many items there are.

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

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

Categories