lundi 2 septembre 2019

Getting a property from a route and inserting into another

i am making a social media application in which after clicking a post, user can add comments to that particular post.

I am having problems in getting the id of the post on which a user can comment. How do i pass the id of the post to add_comment_action? when i add a comment, it is going to the database but not specifying which post it belongs to. (just going randomly in the database). Any help will be appreciated. Thanks

I want to grab the post_id property from item_detail route and want to use that id into the 2nd route which is add_comment_action.

Here is the code snippet. https://imgur.com/gallery/kAvS7bl

For this project, i am supposed to do this in web.php file only. Can not use model and controllers.

This is what i have done yet.

Route::get('item_detail/{post_id}', function($id){
$post = get_item($id);
$comments = get_comment($id);

return view('items.item_detail')->with('post', $post)->with('comments', $comments);

});

Route::post('add_comment_action', function (){


$username = request('comment_username');
$msg = request('comment_msg');
$post_id = request('post_id');
$new_comment = add_comment($username,$msg, $post_id);

});

function add_comment($username,$msg, $post_id){
$sql = "insert into comment (comment_username,  comment_msg, post_id) values (?,?,?)";
DB::insert($sql, array($username, $msg, $post_id));

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire