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

Viewed 34

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]);
    }
1 Answers

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;
Related