Im trying to get a post of a user and display the content of the post but it returns an error: "Call to undefined method Posts::newQuery()"
my User.php model
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function Posts()
{
return $this->hasMany('Posts');
}
}
my Posts.php model
<?php Class Posts extends Eloquent { protected $table = 'posts'; public function User() { return $this->belongsTo('User'); } } my controller public function newPost($id) { $user = User::findOrFail($id); $post = new Posts(); $post->title = Input::get('title'); $post->content = nl2br(Input::get('content')); $user->post()->save($post); return Redirect::route('profile', array('id' => $user->id)); } and the view @section('post') <section> @foreach($users->posts as $post) <p>{{$user->name}} says...</p> <blockquote>{{$post->content}}</blockquote> @endforeach </section> @stopHi thank you for your response but it ddnt work still returns the same error. what do you think is causing the error?
I think error in route You have use
Route::get('newpost',.....);
Route::post('newpost',....);
Or try
$user->posts()->save($post)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community