Eloquent / Laravel 8 : eloquent request doesn’t store wanted values into $variable

I have a problem, in my code below, I want to update my database with new entries, great it worked fine for my other tables beauce I could use $data = Table::find($id);
but here I can’t because my primary key is two foreign keys.. (customersid and bookid make the primary key of my table).

I tried the code below but it doesn’t work, like the $data doesn’t contain the values that it was supposed to get.

    <form action="/editloan" method="POST"><br><br>
        <input type="hidden" name="number" value="{{ $data['number'] }}">
                <!-- Here nothing shows up :( -->
                <td><input type="number" name="clientid" value="{{ $data['clientid'] }}"><br><br></td>

                <td><input type="number" name="livreid" value="{{ $data['livreid'] }}"><br><br></td>

                <td><button class="btn btn-primary btn-block" type="submit">Modifier</button></td>

And when I want to pass the data to my final function to change the data in the database, it doesn’t work either … :

 function updateLoan(Request $req)
        // du coup ne fonctionne pas non plus 

        $listloans = Emprunt::all();
        return view('crud.showloans',['emprunts'=>$listloans]);

How could I do ?

Sorry I know it’s probably a dumb question but it’s my first work on laravel and in only a student 🙂

    function showFormEdit($id)
            $data = DB::table('emprunts')->where('number','=',$id)->get();
            return view('crud.formloanedit',['data'=>$data]);


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

First check here, I can see you forgot to ->first() the results.

// du coup ne fonctionne pas non plus 
// devrait mieux fonctionner

Second part to get data:

function showFormEdit($id)
        $data = DB::table('emprunts')->where('number','=',$id)->first();
        return view('crud.formloanedit',['data'=>$data]);

and you get like this:


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x