2

i have two table one is user which is parent and other is posts(child) table and its coulmns are

 public function up(){
        Schema::create('posts', function (Blueprint $table) {

            $table->increments('id');
            $table->integer('user_id');
            $table->string('title');
            $table->string('body');
            $table->timestamps();

        }
   }

i have already specified

$this->hasOne('Post');

relation into user model. now i want to insert data into posts table where with form i can save data into title and body column into there respective id's.

1 Answer 1

3

If it's an authenticated user, you can do this:

auth()->user()->posts()->create($request->all());

To make create() method work you also need to have $fillable array with all properties in the Post model:

$protected fillable = ['title', 'body'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.