newbie
newbie

Reputation: 3

No query results for model [User] | laravel

I'm a newbie to Laravel so I'm not yet familiar with the errors it's returning, I hope someone can help me with this.

So I created a simple app with registration form and a login form and the registered user can post whatever he/she wants but I'm getting this error

This is the form where the user can post:

@section('content')

    <section class="header">
        <ul>
            <li></li>
            <li><a href="{{ URL::route('profile')}}" class="new">Back to Profile</a></li>
        </ul>

    </section>
    <div class="newpost">
        <h3>What new today?</h3><br>
        <form action="{{URL::route('createPost')}}" method="post" autocomplete="off">

        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" class="form-control" id="title" name="title" placeholder="Title..." required>
        </div>

        <div class="form-group">
            <label for="content">Write Report</label>
            <textarea class="form-control" name="content" id="content" placeholder="Write Here..." rows="10"></textarea>
        </div>

            <button type="submit" class="btn2">Publish</button>

    </form>
    </div>
@stop

This is the route:

Route::get('/Newpost', array(
    'uses' => 'LoginUsersController@newPost',
    'as' => 'newPost'
    ));

Route::post('/CreatePost/{id}', array(
    'uses' => 'LoginUsersController@createPost',
    'as' => 'createPost'
    ));

and the controller

public function createPost($id)
    {
        $users = User::findOrFail($id);

        $post = array(
            'title' => Input::get('title'),
            'content' => Input::get('content')
            );
        $posts = new Post($post);
        $user->post()->save($post);

        dd($post);
    }

and the User model where I'm guessing is causing the error.

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;


    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';


    protected $fillable = array('email', 'password');

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');


    public function getRememberToken() 
    {
        return $this->remember_token;
    }


    public function setRememberToken($value) 
    {
        $this->remember_token = $value;
    }


    public function getRememberTokenName() 
    {
        return 'remember_token';
    }

    public function Post()
    {
        return $this->hasMany('Post', 'user_id');
    }

}

Can someone please explain to me why I'm getting this error? Thanks

Upvotes: 0

Views: 4172

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153020

This error is caused because findOrFail can't find anything. So it fails. If your route depends on the users id. You actually have to pass it along when creating your form:

<form action="{{URL::route('createPost', Auth::id())}}" method="post" autocomplete="off">

(Auth::id() retrieves the id of the current logged in user)

However instead, I suggest that you remove the user id from the createPost route and work with the currently logged in user directly in the controller:

Route::post('/CreatePost', array(
    'uses' => 'LoginUsersController@createPost',
    'as' => 'createPost'
));

And then:

public function createPost()
{
    $user = Auth::user();

    $post = array(
        'title' => Input::get('title'),
        'content' => Input::get('content')
        );
    $posts = new Post($post);
    $user->post()->save($post);

    dd($post);
}

Upvotes: 1

Related Questions