Display data in the blade from the database - php

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>

Related

show single page by using id

I want to go to specific page after click show button but after click show will get this error Undefined offset: 1
web.php
Route::get('/product/{id}', 'ProductController#show')->name('product.show');
ProductController.php
public function show($id)
{
$product = Product::find($id);
return view('product.show')->with(compact('product'));
}
home.blade.php
<div class="row">
#foreach($products as $product)
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
<div class="card-body">
<h4 class="card-title">
{{ $product->name }}
</h4>
<h5>{{ $product->price }}</h5>
<p class="card-text">{{ $product->description }}</p>
<td><a class="btn btn-primary" href="{{ route('product.show', $product->id ) }}">Show</a></td>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
#endforeach
</div>
Your are doing wrong in blade file. Note here php array start from 0. So array is [0,1,2,3] and database id is start from 1.
this is the correct link
{{ route('product.show', ['Product' => $product->id]) }}
You have to use like below code
{{ route('product.show', ['id' => $product->id]) }}
return view('product.show', compact('product'));
And in your show.blade.php you have to use like below
{{$product->id}} or {{$product->name}} //etc //But not use foorloop
The find method will return only one item. You can't use the loop on it.
Try the following code.
web.php
Route::get('/product/{product}', 'ProductController#show')->name('product.show');
ProductController.php
public function show(Product $product)
{
return view('product.show')->with(compact('product'));
}
home.blade.php
<div class="row">
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
<div class="card-body">
<h4 class="card-title">
{{ $product->name }}
</h4>
<h5>{{ $product->price }}</h5>
<p class="card-text">{{ $product->description }}</p>
<td><a class="btn btn-primary" href="{{ route('product.show', $product->id ) }}">Show</a></td>
</div>
<div class="card-footer">
<small class="text-muted">★ ★ ★ ★ ☆</small>
</div>
</div>
</div>
</div>
you are in wrong blade
you are returning product.show blade in your controller:
return view('product.show')->with(compact('product'));
but you put home.blade code!!!
put the right blade code
show.php: Why not try it $_ Get["id"]

Laravel 8 using group by in view/blade by foreach

I have this code in my controller:
public function videoGallery()
{
$videoLinksRecord = VideoLinks::all()->sortBy('video_category_id')->groupBy('video_catetgory_id');
return view('video.gallery')->with('videoLinksRecord', $videoLinksRecord);
}
which return this query:
{
"1": [
{"id":1,"video_category_id":1,"name":"Laszlo - Supernova","link":"PKfxmFU3lWY","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":2,"video_category_id":1,"name":"Subtact - Restart","link":"M5A46A2txC4","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":3,"video_category_id":1,"name":"Sushi Killer & Kevin Villeco - Anime Bae","link":"YHHjfYxLyuA","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":4,"video_category_id":1,"name":"Hyper Potions & Subtact - Adventures","link":"TKZUhs9Gcdo","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:53.000000Z","deleted_at":null}
],
"2": [
{"id":5,"video_category_id":2,"name":"Laszlo - Supernova","link":"PKfxmFU3lWY","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":6,"video_category_id":2,"name":"Subtact - Restart","link":"M5A46A2txC4","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null},
{"id":7,"video_category_id":2,"name":"Sushi Killer & Kevin Villeco - Anime Bae","link":"YHHjfYxLyuA","description":"one loud banger!","home_video":null,"created_at":"2020-12-31T14:12:13.000000Z","updated_at":"2020-12-31T14:12:13.000000Z","deleted_at":null}
]
}
I want to display the result grouped by its video category similar to this blade:
{{-- name of the category --}}
<h2 class="poppins-medium text-lg text-darkergreen text-center">Video Category Name Here</h2>
<div class="container-fluid">
<div class="row">
<div class="col-4"></div>
<div class="col-4 borderline-darkergreen-md"></div>
<div class="col-4"></div>
</div>
</div>
{{-- loop for each result sharing the same "video_category_id" --}}
<div class="container-fluid">
<div class="row d-flex justify-content-center px-0">
<div class="col col-lg-2 d-none d-lg-block d-xl-none"></div>
<div class="col col-xl-2 d-none d-xl-block"></div>
{{-- video --}}
<div class="col-12 col-md-6 col-lg-4 container-fluid d-flex justify-content-center spacer-md">
<div class="row d-flex justify-content-center">
<div class="col-12 d-flex justify-content-center container-fluid px-0">
{{-- video border --}}
<div class="video-box-border position-relative spacer-md">
<a href="">
{{-- video card image --}}
<div class="video-image position-absolute"></div>
{{-- video card background --}}
<div class="video-card"></div>
</a>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-2 px-0"></div>
<div class="col-8 borderline-darkergreen-sm px-0"></div>
<div class="col-2 px-0"></div>
</div>
</div>
{{-- video name--}}
<p class="poppins-normal text-md text-gray text-center">Video Name Here
</p>
</div>
</div>
</div>
</div>
How should I use the $videoLinksRecord in a foreach logic to display it by its groupBy logic? This is my first time grouping query result so I do not know how to construct the logic for the said desired result.
Edit the code below is the relationship code in the model:
VideoLinks Model:
public function categories()
{
return $this->hasMany(VideoCategories::class);
}
VideoCategories Model:
public function links()
{
return $this->belongsTo(VideoLinks::class);
}
#foreach ($videoLinksRecords as $video_category_id => $videoLinks)
{{ $video_category_id }}
#foreach ($videoLinks as $videoLink)
{{ $videoLink->id }}
{{ $videoLink->video_category_id }}
{{ $videoLink->name }}
{{ $videoLink->link }}
{{ $videoLink->description }}
{{ $videoLink->home_video }}
{{ $videoLink->created_at }}
{{ $videoLink->updated_at }}
{{ $videoLink->deleted_at }}
#endforeach
#endforeach
Your relationship between the VideoLinks and VideoCategory models should be as follows
# VideoLinks model
public function category()
{
// 2nd parameter is optional if you follow the naming conventions
return $this->belongsTo(VideoCategory::class, 'video_category_id');
}
In your query eager load the relationship using with('category') but then group by the category's name and finally replace all() with get().
$videoLinksRecord = VideoLinks::with('category')->get()->sortBy('video_category_id')->groupBy('category.name');
#foreach ($videoLinksRecords as $category_name => $videoLinks)
{{ $category_name }}
#foreach ($videoLinks as $videoLink)
...

Laravel - Undefined variable: articles

public function press (Request $request)
{
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
}
return view('pages.press.index', compact('articles'));
}
I'm receiving an error Undefined variable: articles. Above is the code I'm using in my controller. What is the error that I'm making?
VIew
#if(!empty($articles))
#foreach ($articles as $article)
<div class="col-md-4">
<div class="box border">
<div class="box-header">
<img src="{{ url('http://assets.hobohweb.com/uploads/press/'.$article->image) }}" class="img-fluid" alt="">
</div>
<div class="box-body">
<h6 class="h-2x">{{ $article->title }}</h6>
{{ Carbon\Carbon::parse($article->created_at)->diffForHumans() }} on {{ $article->source }}
</div>
</div>
</div>
#endforeach
#endif
Just check if there are any articles in your search, like:
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
} else {
$articles = [];
}
And use it in the view:
#forelse($articles as $article)
...
#empty
<p>There are no articles!</p>
#endforelse
You are declaring the variable inside the if, but if the condition is false, the variable is never set. Try to use an else condition:
public function press (Request $request)
{
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
} else {
$articles = null;
}
return view('pages.press.index', compact('articles'));
}
You will also need to check if $articles is null in your view.
that's because the variable is never assigned a value and does not exist in the external context of the -if
try to use this
public function press (Request $request)
{
$articles = array();
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
}
return view('pages.press.index', [
'articles' => $articles
]);
}
View
#foreach ($articles as $article)
<div class="col-md-4">
<div class="box border">
<div class="box-header">
<img src="{{ url('http://assets.hobohweb.com/uploads/press/'.$article->image) }}" class="img-fluid" alt="">
</div>
<div class="box-body">
<h6 class="h-2x">{{ $article->title }}</h6>
{{ Carbon\Carbon::parse($article->created_at)->diffForHumans() }} on {{ $article->source }}
</div>
</div>
</div>
#endforeach
I hope this help you

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

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