Hi! im sorry if im asking the same question, it was not clear enough for me why this error is showing, im a newbie to laravel so im not yet familiar with the errors its 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 im 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>
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 im 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'); } }As the error message says, because you're using findOrFail, it's failing because it can't find a user in your database with that ID number. Check the users table in your database, what's in there? Is there a user with the ID number you're passing in your route/controller?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community