如何从评论中获取帖子 ID 拉拉维尔中的控制器



这是关于拉拉维尔的。 我想在注释变量中获取post_id并将其存储到 PHPMyAdmin,但我找不到正确的代码。

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

我在评论控制器中的代码:

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsComment;
use AppModelsPosting;
use IlluminateSupportFacadesAuth;

class CommentsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        //
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return IlluminateHttpResponse
     */
    public function create()
    {
        return view('comments.create');
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    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 IlluminateHttpResponse
     */
    public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function edit($id)
    {
        //
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function update(Request $request, $id)
    {
        //
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function destroy($id)
    {
        //
    }
}

我在PostingsController中的代码:

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsposting;
use AppUser;
use IlluminateSupportFacadesAuth;
class PostingsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    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 IlluminateHttpResponse
     */
    public function create()
    {
        return view('create');
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    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 IlluminateHttpResponse
     */
    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 IlluminateHttpResponse
     */
    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  IlluminateHttpRequest  $request
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    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 IlluminateHttpResponse
     */
    public function destroy($id)
    {
        $posting = Posting::find($id);
        $posting->delete();
        return redirect()->to('/home')->with('success', 'Posting deleted successfully');
    }

}

如果需要更多信息,我会更新。提前谢谢你。

---附加信息--- 这是注释模型:

<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Comment extends Model
{
    //comments has  only one post
    public function post(){
        return $this->belongsTo(AppPost::class, 'post_id');
    }

}

这是后期模型:

<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Posting extends Model
{
    // For declaring postings has only one user in the model of users: 
    public function user(){
        return $this->belongsTo(AppUser::class, 'user_id');
    }
    // Posting has many comments
    public function comments(){
        return $this->HasMany(AppComment::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  | /                       | AppHttpControllersPostingsController@index                          |
| GET|HEAD  | api/user                | Closure                                                                |
| POST      | comments                | AppHttpControllersCommentsController@store                          |
| GET|HEAD  | comments                | AppHttpControllersCommentsController@index                          |
| GET|HEAD  | comments/create         | AppHttpControllersCommentsController@create                         |
| DELETE    | comments/{comment}      | AppHttpControllersCommentsController@destroy                        |
| PUT|PATCH | comments/{comment}      | AppHttpControllersCommentsController@update                         |
| GET|HEAD  | comments/{comment}      | AppHttpControllersCommentsController@show                           |
| GET|HEAD  | comments/{comment}/edit | AppHttpControllersCommentsController@edit                           |
| GET|HEAD  | home                    | AppHttpControllersHomeController@index                              |
| GET|HEAD  | login                   | AppHttpControllersAuthLoginController@showLoginForm                |
| POST      | login                   | AppHttpControllersAuthLoginController@login                        |
| POST      | logout                  | AppHttpControllersAuthLoginController@logout                       |
| POST      | password/confirm        | AppHttpControllersAuthConfirmPasswordController@confirm            |
| GET|HEAD  | password/confirm        | AppHttpControllersAuthConfirmPasswordController@showConfirmForm    |
| POST      | password/email          | AppHttpControllersAuthForgotPasswordController@sendResetLinkEmail  |
| POST      | password/reset          | AppHttpControllersAuthResetPasswordController@reset                |
| GET|HEAD  | password/reset          | AppHttpControllersAuthForgotPasswordController@showLinkRequestForm |
| GET|HEAD  | password/reset/{token}  | AppHttpControllersAuthResetPasswordController@showResetForm        |
| GET|HEAD  | postings                | AppHttpControllersPostingsController@index                          |
| POST      | postings                | AppHttpControllersPostingsController@store                          |
| GET|HEAD  | postings/create         | AppHttpControllersPostingsController@create                         |
| PUT|PATCH | postings/{posting}      | AppHttpControllersPostingsController@update                         |
| GET|HEAD  | postings/{posting}      | AppHttpControllersPostingsController@show                           |
| DELETE    | postings/{posting}      | AppHttpControllersPostingsController@destroy                        |
| GET|HEAD  | postings/{posting}/edit | AppHttpControllersPostingsController@edit                           |
| POST      | register                | AppHttpControllersAuthRegisterController@register                  |
| GET|HEAD  | register                | AppHttpControllersAuthRegisterController@showRegistrationForm      |
+-----------+-------------------------+------------------------------------------------------------------------+

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

内部注释创建/更新视图:

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

内部评论控制器:

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

最新更新