oggiesutrisna
oggiesutrisna

Reputation: 19

SOLVED! Call to undefined method App\Category::posts() Laravel

i want to create some CMS project with the laravel, when the user trying to click the categories, the app will show the post that only shows the category that user clicked. When the user click the categories, the app says error Bad Method Call. What should i do? Any help will be appreciated.

Here's the code from web.php

<?php

use App\Http\Controllers\Blog\PostsController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', 'WelcomeController@index')->name('welcome');
Route::get('blog/posts/{post}', [PostsController::class, 'show'])->name('blog.show');
Route::get('blog/categories/{category}', [PostsController::class, 'category'])->name('blog.category');
Route::get('blog/tags/{tag}', [PostsController::class, 'tag'])->name('blog.tag');

Auth::routes();

Route::middleware(['auth'])->group(function () {
    Route::get('/home', 'HomeController@index')->name('home');
    Route::resource('categories', 'CategoriesController');
    Route::resource('posts','PostsController');
    Route::resource('tags','TagsController');
    Route::get('trashed-posts','PostsController@trashed')->name('trashed-posts.index');
    Route::put('restore-post/{post}', 'PostsController@restore')->name('restore-posts');
});

Route::middleware(['auth', 'admin'])->group(function () {
    Route::get('users/profile', 'UserController@edit')->name('users.edit-profile');
    Route::put('users/profile', 'UserController@update')->name('users.update-profile');
    Route::get('users','UserController@index')->name('users.index');
    Route::post('users/{user}/make-admin', 'UserController@makeAdmin')->name('users.make-admin');
});

Here's the code from PostsController.php

<?php

namespace App\Http\Controllers\Blog;

use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
use App\Tag;
use App\Category;


class PostsController extends Controller
{
    public function show(Post $post) {
        return view('blog.show')->with('post', $post);
    }

    public function category(Category $category) {
        return view('blog.category')
        ->with('category', $category)
        ->with('posts', $category->posts()->simplePaginate())
        ->with('categories', Category::all())
        ->with('tags', Tag::all());
    }

    public function tag(Tag $tag) {
        return view('blog.tag')
        ->with('tag', $tag)
        ->with('posts', $tag->posts()->simplePaginate(3));
    }
}

Here's the code from App\Category.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
   protected $fillable = ['name'];

   public function post() {
      return $this->hasMany(Post::class);
   }
}

Here's the code from category.blade.php

@extends('layouts.blog')


@section('title')
Category {{ $category->name }}
@endsection

@section('header')
<header class="header text-center text-white" style="background-image: linear-gradient(-225deg, #5D9FFF 0%, #B8DCFF 48%, #6BBBFF 100%);">
    <div class="container">

      <div class="row">
        <div class="col-md-8 mx-auto">

          <h1>{{ $category->name }}</h1>
          <p class="lead-2 opacity-90 mt-6">Read and get updated on how we progress</p>

        </div>
      </div>

    </div>
  </header><!-- /.header -->
@endsection

@section('content')
  <main class="main-content">
    <div class="section bg-gray">
      <div class="container">
        <div class="row">


          <div class="col-md-8 col-xl-9">
            <div class="row gap-y">

              @forelse($posts as $post)
                  <div class="col-md-6">
                      <div class="card border hover-shadow-6 mb-6 d-block">
                        <a href="{{ route('blog.show',$post->id) }}"><img class="card-img-top" src="{{ asset($post->image) }}" alt="Card image cap"></a>
                        <div class="p-6 text-center">
                          <p>
                              <a class="small-5 text-lighter text-uppercase ls-2 fw-400" href="#"></a>
                                  {{ $post->category->name }}
                              </p>
                          <h5 class="mb-0">
                              <a class="text-dark" href="{{ route('blog.show',$post->id) }}">
                                  {{ $post->title }}
                              </a>
                          </h5>
                        </div>
                      </div>
                    </div>
                    @empty
                    <p class="text-center">
                    No Results found for query   <strong>{{ request()->query('search') }} </strong>
                </p>
                    @endforelse
            </div>


            {{-- <nav class="flexbox mt-30">
              <a class="btn btn-white disabled"><i class="ti-arrow-left fs-9 mr-4"></i> Newer</a>
              <a class="btn btn-white" href="#">Older <i class="ti-arrow-right fs-9 ml-4"></i></a>
            </nav> --}}
            {{ $posts->appends(['search' => request()->query('search') ])->links() }}

          </div>

          @include('partials.sidebar')

        </div>
      </div>
    </div>
  </main>
@endsection

If you guys didn't see which file that i don't show up, just ask,. If you guys see any problems that confusing, i can use teamviewer. Any Help will be appreciated. Thank you.

Upvotes: 1

Views: 8114

Answers (3)

ariefbayu
ariefbayu

Reputation: 21979

The error told that you don't have posts. Indeed it is. What you have is post() method on Category class, as evidently seen in this line:

public function post() {

You should change this ->with('posts', $category->posts()->simplePaginate()) to be:

public function category(Category $category) {
    return view('blog.category')
    ->with('category', $category)
    ->with('posts', $category->post()->simplePaginate())
    ->with('categories', Category::all())
    ->with('tags', Tag::all());
}

Upvotes: 2

Edward Chew
Edward Chew

Reputation: 504

Your relationship is post and you have multiple ->with('posts', $category->posts()->simplePaginate()). Can you change those to ->with('post') and try again?

Upvotes: 1

Shizzen83
Shizzen83

Reputation: 3529

In your model, you defined a post relationship whereas you use a posts relationship then, they're not the same one. You should replace post by posts because it's a to-many relationship.

Upvotes: 1

Related Questions