boby
1 month ago
281
13
Laravel

Method destroy() not picking up ID

Posted 1 month ago by boby

So, I am working on something that should be like online books library. I have controllers Books and Users, and one as a relation between them, called OweController. I use CRUD on this controllers. When I navigate to Owes page I fill it like this:

public function index()
    {
        $owes = Owe::where('owes.active', '1')
                    ->leftjoin('books','owes.book_id', '=', 'books.id')
                    ->leftjoin('users','owes.user_id', '=', 'users.id')
                    ->select('owes.id as oweId', 'books.id as bookId','books.title','users.id as userId','users.first_name','users.last_name')
                    ->get();

        return view('owes.index', compact('owes'));
    }

then on index I have a form for book return:

<form method="POST" action="{{ route('owes.destroy', $owe->oweId) }}">
@method('DELETE')
@csrf
    <input type="hidden" name="member" value="{{ $owe->userId }}">  
        <button type="submit" class="btn btn-primary">
                {{ __('Return book') }}
            </button>
</form>

So question: if I use as above: route('owes.destroy', $owe->oweId) my destroy method picks this oweId as:

public function destroy($oweId)
{
        $owe = Owe::findOrFail((int)($oweId));
        $owe->update(['active' => false]);
    ...

but if I do it like on other controllers using: action="/owes/$owe->oweId) - this oweID is sent to destroy but not picked up from it. It is null. Of course my destroy is different in that case: destroy(Request $request, Owe $owes)

Second question is, can I send userId without using hidden in above form as I think that is not a proper way to do it.

So sorry for such a long post, but I wanted to provide as much info as possible.

Please sign in or create an account to participate in this conversation.