Hello guys! I am running with two problem in my system.. I installed Laravel 5.4 and followed the Blog tutorial step by step. When I login and go to the link posts, I see all posted posts but if I press the edit button, I get the following error: 2/2 ErrorException in Builder.php line 2508: Call to undefined method Illuminate\Database\Query\Builder::getRelatedIds() (View: C:\wamp64\www\GOLITOnuevo\resources\views\posts\edit.blade.php)..
1/2 BadMethodCallException in Builder.php line 2508: Call to undefined method Illuminate\Database\Query\Builder::getRelatedIds()..
When I go the link blog in the main menu. If I press the button see more, I get the following errors: 2/2 ErrorException in a00eaacb888ecdb0fab13fde293184ce85a468a8.php line 1: Trying to get property of non-object (View: C:\wamp64\www\GOLITO1\resources\views\blog\single.blade.php) 1/2ErrorException in a00eaacb888ecdb0fab13fde293184ce85a468a8.php line 1: Trying to get property of non-object..
This is the code for the BlogController:..
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Post;
use App\Tag;
use App\Category;
use Session;
use Purifier;
use Image;
class PostController extends Controller
{
public function __construct() {
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::orderBy('id', 'desc')->paginate(10);
return view('posts.index')->withPosts($posts);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$categories = Category::all();
$tags = Tag::all();
return view('posts.create')->withCategories($categories)->withTags($tags);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// validate the data
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
'category_id' => 'required|integer',
'body' => 'required'
// 'featured_image' => 'sometimes|image'
));
// store in the database
$post = new Post;
$post->title = $request->title;
$post->slug = $request->slug;
$post->category_id = $request->category_id;
$post->body = Purifier::clean($request->body);
if ($request->hasFile('featured_image')) {
$image = $request->file('featured_image');
$filename = time() .'.' .$image->getClientOriginalExtension();
$location = public_path('images/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$post->image = $filename;
}
$post->save();
$post->tags()->sync($request->tags, false);
Session::flash('success', 'The blog post was successfully saved!');
return redirect()->route('posts.show', $post->id);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$post = Post::find($id);
return view('posts.show')->withPost($post);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
// find the post in the database and save as a var
$post = Post::find($id);
$categories = Category::all();
$categories = array();
foreach ($categories as $category) {
$categories[$category->id] = $category->name;
}
$tags = Tag::all();
$tags2 = array();
foreach ($tags as $tag) {
$tags2[$tag->id] = $tag->name;
}
// return the view and pass in the var we previously created
return view('posts.edit')->withPost($post)->withCategories($categories)->withTags($tags2);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
// Validate the data
$post = Post::find($id);
if ($request->input('slug') == $post->slug) {
$this->validate($request, array(
'title' => 'required|max:255',
'category_id' => 'required|integer',
'body' => 'required'
));
} else {
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
'category_id' => 'required|integer',
'body' => 'required'
));
}
// Save the data to the database
$post = Post::find($id);
$post->title = $request->input('title');
$post->slug = $request->input('slug');
$post->category_id = $request->input('category_id');
$post->body = Purifier::clean($request->input('body'));
$post->save();
if (isset($request->tags)) {
$post->tags()->sync($request->tags);
} else {
$post->tags()->sync(array());
}
// set flash data with success message
Session::flash('success', 'This post was successfully saved.');
// redirect with flash data to posts.show
return redirect()->route('posts.show', $post->id);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post = Post::find($id);
$post->tags()->detach();
$post->delete();
Session::flash('success', 'The post was successfully deleted.');
return redirect()->route('posts.index');
}
}
I will attach the code for Blog/index.blade.php as well..
@extends('main')
@section('title', '| Blog')
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Blog</h1>
</div>
</div>
@foreach ($posts as $post)
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h2>{{ $post->title }}</h2>
<h5>Published: {{ date('M j, Y', strtotime($post->created_at)) }}</h5>
<p>{{ substr(strip_tags($post->body), 0, 250) }}{{ strlen(strip_tags($post->body)) > 250 ? '...' : "" }}</p>
<a href="{{ route('blog.single', $post->id) }}" class="btn btn-success">Read More</a>
<hr>
</div>
</div>
@endforeach
<div class="row">
<div class="col-md-12">
<div class="text-center">
{!! $posts->links() !!}
</div>
</div>
</div>
@endsection
This is the code in the single.blade.php
@extends('main')
<?php $titleTag = htmlspecialchars($post->title); ?>
@section('title', "| $titleTag")
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<img src="{{ asset('images/' .$post->image) }}" height="400" width="800"/>
<h1>{{ $post->title }}</h1>
<p>{!! $post->body !!}</p>
<hr>
<p>Posted In: {{ $post->category->name }}</p>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h3 class="comments-title"><span class="glyphicon glyphicon-comment"></span> {{ $post->comments()->count() }} Comments</h3>
@foreach($post->comments as $comment)
<div class="comment">
<div class="author-info">
<img src="{{ "https://www.gravatar.com/avatar/" . md5(strtolower(trim($comment->email))) . "?s=50&d=monsterid" }}" class="author-image">
<div class="author-name">
<h4>{{ $comment->name }}</h4>
<p class="author-time">{{ date('F nS, Y - g:iA' ,strtotime($comment->created_at)) }}</p>
</div>
</div>
<div class="comment-content">
{{ $comment->comment }}
</div>
</div>
@endforeach
</div>
</div>
<div class="row">
<div id="comment-form" class="col-md-8 col-md-offset-2" style="margin-top: 50px;">
{{ Form::open(['route' => ['comments.store', $post->id], 'method' => 'POST']) }}
<div class="row">
<div class="col-md-6">
{{ Form::label('name', "Name:") }}
{{ Form::text('name', null, ['class' => 'form-control']) }}
</div>
<div class="col-md-6">
{{ Form::label('email', 'Email:') }}
{{ Form::text('email', null, ['class' => 'form-control']) }}
</div>
<div class="col-md-12">
{{ Form::label('comment', "Comment:") }}
{{ Form::textarea('comment', null, ['class' => 'form-control', 'rows' => '5']) }}
{{ Form::submit('Add Comment', ['class' => 'btn btn-success btn-block', 'style' => 'margin-top:15px;']) }}
</div>
</div>
{{ Form::close() }}
</div>
</div>
@endsection
This is the code for Post/edit.blade.php..
@extends('main')
@section('title', '| Edit Blog Post')
@section('stylesheets')
{!! Html::style('css/select2.min.css') !!}
<script src="//cloud.tinymce.com/stable/tinymce.min.js"></script>
<script>
tinymce.init({
selector:'textarea',
plugins: 'link code'
});
</script>
@endsection
@section('content')
<div class="row">
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
<div class="col-md-8">
{{ Form::label('title', 'Title:') }}
{{ Form::text('title', null, ["class" => 'form-control input-lg']) }}
{{ Form::label('slug', 'Slug:', ['class' => 'form-spacing-top']) }}
{{ Form::text('slug', null, ['class' => 'form-control']) }}
{{ Form::label('category_id', "Category:", ['class' => 'form-spacing-top']) }}
{{ Form::select('category_id', $categories, null, ['class' => 'form-control']) }}
{{ Form::label('tags', 'Tags:', ['class' => 'form-spacing-top']) }}
{{ Form::select('tags[]', $tags, null, ['class' => 'form-control select2-multi', 'multiple' => 'multiple']) }}
{{ Form::label('featured_image', 'Update Featured Image:') }}
{{ Form::file('featured_image') }}
{{ Form::label('body', "Body:", ['class' => 'form-spacing-top']) }}
{{ Form::textarea('body', null, ['class' => 'form-control']) }}
</div>
<div class="col-md-4">
<div class="well">
<dl class="dl-horizontal">
<dt>Created At:</dt>
<dd>{{ date('M j, Y h:ia', strtotime($post->created_at)) }}</dd>
</dl>
<dl class="dl-horizontal">
<dt>Last Updated:</dt>
<dd>{{ date('M j, Y h:ia', strtotime($post->updated_at)) }}</dd>
</dl>
<hr>
<div class="row">
<div class="col-sm-6">
{!! Html::linkRoute('posts.show', 'Cancel', array($post->id), array('class' => 'btn btn-danger btn-block')) !!}
</div>
<div class="col-sm-6">
{{ Form::submit('Save Changes', ['class' => 'btn btn-success btn-block']) }}
</div>
</div>
</div>
</div>
{!! Form::close() !!}
</div> <!-- end of .row (form) -->
@stop
@section('scripts')
{!! Html::script('js/select2.min.js') !!}
<script type="text/javascript">
$('.select2-multi').select2();
$('.select2-multi').select2().val({!! json_encode($post->tags()->getRelatedIds()) !!}).trigger('change');
</script>
@endsection
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community