mardi 8 mai 2018

How do I get the user id and using it to post new comment in the store method in laravel?

This code does not seem to capture the user id from the session so I can use it as a foreign key in the database for the place the user is adding a comment to.

public function store(Request $request, $place_id) {

  // find place in the database
  $place = Place::find($place_id);

  // find user in the database
  $user = User::find(\Auth::id());

  // create review instance
  $review = new Review([
    'review' => request('review'),
    'rating' => request('rating')
  ]);

  // save review (creating relationship) in the places table as reviews
  $place - > reviews() - > save($review);

  // save review (creating relationship) in the users table as reviews
  $user - > reviews() - > save($review);

  $reviewData = Review::find($review - > id);

  if (request() - > wantsJson()) {
    return $reviewData; // Returns JSON, thanks to Laravel Magic™
  }

  // return view
  return view('place');

}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2FWGUDM
via IFTTT

Aucun commentaire:

Enregistrer un commentaire