salama2121
salama2121

Reputation: 59

Laravel Controller error

I'm learning how to do a simple Laravel app, an social network and I came across an error

[Fri Aug 26 00:46:30 2016] PHP Fatal error:  Class 'App\Http\Controllers\Post' not found in C:\xampp\htdocs\social-network\app\Http\Controllers\PostController.php on line 13

My PostController.php is this:

<?php
namespace App\Http\Controllers;

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

class PostController extends Controller
{

    public function postCreatePost(Request $request)
    {

        $post = new Post();
        $post->body = $request['body'];
        $request->user()->posts()->save($post);
        return redirect()->route('dashboard');

    }
}

My Post model is this:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

    public function user(){
        return $this->belongsTo('App\User');
    }
}

I don't understand what I did wrong. Can anyone help me figure out the error. Thanks in advance

Upvotes: 1

Views: 154

Answers (2)

Geoherna
Geoherna

Reputation: 3534

You forgot to use tell your controller to use App\Post:

<?php
namespace App\Http\Controllers;

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

class PostController extends Controller{

    public function postCreatePost(Request $request){
        $post = new Post();
        $post->body = $request['body'];
        $request->user()->posts()->save($post);
        return redirect()->route('dashboard');

    }
}

Also, typically good idea to make sure you run composer dump-autoload after adding new classes.

Upvotes: 2

Maytham Fahmi
Maytham Fahmi

Reputation: 33377

You need to add the model.

Try to add following line in your controller after namespace

use App\Post;

This is what the error message exactly telling you.

Upvotes: 2

Related Questions