Ariando
Ariando

Reputation: 1421

Update related data Laravel 5

I have this method update to update a data (jurnal data) that belongs to another data (edisi data), below is the code :

public function update(Jurnal $jurnal, JurnalRequest $request) {
        $input = $request->all();
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            // Delete old file
            $this->hapusPDF($jurnal);

            // Upload new file
            $input['file'] = $this->uploadPDF($request);
        }
        $id = $request->id; //retrieve id edisi

        $jurnal = Edisi::findOrFail($id)->jurnal()->update($input);
        return redirect()->route('edisi', ['id' => $id]);
    }

The method above gave me this error : No query results for model [App\Edisi]. My question is how to make my update method working ? Thank you ..

Upvotes: 1

Views: 107

Answers (3)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You're using findOrFail($id) and it doesn't find any row with id = $id, so it throws an exception. You can do this instead:

$edisi = Edisi::find($id);
if (!is_null($edisi)) {
    $jurnal = $edisi->jurnal()->update($input);
} else {
    echo 'There is no edisi with ID = '.$id;
    die();
}

Upvotes: 1

Zalo
Zalo

Reputation: 704

I think you are not retrieving any id using

$request->id

You should use

$request->get('id') // And you must ensure that there is an input with 'id' name comming from client

You could use too $input['id']. I hope it helps.

Upvotes: 0

jaysingkar
jaysingkar

Reputation: 4435

Assuming you want to update single journal and $jurnal has the journal id you want to update, your update statement will be like below:

$jurnal = Jurnal::findOrfail($jurnal->id)->update($input);

Upvotes: 1

Related Questions