【问题标题】:"Class 'App\Models\Post' not found"“找不到类 'App\Models\Post'”
【发布时间】:2019-01-19 10:45:30
【问题描述】:

我试图让我的用户在我的社交媒体网站上发帖。我已经有一个“App\Models\Post”。怎么解决??

当我尝试提交帖子时也会出现错误,问题在于:“$post = new Post();”的行

好的,所以这里说我的帖子看起来主要是代码,所以我会写毫无意义的东西,所以这个简单的小东西消失了。我不是以英语为母语的人,所以如果您发现拼写或语法错误,请纠正我:)

这是我的控制器的代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post ;
use App\User;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
      return view('makePost');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */



    /**
     * Store a newly created resource in storage.
     *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function makePost(Request $request)
{
  $this->validate($request, array(
    'post' => 'required',
    'title' => 'nullable|max:50',
    'label' => 'nullable|max:25',
  ));

  $post = new Post();

  $post->post = $request->post;
  $post->title = $request->title;
  $post->label = $request->label;
  $post->save();

  return redirect()->route('index');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */

public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}

这是我的帖子模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function Users() {
      return $this->belongsTo(User::class);
    }
}

【问题讨论】:

  • 帖子模型应该是namespace App\Models;
  • 你可以运行一个简单的命令来填充正确的命名空间,模型绑定,控制器名称,模型名称使用php artisan make:controller PostController --resource --model=Models\Post

标签: laravel posts


【解决方案1】:

尝试使用: 使用应用\发布; 而不是使用 App\Models\Post ;

【讨论】:

    【解决方案2】:

    你在你的模型中定义了错误的命名空间,如果它在模型目录中将它更改为:

    namespace App\Models;
    

    如果不是,您可以随时将控制器更改为:

    use App\Post;
    

    【讨论】:

    • 对我来说似乎是完美的解决方案。
    猜你喜欢
    • 2021-12-28
    • 2017-06-07
    • 2017-10-04
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 2021-10-15
    相关资源
    最近更新 更多