Laravel Commenting System Only Working on most recent Post - php

I recently created a commenting system for a blog. When there is one post in the blog, the commenting sytem works fine. However, when there are two posts on the blog, and I comment on the FIRST post, the comment shows up on the SECOND post. Similarily, if I add another post, so there are three posts in my blog, when I comment on the first post, the comment shows up under the third post. In the same situation, when I comment on the second post, the comment shows up under the third post. Here is my code:
/index.blade.php
#section('content')
<div class="col-md-8 col-md-offset-2">
<hr>
#foreach($posts as $post)
<h3>{{ $post->title}}</h3>
<p>by {{ $post->user->name }}</p>
<img src='{{ asset($post->image_path) }}' class="img-responsive" id="blogpic"/>
<p>{{ Str::words($post->body) }}</p>
<p>Read More...</p>
<hr>
{{ Form::open(array('action' => 'CommentController#newComment')) }}
{{ Form::hidden('post_id', $post->id) }}
<div class="row">
<div class="col-md-10">
{{ Form::text('body', null, array('class'=>'form-control')) }}
</div>
<div class="col-md-2">
{{ Form::submit('Post', array('class'=>'btn btn-info')) }}
</div>
</div>
<br>
#foreach($post->comments as $comment)
<div class="comment">
<p><span class="bold">{{ $comment->user->name }}</span>: {{ $comment->body }}</p>
</div>
#endforeach
#endforeach
</div>
#stop
Here is my new comment controller:
public function newComment()
{
$comment = New Comment();
$comment->user_id = Auth::user()->id;
$comment->post_id = $post_id = Input::get('post_id');
$comment->body = Input::get('body');
Post::find($post_id)->comments()->save($comment);
return Redirect::action('BlogController#index');
}
I am pretty sure the problem is in my index file, and I can provide more code if needed. My comment only seems to get the most recent id in this line
{{ Form::hidden('post_id', $post->id) }}
What could be going wrong?
Thanks in advance

Related

I'm working on a project in Laravel and I'm facing an Undefined variable $post error in post-card.blade.php

post.blade.php
The main post file
<main class="max-w-6xl mx-auto mt-6 lg:mt-20 space-y-6">
<x-post-featured-card :post="$posts[0]" />
<div class="lg:grid lg:grid-cols-2">
#foreach ($posts->skip(1) as $post)
<x-post-card :post="$post" />
#endforeach
</div>
</main>
post-card.blade.php
This file is throwing the undefined variable $post error
<div class="mt-4">
<h1 class="text-3xl">
{{ $post->title }}
</h1>
</div>
#props(['post'])
#foreach($posts as $post)
<div class="mt-4">
<h1 class="text-3xl">
{{ $post->title }}
</h1>
</div>
#endforeach
You can try a few steps
change the name of the file of post-card.blade.php
remove the other commented component from the file post-card.blade.php if have any
use props in the file that give error, like #props(['post'])

Missing required parameters for [Route: ticket.edit] [URI: ticket_ads/edit/{ad}]

I am trying to edit a record in a table. I have created a route and the form, but I can't get past this error. I have figured out the problem but I can't find a fix. Am I correct in thinking that the edit.blade.php file needs the $ad->id passing?
The $ad->id is an ID of a specific add in a List View. The list view has all the tickets displayed from a table, and the below link is meant to edit that one item.
The edit route is accessed using following code:
Edit
I have one route that is supposed to open up the edit view form:
Route::get('/ticket_ads/edit/{ad}', 'TicketAdsController#editTicketAdForm')->name('ticket.edit');
The above route points to this in the controller:
public function editTicketAdForm($id)
{
//$ad = DB::table('ticket_ads')->where('id', $id)->value('id');
return view('Ads.edit')->with('id', $id);
}
This is the view called by the above function:
#extends('Shared.Layouts.MasterWithoutMenus')
#section('title')
Edit a ticket ad
#stop
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h2>Edit your ticket ad</h2></div> <br/>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title', Input::old('title'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('comment', 'Comment') }}
{{ Form::text('comment', Input::old('comment'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
#endsection
This is the line that throws the error
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
The ID displays normally in the URL as ticket_ads/edit/7 for example.
How do I get past this?
Change this line:
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
to this:
{{Form::open(array('route' => array('ticket.edit', $id)))}}
This
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
is wrong. Correct syntax is:
{{ Form::open(['route' => ['ticket.edit', $id]]) }}
also you should safely ditch using array() in favor of [] syntax as Laravel requires PHP 5.4+ anyway (unless you are using ancient version of Laravel, like v4?)
The correct syntax for calling route is.
{{ route('/cardetails', ['121','cars'] ) }}
In URL it will be like this below line.
127.0.0.1:8000/cardetails/121/cars

Echoing data if it exists

I want to display default data if the table is empty. I searched in the laravel documentation and found this.
In my controller i send data to my show.blade.php
public function show($id)
{
//Get profile data from the user
$user = User::find($id);
return view( 'gebruiker/show',[
'user' => $user,
] );
}
And in my blade I display the data
#foreach ($user->profile as $profile)
<div class="col-md-12 col-xs-12" align="center">
<h1>{{ $profile->user->name or 'Geen naam' }}
</h1>
<h3>{{ $profile->age or 'Geen leeftijd' }}</h3>
<span>{{ $profile->country or 'Geen land' }}</span>
</div>
<div class="col-md-6 col-xs-6 follow line" align="center">
<h3>
Bezoekers <br/> <span>{{ $profile->visitors or 'Geen bezoekers' }}</span>
</h3>
</div>
<div class="col-md-12 col-xs-12 login_control text-center">
<br>
{{ $profile->profile or 'Geen beschrijving' }}
</div>
#endforeach
When $profile->age is not empty it shows data but when there is no data it just shows nothing. What am I doing wrong?
In blade "or" checks only if variable exists.
Lest try something like that:
#if(!empty($profile->age))
{{ $profile->age }}
#else
Geen leeftijd
#endif
You can also try this:
#if($profile->age !='' && $profile->age !='0')
{{ $profile->age }}
#else
Geen leeftijd
#endif
Depending on your needs you can create an accessor in your model:
public function getAgeAttribute($value)
{
return !empty($value) ? $value : 'Geen leeftijd';
}
Ideally this should live in a presenter. Too bad {{ $profile->age or 'Geen leeftijd' }} doesn't work, it should. Probably someone will do a PR for it soon enough.

Display RSS feed with Laravel PHP using SimpleXML

I want to display a RSS feed, keeping it as simple as possible.
I am using Laravel 4.
Here is my controller :
public function getHome()
{
$content = file_get_contents('http://rss.leparisien.fr/leparisien/rss/paris-75.xml');
$flux = new SimpleXmlElement($content);
return View::make('frontend/homepage', compact('flux'));
}
And here is my view :
#foreach ($flux as $flu)
<article class="entry-item">
<img src="{{utf8_decode((string)$flu->item->enclosure['url'])}}" alt="">
<div class="entry-content">
{{ $flu->item->title }}
{{ $flu->item->description }}
</div>
</article>
#endforeach
This is working fine, but only one article (item) is shown.
I tried to find a way to get several items displayed with SimpleXMLElement::attributes
I can't find any clear tutorial about it.
try this
#foreach ($flux[0]->item->link as $item)
<article class="entry-item">
<img src="{{utf8_decode((string)$item->enclosure['url'])}}" alt="">
<div class="entry-content">
{{ $item->title }}
{{ $item->description }}
</div>
</article>
#endforeach
because you have mutliple items
#foreach ($flux->channel->item as $flu)
<article class="entry-item">
<img src="{{utf8_decode((string)$flu->enclosure['url'])}}" alt="">
<div class="entry-content">
{{ $flu->title }}
{{ $flu->description }}
</div>
</article>
#endforeach

Blog posts from category isn't displayed

I've created a page and then put this code
<section class="cont_pad">
<div class="container_12">
<article class="grid_8">
{{ blog:posts limit="5" offset="5" category="adultos" }}
<section class="post">
{{ if imagen_portada }}
<div class="postimg"><img src="{{ url:site }}files/thumb/{{ imagen_portada.id }}/610/220" class="pic2" alt="{{title}}" title="{{title}}"/></div>
{{ endif }}
<div class="entry-date">
<div class="posttime">
<h3>{{ helper:date timestamp=created_on }}</h3>
</div>
<div class="entry-utility">
{{ asset:image file="blog/icon1.png" }} {{ user:display_name user_id=author_id }}
<br/>
{{ if category }}
<span>{{ asset:image file="blog/icon2.png" }} {{ category:title }}</span>
{{ endif }}
{{ if keywords }}
<span>{{ asset:image file="blog/icon2.png" }} {{ keyword }}</span>
{{ endif }}
</div>
</div>
<div class="entry-text">
<h3 class="posttitle">{{ title }}</h3>
<div class="entry-content">
{{ intro }}
<p>{{ helper:lang line="blog:read_more_label" }}</p>
</div>
</div>
</section>
{{ /blog:posts }}
{{ pagination }}
</article>
<article class="grid_4 last-col">
<aside id="sidebar">
{{ widgets:area slug="widgets_blog_adultos" }}
</aside>
</article>
</div>
</section>
inside to show posts from category "adultos" but I'm not getting nothing. I have one post and it's LIVE in this category. What is wrong? I read Blog Plugin Docs also check the Blog Plugin Code at package "PyroCMS\Core\Modules\Blog\Plugins" and can't find where it fails. Also and related to this same question, can I paginate trough all the posts? I need some help here because I can't find what is wrong
Is the timezone on the computer set up wrong? If PyroCMS thinks the "Publish Date" is in the future it wont show it on the frontend.

Categories