9阅网

您现在的位置是:首页 > 知识 > 正文

知识

php - 解决了! 调用未定义的方法App\\Category::posts() Laravel.

admin2022-10-29知识18

我想用laravel创建一些CMS项目,当用户试图点击分类时,应用程序将显示只显示用户点击的类别的帖子。当用户点击分类时,应用程序说错误的方法调用。我应该怎么做?任何帮助将被感激。

以下是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('/', '[email protected]')->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', '[email protected]')->name('home');
    Route::resource('categories', 'CategoriesController');
    Route::resource('posts','PostsController');
    Route::resource('tags','TagsController');
    Route::get('trashed-posts','[email protected]')->name('trashed-posts.index');
    Route::put('restore-post/{post}', '[email protected]')->name('restore-posts');
});

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

以下是 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));
    }
}

以下是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);
   }
}

以下是category.blade.php的代码。

@extends('layouts.blog')


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

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

      <div>
        <div>

          <h1>{{ $category->name }}</h1>
          <p>Read and get updated on how we progress</p>

        </div>
      </div>

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

@section('content')
  <main>
    <div>
      <div>
        <div>


          <div>
            <div>

              @forelse($posts as $post)
                  <div>
                      <div>
                        <a href="{{ route('blog.show',$post->id) }}"><img src="{{ asset($post->image) }}" alt="Card image cap"></a>
                        <div>
                          <p>
                              <a href="#"></a>
                                  {{ $post->category->name }}
                              </p>
                          <h5>
                              <a href="{{ route('blog.show',$post->id) }}">
                                  {{ $post->title }}
                              </a>
                          </h5>
                        </div>
                      </div>
                    </div>
                    @empty
                    <p>
                    No Results found for query   <strong>{{ request()->query('search') }} </strong>
                </p>
                    @endforelse
            </div>


            {{-- <nav>
              <a><i></i> Newer</a>
              <a href="#">Older <i></i></a>
            </nav> --}}
            {{ $posts->appends(['search' => request()->query('search') ])->links() }}

          </div>

          @include('partials.sidebar')

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

如果你们没有看到哪个文件我没有显示出来,就问一下,。如果你们看到任何问题,令人困惑,我可以用teamviewer。Any Help will be appreciated.Thank you.



【回答】:

错误告诉你,你没有 posts. 确实如此。你所拥有的是 post() 办法 Category 类,从这一行可以明显看出。

public function post() {

你应该把这句话改成: ->with('posts', $category->posts()->simplePaginate()) 是。

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());
}
【回答】:

在你的模型中,你定义了一个 post 关系,而你却用 posts 那么,他们不是同一个人的关系。你应该把 postposts 因为这是一个对多的关系。

【回答】:

你们的关系是 post 而你有多个 ->with('posts', $category->posts()->simplePaginate()). 你能不能把这些改成 ->with('post') 然后再试一次?