lundi 2 avril 2018

Using Eloquent's ORM to insert usermeta.id into posts.usermeta_id

I'm working on a user-generated content blog that allows a user to go through the whole upload process before being prompted to sign up. Basic flow: fill out form to pick username/basic info->upload blog post->prompt to sign up with email/password. The purpose of reversing the normal flow is to increase the UX and conversion rate and avoid a wall in the beginning.

Instead of migrating, I've just created the tables manually in PHPmyAdmin. I have 3 relational models: Usermeta->hasOne(App\Mopdels\Post), Post->belongsTo(App\Models\Usermeta), and User->belongsTo(App\Models\Usermeta).

What I'm having trouble with is once the user has created a username and submits the first form to the usermeta table, and then submits the second form to upload their blog post to the post table, it doesn't seem to be attaching the usermeta.id to posts.usermeta_id linking them together. I must be missing something or not attaching it correctly. Here's my StoryController:

<?php

namespace App\Controllers\Story;

use App\Models\Post;
use App\Models\User;
use App\Models\Usermeta;
use App\Controllers\Controller;
use Respect\Validation\Validator as v;


class StoryUploadController extends Controller
{

  public function guidance($request, $response)
  {
    return $this->view->render($response, 'storyupload/guidance.twig');
  }

  //set up our the Upload Story class so the user can upload their story
  //render the view 'uploadstory.twig'
  public function getStoryUpload($request, $response)
  {
    return $this->view->render($response, 'storyupload/upload.twig');
  }

  // This method is called when the user submits the final form
  public function postStoryUpload($request, $response, $id)
  {
    //set up our validation rules for our complete sign up form
    $validation = $this->validator->validate($request, [
      'title' => v::stringType()->notEmpty()->length(1, 80),
      'body' => v::stringType()->notEmpty()->length(1, 2500),
    ]);

    //if validation fails, stay on story upload page
    if ($validation->failed()) {
      return $response->withRedirect($this->router>pathFor('storyupload.upload'));
    }

    $user = Usermeta::find($id)->first();

    //We can use our Post Model to send the form data to the database
    $post = Post::create([
      'title' => $request->getParam('title'),
      'body' => $request->getParam('body'),
      'category' => $request->getParam('category'),
      'files' => $request->getParam('img_path'),
      'usermeta_id' => usermeta()->attach($user->id),
    ]);

    //after submit, redirect to completesignup page
    return $response->withRedirect($this->router->pathFor('auth.completesignup'));
  }
}

I continue to get the error 'usermeta_id cannot be null' so it's definitely not pulling the id from the usermeta table correctly. I've used the create() method to send the usermeta data to the table in my Auth controller.

Would it be better to have all of my form submissions in the Auth controller and what is the proper way using my example to make sure that my posts.usermeta_id is linked to my usermeta.id?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire