没有模型[User] |的查询结果laravel

时间:2015-02-03 09:32:00

标签: php laravel laravel-4

我是Laravel的新手,所以我还不熟悉它回来的错误,我希望有人能帮助我。

所以我创建了一个带注册表单和登录表单的简单应用程序,注册用户可以发布他/她想要的任何内容,但我收到此错误

这是用户可以发布的表单:

@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

这是路线:

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);
    }

和我猜测的用户模型导致错误。

<?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');
    }

}

有人可以向我解释为什么我收到此错误?感谢

1 个答案:

答案 0 :(得分:1)

导致此错误是因为findOrFail无法找到任何内容。所以它失败。 如果您的路由取决于用户ID。实际上,在创建表单时,您必须将其传递出去:

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

Auth::id()检索当前登录用户的ID)

但是,我建议您从createPost路由中删除用户ID,并直接在控制器中使用当前登录的用户:

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

然后:

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);
}