Support the ongoing development of Laravel.io →
posted 10 years ago
Queues

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> @stop
Last updated 3 years ago.
0

Instead of:

$user->post()->save($post);

Try:

$post->push();
0

Hi thank you for your response but it ddnt work still returns the same error. what do you think is causing the error?

0

I think error in route You have use

Route::get('newpost',.....);
Route::post('newpost',....);

Or try

$user->posts()->save($post)
Last updated 10 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

lovingLara lovinglara Joined 16 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.