简体   繁体   English

多对多关系与资源 controller 没有 model laravel

[英]Many to many realtionship with resource controller without model laravel

i am a new php programmer and i just started with laravel, i am trying to build a library application and at the moment i am working on the loans part.我是一个新的 php 程序员,我刚开始使用 laravel,我正在尝试构建一个图书馆应用程序,目前我正在处理贷款部分。 basically i have a pivot table named copy_user with the following columns: ('copy_id', 'user_id', 'date', 'return_date', 'created_at', 'updated_at') copy_id is one of many copies that could be linked to a book in the books table.基本上我有一个名为copy_user的 pivot 表,其中包含以下列: ('copy_id', 'user_id', 'date', 'return_date', 'created_at', 'updated_at') copy_id 是可以链接到书桌上的书。 basically i on the user model i have this code:基本上我在用户 model 我有这个代码:

public function loanedCopies() {
        return $this->belongsToMany(Copy::class)->withPivot('date', 'return_date');
    }

while on the copy model i have that:在副本 model 上,我有:

public function loaningUsers() {
        return $this->belongsToMany(User::class)->withPivot(['date', 'return_date']);
    }

than i created a view for the loans which will show the content of the content of the table copy_user so i also created a LoanController and a route to use the resource controller like that:比我为贷款创建一个视图,它将显示表 copy_user 的内容,所以我还创建了一个 LoanController 和一个使用资源 controller 的路由,如下所示:


Route::resource('loans', LoanController::class);

now what i'm trying to do is to delete or edit the records on the copy_user table by using the resource WITHOUT using a model, and i'm trying to do so like that:现在我要做的是通过使用资源而不使用 model 来删除或编辑 copy_user 表上的记录,我正在尝试这样做:

code on the view:视图上的代码:


<form action="/loans/{{$copy->id}}?user_id={{$user->id}}&date={{$copy->pivot->date}}" method="POST">
          @csrf
          @method('DELETE')
          <button type="submit" class="btn btn-link"><i class="fas fa-trash-alt"></i></button>
</form>

code in the controller: controller中的代码:

public function destroy(Copy $copy)
    {
        $request = Request::capture();
        $request->get('user_id');
        $request->get('date');
        $copy->loaningUsers()->where('id', $request->get('user_id'))->wherePivot('date', $request->get('date'))->detach();
        return redirect('/loans');
    }

Now the problem is that the destroy method is not receiving any data so it's not deleting anything.现在的问题是 destroy 方法没有接收到任何数据,所以它没有删除任何东西。 I was specifically told to do so without using a model for loans but i don't know if it's possible.我被特别告知不要使用 model 贷款,但我不知道这是否可能。 using the resource controller was my decision but i don't know if it's the right way to go, does any body know why this doesn't work?使用资源 controller 是我的决定,但我不知道它是否是通往 go 的正确方法,有人知道为什么这不起作用吗? thanks in advance to every body who will answare!提前感谢每一位愿意回答的人!

Since your resource is named loans, the destroy method will expect you to use $loan as it's parameter in order to bind data.由于您的资源名为 loans,destroy 方法将期望您使用 $loan 作为参数来绑定数据。

Route::resource('loans', LoanController::class); //This expects you to pass $loan


public function destroy(Copy $loan) { //Switch from $copy to $loan

}

You can also verify this by checking the routes using:您还可以通过使用以下方法检查路由来验证这一点:

php artisan route:list

thankyou Vimona, just got back on that project and i ditched the resource controller in favor of normal routing for the loans by doing the following:谢谢 Vimona,刚刚回到那个项目,我放弃了资源 controller,通过执行以下操作支持贷款的正常路由:

Route::get('/loans', 'App\Http\Controllers\LoanController@index');
Route::delete('/loans/{copy}', 'App\Http\Controllers\LoanController@destroy');

and on the controller i left the method as it was:在 controller 上,我保留了原样的方法:

public function destroy(Copy $copy)
{
    $request = Request::capture();
    $request->get('user_id');
    $request->get('date');
    $copy->loaningUsers()->where('id', $request->get('user_id'))->wherePivot('date', $request->get('date'))->detach();
    //$copy->loaningUsers()->detach();
    return redirect('/loans');
}

by doing that now the the delete method works fine, hopefully i didn't make any mistake, if so, please tell me so i can try to fix it!通过这样做,删除方法现在可以正常工作,希望我没有犯任何错误,如果是这样,请告诉我,以便我可以尝试修复它!

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

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