I am trying to print out the lists of payment methods using the foreach loop, however, i get an undefined variable.. error.
Here is my controller class
class HomeController extends Controller
{
//
public function index()
{
$payments = Payment::where('status', 1)->get();
return view('user.account.index', [
'payments' => $payments
]);
}
}
and here is my section in my view page(index) i want to loop through
#foreach ($payments as $payment)
<!-- {{ Cryptocap::getSingleAsset($payment->name) }} -->
<div class="card col-md-6 no-padding ">
<div class="card-body">
<div class="h1 text-muted text-right mb-4">
<i class="pe-7s-wallet"></i>
</div>
<div class="h4 mb-0">
<span class="">0.0012930403</span>
</div>
<small class="text-muted text-uppercase font-weight-bold">BTC</small>
<div class="progress progress-xs mt-3 mb-0 bg-flat-color-1" style="width: 40%; height: 5px;"></div>
</div>
</div>
#endforeach
The above approach works on other areas of my application, as this is the approach i have been using throughout the application. But it doesn't seem to work on this particular controller and view.
Can you give this a try
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
For more info visit this link
Passing data from controller to view in Laravel
Related
I am developing an e-commerce website in Laravel. On my product page, I have all my products. If I select one product that needs to direct for another page as a single product details page and that page should be loaded. In my case, when I select a product, I am directed to a single product page as well as that product's product id showed in the URL, but the page data didn't be loaded. what could be the problem?
This is my first project in laravel. If anyone can give a hand to sort it out it will be a great help.
SingleProductController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Category;
class SingleProductController extends Controller
{
public function index(Product $product)
{
$arr['product'] = $product;
return view('singleproducts')->with($arr);
}
// public function update(Request $request, Product $product)
// {
// $product-> prod_name = $request-> prod_name;
// $product-> prod_image_path = $request-> prod_image_path;
// return view('singleproducts',['product'=>$product]);
// }
public function show($id)
{
$product = Product::find($id);
return view ('singleproducts',['product'=>$product]);
}
}
singleproducts.blade.php
<form action="{{ route('singleproducts',$product->id) }}" enctype="multipart/form-data">
<div class="mb-xl-14 mb-6">
<div class="row">
<div class="col-md-5 mb-4 mb-md-0">
<div id="sliderSyncingThumb" class="js-slick-carousel u-slick u-slick--slider-syncing u-slick--slider-syncing-size u-slick--gutters-1 u-slick--transform-off"
data-infinite="true"
data-slides-show="5"
data-is-thumbs="true"
data-nav-for="#sliderSyncingNav">
<div class="js-slide">
<img class="img-fluid" src="{{asset('/storage/admin/'.$product ['prod_image_path'] ) }}" alt="Image Description">
</div>
</div>
</div>
</div>
<div class="col-md-7 mb-md-6 mb-lg-0">
<div class="mb-2">
<div class="border-bottom mb-3 pb-md-1 pb-3">
<h2 class="font-size-25 text-lh-1dot2">{{ $product ['prod_name'] }}</h2>
<div class="mb-2">
<a class="d-inline-flex align-items-center small font-size-15 text-lh-1" href="#">
<div class="text-warning mr-2">
<small class="fas fa-star"></small>
<small class="fas fa-star"></small>
<small class="fas fa-star"></small>
<small class="fas fa-star"></small>
<small class="far fa-star text-muted"></small>
</div>
</a>
</div>
</div>
</div>
</div>
</form>
This is my route
Route::get('/singleproducts', 'SingleProductController#index')->name('singleproducts');
What could be the problem?
Assuming that you have set your routes well, it may be due to a problem in your blade file. Just try it with a simple blade file and if it works you can search for a problem in your blade file.
When i try to paginate i got an exception Call to undefined method App\Clothes::links() (View: D:\work\Laravel\winter\resources\views\pages\home.blade.php).my data is view in the blade file but i am not been able to do pagination because of the above exception.please help
public function images(){
if(auth::check()){
$user = Auth::user();
$renimage = Clothes::orderBy('id','desc')->paginate(3);
$cart = cart::where('user_id',$user->id)->get();
$cart_added=count($cart);
return view('pages.home',['renimage'=>$renimage,'google_avater'=>$user,
'cart_added'=>$cart_added]);
}
}
In the blade file.
<div class="container" style="margin-top:50px;margin-bottom:50px">
<div>
#foreach ($renimage as $renimage)
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="hovereffect">
<img class="img-responsive" src="{{asset('upload/'.$renimage->img) }}" alt="">
</br>
<div class="overlay">
<h2>{{$renimage->name}}</h2>
<h3 style="color: white;padding-top: 70px;">{{'$'.$renimage->price}}</h2>
<a class="info" href="{{ route('product',['id'=>$renimage->id])}}" style="margin-top:
20px;">Buy Now</a>
</div>
</div>
</div>
#endforeach
{{$renimage->links()}}
you need to handle empty collections
#unless (count($renimage))
{{$renimage->links()}}
#endunless
Ok, so I saw similar questions asked but none of the answers helped, so forgive me if it seems like this is a duplicate.
I am following a course on Udemy(It's been over a week, the instructor hasn't answered my question, although they are active on the site, hence why I'm here), the codes I'm displaying below is how the instructor has taught us to write it.
THE ISSUE:
We are creating a blog and that blog has many categories and many posts but each post belongs to only ONE category which can hold multiple posts.
Here's a look at the model for them.
public function user(){
return $this->belongsTo('App\User');
}
public function posts(){
return $this->hasMany('App\Post');
}
POST MODEL
public function user(){
return $this->belongsTo('App\User');
}
public function category(){
return $this->belongsTo('App\Category');
}
They each have their own controller also, we also have a frontend controller that displays all the categories and when you click read more it takes you to a single page that displays the most recent post within that category.
This single page also has a pagination that supposed to display the previous post that belongs to that category.
INSTEAD: it takes the user to the most recent post regardless of what category it belongs to and display all the previous post for all category.
Below is the frontend controller
public function index()
{
return view('categories')
->with('categories',Category:orderBy('created_at', 'desc')->take(24)->get())
->with('tag', Tag::all())
->with('posttitle', Post::orderBy('created_at', 'desc')->first());
}
public function tag($id)
{
$tag = Tag::find($id);
return view('tag')->with('tag', $tag)
->with('categories', $tag->tag)
->with('categories', Category::all());
}
public function singlePage($slug)
{
$post = Post::where('slug', $slug)->first();
$next_id = Page::where('id', '>', $page->id)->min('id');
$prev_id = Post::where('id', '<', $post->id)->max('id');
return view('single')->with('post', $post)
->with('categories', Category::all())
->with('posttitle', Post::take(1)->get())
->with('next', Post::find($next_id))
->with('prev', Post::find($prev_id));
}
Here's a look at the view for the index function
<div class="container">
<div class="container-fluid">
<div class="row medium-padding120 bg-border-color">
<div class="container">
<div class="col-lg-12">
<div class="offers">
<div class="row">
<div class="col-lg-8 col-md-12 col-sm-12 col-xs-12">
<div class="heading">
<h4 class="h2 heading-title">Discover New Articles</h4>
<div class="heading-line">
<span class="short-line"></span>
<span class="long-line"></span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="case-item-wrap">
#foreach($categories as $category)
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="case-item">
<div class="case-item__thumb">
<img src="{{ $category->featured_img }}" alt="{{ $category->title }}"style="width:300px; height:280px;">
</div>
<h6 class="case-item__title text-center">{!! $category->title !!}</h6>
<!--<i class="seoicon-clock"></i>
<time class="published" datetime="2016-04-17 12:00:00">
{{ $category->created_at->diffForHumans() }}
</time>-->
<h6 style="width:300px; height:85px;">{!! str_limit($category->summary, 85) !!}
<small> Read More</small>
</h6>
</div>
</div>
#endforeach
</div>
</div>
<div class="padded-50"></div>
</div>
</div>
</div>
</div>
and this is the single page view below
<div class="content-wrapper"> <div class="container"> <div>
<main class="main">
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-1 col-md-7 col-md-offset-1 col-lg-6 col-lg-offset-3 panel panel-reading">
<article class="hentry post post-standard-details">
<h2>{!! $post->posttitle !!}</h2>
<div class="post__content">
<div class="post-additional-info">
<div class="post__author author vcard">
By:
<div class="post__author-name fn">
{{ $post->user->username}}
</div>
</div>
{{--Post date aka page date --}}
<span class="post__date">
<time class="published" datetime="2016-03-20 12:00:00">
<i class="seoicon-clock"></i> {{ $post->created_at->diffForHumans() }}
</time>
</span>
{{--End Post date aka page date--}}
</div>
{{--Post content aka page content--}}
<div class="post__content-info">
<p> {!! $post->content !!} </p>
</div>
{{--End post content aka page content--}}
</div>
<div class="pagination-arrow">
{{--Left arrow--}}
#if($prev)
<a href="{{ route('post.single', ['slug' => $prev->slug] )}}" class="btn-prev-wrap">
<svg class="btn-prev">
<use xlink:href="#arrow-left"></use>
</svg>
<div class="btn-content">
<div class="btn-content-title">Previous Page</div>
<p class="btn-content-subtitle">{{ $prev->posttitle}}</p>
</div>
</a>
#endif
{{--Right arrow--}}
#if($next)
<a href="{{ route('post.single', ['slug'=>$next->slug]) }}" class="btn-next-wrap">
<div class="btn-content">
<div class="btn-content-title">Next Page</div>
<p class="btn-content-subtitle">{{ $next->posttitle }}</p>
</div>
<svg class="btn-next">
<use xlink:href="#arrow-right"></use>
</svg>
</a>
#endif
</div>
</article>
</div>
</main>
HERE'S the post database
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('posttitle');
$table->string('slug');
$table->text('content');
$table->softDeletes();
$table->timestamps();
});
}
IN CONCLUSION:
Using this how would you filter it so that only the post that has the same category_id is displayed when the user clicks on that category?
Thank you
As a bonus I did a short screen recording of the problem https://www.screencast.com/t/vVzRMSunH
Sorry, I cant repeat all your codes but if I well understand you, you want to be able to show on a single page, all posts belonging to a specifique category. If it is, the controller who generate view should be like this. Question of organisation, I prefer to have a specific Controller for all action about category model.
/**
* Show all posts of category $id
*
* #param int $id : Id of category mapped via route
*/
public function show($id)
{
$category = Category::findOrFail($id);
$posts = $category->posts()->paginate();
return view('category.show', compact('posts'));
}
And your resources/views/category/show.blade.php can be like this
#extends('layouts.app')
#section('content')
#foreach($posts as $post)
<h2>{{ $post->title }}</h2>
{{-- and you can show more informations about post here --}}
#endforeach
{{-- pagination links --}}
{!! $posts->links() !!}
#stop
I am newbie for laravel and doing my first project called blog. For post article i can fetch data but I am trying to display reader's comment in the index page just below the article by fetching from database but it gives error(For info,I have already inserted a row from xammp just to fetch).Here is the code for PostController.php
public function index()
{
//$show = Post::all();
$show = Post::orderBy('id','desc')->paginate(1);
return view('pages/blog')->with('post',$show);
}
public function comment()
{
$show = readerComment::all();
return view('pages/blog')->with('commentShow',$show);
}
In index pages or blog.blade.php
#if(count($post)>0)
#foreach($post as $article)
<div class = "row">
<div class="col-md-12">
<h3 class="text-center">{{$article->title}}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{!!$article->article!!}</p>
</div>
</div>
<!-- Comment section -->
<div class = "comment">
<h3>Comments</h3>
#foreach($commentShow as $commShow)
<div class = "row">
<div class = "col-md-12">
<p>{{$commShow->comment}}</p>
<p>{{$commShow->name}}</p>
</div>
</div>
#endforeach
</div>
And in web route
Route::resource('posts','PostController');
Route::get('/','PostController#comment');
I get error as
Undefined variable: post (View: C:\xampp\htdocs\blogging\resources\views\pages\blog.blade.php)
Any help would be appreciated. Thanks
Do it like that,
public function index()
{
$posts = Post::orderBy('id','desc')->paginate(1);
return view('pages.blog',compact('posts'));
}
You're blade looks good without error, but let's try this view..
#extends('layouts.app')
#section('content')
#if(count($post)>0)
#foreach($post as $article)
<div class = "row">
<div class="col-md-12">
<h3 class="text-center">{{$article->title}}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{!!$article->article!!}</p>
</div>
</div>
<!-- Comment section -->
<div class = "comment">
<h3>Comments</h3>
#foreach($post as $commShow)
<div class = "row">
<div class = "col-md-12">
<p>{{$commShow->comment}}</p>
<p>{{$commShow->name}}</p>
</div>
</div>
#endforeach
</div>#endsection
Here you are defining your Eloquent Collection of models to be $commentShow:
return view('pages/blog')->with('commentShow',$show);
In your view, you are using the variable $post.
You need to change one of them to match the other.
in addition to this:
When you use the ->with() method the first parameter passed to it is
the name of the variable available on the view. So for the comment
part you make this call ->with('commentShow',$show); yet in your view
you try and access it via $post. Change this line #foreach($post as
$commShow) to #foreach($commentShow as $commShow)
change you view code to this: (please note the changed variable name in second #foreach)
#if(isset($post) && !empty($post))
#foreach($post as $article)
<div class="row">
<div class="col-md-12">
<h3 class="text-center">{{$article->title}}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{!!$article->article!!}</p>
</div>
</div>
#endforeach
#endif
#if(isset($commentShow) && !empty($commentShow))
<!-- Comment section -->
<div class="comment">
<h3>Comments</h3>
#foreach($commentShow as $commShow)
<div class="row">
<div class="col-md-12">
<p>{{$commShow->comment}}</p>
<p>{{$commShow->name}}</p>
</div>
</div>
#endforeach
</div>
#endif
I'm making a forum and the if the user clicks on a theme, he gets redirected to the page where all the topics are that belong to that theme. What I'm trying to do, is that the user is able to click on the topic and gets redirected to it.
The problem is, is that I get an error saying Undefined variable: theme
As you can see in the code below, I have a foreach loop in my theme.blade.php which loops all the topics that belong to the theme he/she clicked on. In the beginning of the loop there is a <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}"1 which is supposed to bring the user to the topic that he/she clicked on. The URL that is supposed to show is forum.dev/theme/{theme_id}/topics/{topic_id} But that doesn't work.
And it gives me that error code. All the code is below, If i miss some code, Please inform me about it and i'll update the question. Thanks in advance
theme.blade.php
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 125, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Posted by: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Last reply</h6>
<p class="center-align"></p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{theme_id}/topics', 'ThemesController#show')->name('showtheme');
Route::get('/theme/{theme_id}/topics/{topic_id}', 'TopicsController#topic')->name('showtopic');
Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function() {
//THEMES
Route::get('/theme/{theme_id}/edit', 'ThemesController#edit')->name('edittheme');
Route::patch('/theme/{theme_id}/edit', 'ThemesController#update')->name('updatetheme');
Route::get('/theme/create', 'ThemesController#create')->name('createtheme');
Route::post('/theme/create', 'ThemesController#save')->name('savetheme');
Route::delete('/theme/{theme_id}/delete', 'ThemesController#destroy')->name('deletetheme');
//TOPICS
Route::get('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#edit')->name('edittopic');
Route::patch('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController#update')->name('updatetopic');
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController#save')->name('savetopic');
Route::delete('/theme/{theme_id}/topics/{topic_id}/delete', 'TopicsController#destroy')->name('deletetopic');
});
Route::get('user/profile', 'UserController#profile')->name('showprofile');
Route::post('user/profile', 'UserController#update_avatar');
TopicsController.php (Show method)
public function show($id)
{
$theme = Theme::find($id);
$topics = Topic::find($id);
return view('topics/topic')->with('topics', $topics)->with('theme', $theme);
}
I don't know if you can use the double '->with' option.
What will work is the following:
return view('topics/topic', ['topics' => $topics, 'theme'=> $theme]);
You pass an array as the second argument of the view. You can add as many keys and variables as you like.