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"]
Related
I want to display multiple product image from database with eager loading, and when i tried to displaying it, console said localhost:8000/storage/ 404 not found. Any ideas how to solve this? this is my first time using eager loading & relationship on Laravel. Thank you!
Here is my controller :
public function homeProduct(){
$products = Product::with('productCategory')->get();
$images = Product::with('productImage')->get();
return view('home.index', compact('products', 'images'));
}
Product Model :
public function productImage()
{
return $this->hasMany(ProductImage::class, 'product_id', 'id');
}
Product Image Model :
public function product()
{
return $this->belongsTo(Product::class, 'id');
}
Here are the index.blade.php View :
#foreach ($products as $p)
<div class="col-lg-6 col-12">
<div class="portfolio-thumb mb-5" data-aos="fade-up">
<div id="yoreBeans" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-inner">
#foreach ($images as $key => $image)
<div class="carousel-item {{ $key == 0 ? 'active' : '' }}">
<a href="{{ asset('storage/'.$image->path) }}" class="image-popup">
<img src="{{ asset('storage/'.$image->path) }}" class="img-fluid portfolio-image" alt="">
</a>
</div>
#endforeach
</div>
</div>
<div class="portfolio-info">
<h3 class="text-black">{{ $p->name }}</h3>
<h4 class="text-danger">{{ $p->productCategory->name }}</h4>
</div>
</div>
</div>
#endforeach
Your method $images = Product::with('productImage')->get(); will also return Product objects with images.
What you exactly want is to eager load both realtions and then reference it at the same time:
public function homeProduct(){
$products = Product::with(['productCategory', 'productImage'])->get();
return view('home.index', compact('products'));
}
And your view like this:
#foreach ($products as $p)
<div class="col-lg-6 col-12">
<div class="portfolio-thumb mb-5" data-aos="fade-up">
<div id="yoreBeans" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-inner">
<!-- // this line changed --->
#foreach ($p->productImage as $key => $image)
<div class="carousel-item {{ $key == 0 ? 'active' : '' }}">
<a href="{{ asset('storage/'.$image->path) }}" class="image-popup">
<img src="{{ asset('storage/'.$image->path) }}" class="img-fluid portfolio-image" alt="">
</a>
</div>
#endforeach
</div>
</div>
<div class="portfolio-info">
<h3 class="text-black">{{ $p->name }}</h3>
<h4 class="text-danger">{{ $p->productCategory->name }}</h4>
</div>
</div>
</div>
#endforeach
HTML Code
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">{{ $post->owner_user->username }}</p>
</div>
</div>
#endforeach
Laravel code
public function testpage()
{
return Posts::with(['OwnerUser','like'])->get();
}
when I just return Posts table, it has column owner_user with user information in it.
relations work, but somehow I get "Null" as a result in {{ $post->owner_user->username }} when I return it in HTML code.
because that is not a single object try
#foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">#(foreach $post->owner_user as $user){{$user->username }}#endforeach</p>
</div>
</div>
#endforeach
I currently print all categories in one view, with them I mark a difference between its subcategories
#foreach($tipos as $tipo)
<div class="card">
<h4 style="margin-top: 5px;">{{$tipo->tipo }}</h4>
</div>
#foreach($tipo->categories as $category)
<div style="margin-top: 20px;" class="col-lg-4 col-sm-6 mb-4">
<div class="card h-100">
<a href="{{ route('category-detail',['slug'=>$category->slug]) }}">
#if(!empty($category->image))
<img class="card-img-top" src="{{url($category->image)}}" alt="">
#else
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
#endif
</a>
<div class="card-body">
<h4 class="card-title">
{{$category->name}}
</h4>
<p class="card-text">{{$category->descripcion}}</p>
</div>
</div>
</div>
#endforeach
#endforeach
I get the names from:
categories table:
Now I need to create a view for each of them, enter it using url,
controller:
public function list($id){
$tipos = App\Tipo::findOrFail($id);
return view('list.tipos', compact('tipos'));
}
route:
Route::get('/list{id}','HomeController#list')->name('list.categories');
pivot table:
How can I print the content of each category? help pls
Modify your controller like this
Note: Change the Model Names (App\Pivote) and (App\Category) to what you have set in your code.
public function list($id){
$tipos = App\Tipo::findOrFail($id);
$pivot_data = App\Pivote::select('category_id')->where('tipo_id',$id)->get()->toArray();
$categories = App\Category::whereIn('id',$pivot_data)->get()->toArray();
return view('list.tipos', compact('tipos','categories'));
}
And modify your view something like this
<div class="card">
<h4 style="margin-top: 5px;">{{$tipos->tipo }}</h4>
</div>
#foreach($categories as $key => $category)
<div style="margin-top: 20px;" class="col-lg-4 col-sm-6 mb-4">
<div class="card h-100">
<a href="{{ route('category-detail',['slug'=>$category['slug']]) }}">
#if(!empty($category['image']))
<img class="card-img-top" src="{{asset($category['image'])}}" alt="">
#else
<img class="card-img-top" src="http://placehold.it/700x400" alt="">
#endif
</a>
<div class="card-body">
<h4 class="card-title">
{{$category['name']}}
</h4>
<p class="card-text">{{$category['descripcion']}}</p>
</div>
</div>
#endforeach
I'm making a discussion forum app with laravel and i face this error:
Trying to get property of non-object (View:
C:\xampp\htdocs\fourm\resources\views\discussion\show.blade.php) at
PhpEngine->evaluatePath('C:\xampp\htdocs\fourm\storage\framework\views/a2f2c494b8859e0552bfa22c40ceb4065b2efbe5.php',
array('__env' => object(Factory), 'app' => object(Application),
'channels' => object(Collection), 'errors' => object(ViewErrorBag),
'd' => null, 'best_answer' => null))in CompilerEngine.php (line 59)
This is my controller code:
public function show($slug)
{
$discussion = Discussion::where('slug', $slug)->first();
$best_answer = Reply::where('best_answer', 1)->first();
return view('discussion.show')->with('d', $discussion)->with('best_answer', $best_answer);
}
This is my view code:
#extends('layouts.app')
#section('content')
<div class="panel panel-default">
<div class="panel-heading">
<img src="{{ $d->user->avatar }}" alt="" width="40px" height="40px">
<span>{{ $d->user->name }}, <b>( {{ $d->user->points }} )</b></span>
#if($d->is_being_watch_by_user())
unwatch
#else
watch
#endif
</div>
<div class="panel-body">
<h4 class="text-center">
<b>{{ $d->title }}</b>
</h4>
<hr>
<p class="text-center">
{{ $d->content }}
</p>
<hr>
#if($best_answer)
<div class="text-center" style="padding: 40px;">
<h3 class="text-center">BEST ANSWER</h3>
<div class="panel panel-success">
<div class="panel-heading">
<img src="{{ $best_answer->user->avatar }}" alt="" width="40px" height="40px">
<span>{{ $best_answer->user->name }} <b>( {{ $best_answer->user->points }} )</b></span>
</div>
<div class="panel-body">
{{ $best_answer->content }}
</div>
</div>
</div>
#endif
</div>
<div class="panel-footer">
<span>
{{ $d->replies->count() }} Replies
</span>
{{ $d->channel->title }}
</div>
</div>
#foreach($d->replies as $r)
<div class="panel panel-default">
<div class="panel-heading">
<img src="{{ $r->user->avatar }}" alt="" width="40px" height="40px">
<span>{{ $r->user->name }} <b>( {{ $r->user->points }} )</b></span>
#if(!$best_answer)
#if(Auth::id() == $d->user->id)
Mark as best answer
#endif
#endif
</div>
<div class="panel-body">
<p class="text-center">
{{ $r->content }}
</p>
</div>
<div class="panel-footer">
#if($r->is_liked_by_user())
Unlike <span class="badge">{{ $r->likes->count() }}</span>
#else
Like <span class="badge">{{ $r->likes->count() }}</span>
#endif
</div>
</div>
#endforeach
<div class="panel panel-default">
<div class="panel-body">
#if(Auth::check())
<form action="{{ route('discussion.reply', ['id' => $d->id ]) }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="reply">Leave a reply...</label>
<textarea name="reply" id="reply" cols="30" rows="10" class="form-control"></textarea>
</div>
<div class="form-group">
<button class="btn pull-right">Leave a reply</button>
</div>
</form>
#else
<div class="text-center">
<h2>Sign in to leave a reply</h2>
</div>
#endif
</div>
</div>
#endsection
You're getting this error because both queries return null:
$discussion = Discussion::where('slug', $slug)->first();
$best_answer = Reply::where('best_answer', 1)->first();
So, you need to check for empty result manually with is_null() or empty() or just:
if (!$discussion) {
abort(404);
}
Or use findOrFail() to throw an exception if no record found.
You are trying to access property with null objects, and that's why you got the error like "Trying to get property of null object".
As per our discussion in comments, you are getting null values in both queries and that's the reason for error!
Try to add if..else condition like below:
if (!$d) {
session()->flash('error', 'Discussion not found.');
return redirect()->back();
} else {
return view('discussion.show')->with('d', $discussion)->with('best_answer', $best_answer);
}
Hope this helps you!
Make sure you're not returning an object and accessing it as an array. It can be a common error.
I solved mine by adding: <?php error_reporting(0);?> on page where i got error. :D
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