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.
<table> <form action="/editloan" method="POST"><br><br> @csrf <input type="hidden" name="number" value="{{ $data['number'] }}"> <tr> <!-- Here nothing shows up :( --> <td><input type="number" name="clientid" value="{{ $data['clientid'] }}"><br><br></td> </tr> <tr> <td><input type="number" name="livreid" value="{{ $data['livreid'] }}"><br><br></td> </tr> <tr> <td><button class="btn btn-primary btn-block" type="submit">Modifier</button></td> </tr> </form> </table>
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 $data=DB::table('emprunts')->where('number',$req); $data->clientid=$req->clientid; $data->livreid=$req->livreid; $data->save(); $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]); }
Answers:
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 $data=DB::table('emprunts')->where('number',$req->number)->first(); // 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:
$data->columnName;
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