繁体   English   中英

如何从laravel中的commentsController获取帖子ID

[英]How to get the post id from commentsController in laravel

这是关于 Laravel。 我想在评论变量中获取 post_id 并将其存储到 PHPMyAdmin,但我找不到正确的代码。

当我输入 $comment->post_id = Posting::id(); 在 CommentsController 中,它产生了错误。 它显示“调用未定义的方法 App\\Models\\Posting::id()”

我在 CommentsController 中的代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Comment;
use App\Models\Posting;
use Illuminate\Support\Facades\Auth;


class CommentsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('comments.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //if user did not fill the form properly
        //error messages will popup
        $this->validate($request, [
            'comment' => 'required',
        ]);


        //if user filled the form properly
        //store the form
        $comment = new Comment();
        //to store user_id
        $comment->user_id = Auth::id();
        //to store post_id

        //-----------------------------------------------------
        //Here is the problem I have right now.
        //I could not find proper code to store post_id in comment variable here.
        //-----------------------------------------------------
        h1$comment->post_id = ;
        //to store posting
        $comment->comment = $request->input('comment');
        //save all user input
        $comment->save();
        dd($request);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

我在 PostingsController 中的代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\posting;
use App\User;
use Illuminate\Support\Facades\Auth;

class PostingsController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //this is how to get all posting, but this shows only all posting
        // $postings = Posting::all();
        // $postings->load('user');
        //this is how to get all posting with created_at and descending order.
        $postings = Posting::orderBy('created_at', 'desc')->get();
        //should be function name
        $postings->load('user');


        return view('index')->with('postings', $postings);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //if user did not fill the form properly
        //error messages will popup
        $this->validate($request, [
            'posting' => 'required',
        ]);

        //if user filled the form properly
        //store the form
        $posting = new Posting();
        //for connecting both user and posting
        $posting->user_id = Auth::id();
        //to store posting
        $posting->post = $request->input('posting');
        //save all user input
        $posting->save();

        return redirect()->to('/home')->with('success', 'Posting created successfully!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $posting = Posting::find($id);
        return view('show')->with('posting', $posting);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //this is the way how to find posting of authenticated user
        $posting = Posting::find($id);

        return view('edit')->with('posting', $posting);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //if user did not fill the form properly
        //error messages will popup
        $this->validate($request, [
            'posting' => 'required',
        ]);

        //if user filled the form properly
        //store the form
        $posting = Posting::find($id);
        //to store posting
        $posting->post = $request->input('posting');
        //save all user input
        $posting->save();

        return redirect()->to('/home')->with('success', 'Posting edited successfully!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $posting = Posting::find($id);
        $posting->delete();

        return redirect()->to('/home')->with('success', 'Posting deleted successfully');
    }


}

如果需要更多信息,我会更新。 先感谢您。

---附加信息---这是评论模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    //comments has  only one post
    public function post(){
        return $this->belongsTo(\App\Post::class, 'post_id');
    }


}

这是后模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Posting extends Model
{
    // For declaring postings has only one user in the model of users: 
    public function user(){
        return $this->belongsTo(\App\User::class, 'user_id');
    }
    // Posting has many comments
    public function comments(){
        return $this->HasMany(\App\Comment::class,'post_id');
    }


}

这是路线清单:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

//when users hit my website, go to welcome.blade.php
Route::get('/', 'PostingsController@index');

//Auth::routes() is a helper class that helps you generate all the routes required for user authentication.
Auth::routes();
Route::resource('postings','PostingsController');
Route::resource('comments', 'CommentsController');

//Route::get('address_name', 'NameController');
//sometimes we deal with another one where I wrote 'HomeController@index'
Route::get('/home', 'HomeController@index')->name('home');

+-----------+-------------------------+------------------------------------------------------------------------+
| Method    | URI                     | Action                                                                 |
+-----------+-------------------------+------------------------------------------------------------------------+
| GET|HEAD  | /                       | App\Http\Controllers\PostingsController@index                          |
| GET|HEAD  | api/user                | Closure                                                                |
| POST      | comments                | App\Http\Controllers\CommentsController@store                          |
| GET|HEAD  | comments                | App\Http\Controllers\CommentsController@index                          |
| GET|HEAD  | comments/create         | App\Http\Controllers\CommentsController@create                         |
| DELETE    | comments/{comment}      | App\Http\Controllers\CommentsController@destroy                        |
| PUT|PATCH | comments/{comment}      | App\Http\Controllers\CommentsController@update                         |
| GET|HEAD  | comments/{comment}      | App\Http\Controllers\CommentsController@show                           |
| GET|HEAD  | comments/{comment}/edit | App\Http\Controllers\CommentsController@edit                           |
| GET|HEAD  | home                    | App\Http\Controllers\HomeController@index                              |
| GET|HEAD  | login                   | App\Http\Controllers\Auth\LoginController@showLoginForm                |
| POST      | login                   | App\Http\Controllers\Auth\LoginController@login                        |
| POST      | logout                  | App\Http\Controllers\Auth\LoginController@logout                       |
| POST      | password/confirm        | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            |
| GET|HEAD  | password/confirm        | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    |
| POST      | password/email          | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  |
| POST      | password/reset          | App\Http\Controllers\Auth\ResetPasswordController@reset                |
| GET|HEAD  | password/reset          | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm |
| GET|HEAD  | password/reset/{token}  | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        |
| GET|HEAD  | postings                | App\Http\Controllers\PostingsController@index                          |
| POST      | postings                | App\Http\Controllers\PostingsController@store                          |
| GET|HEAD  | postings/create         | App\Http\Controllers\PostingsController@create                         |
| PUT|PATCH | postings/{posting}      | App\Http\Controllers\PostingsController@update                         |
| GET|HEAD  | postings/{posting}      | App\Http\Controllers\PostingsController@show                           |
| DELETE    | postings/{posting}      | App\Http\Controllers\PostingsController@destroy                        |
| GET|HEAD  | postings/{posting}/edit | App\Http\Controllers\PostingsController@edit                           |
| POST      | register                | App\Http\Controllers\Auth\RegisterController@register                  |
| GET|HEAD  | register                | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      |
+-----------+-------------------------+------------------------------------------------------------------------+

在此处输入链接描述,此链接是基于laravel的发布和提交系统,您需要将post_id作为参数传递给Add commit视图,以将其放置在post_id的值中。 看看这个

您需要在请求中传递 post_id ,然后:

内部评论创建/更新视图:

<input type="hidden" name="post_id" id="post_id" value="{{ $post->id }}" />

内部评论控制器:

//..
$comment->post_id = $request->input('post_id');
//..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM