1

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 ..

Ariando
  • 1,391
  • 4
  • 24
  • 43

3 Answers3

1

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();
}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • tried this and gave me this error : Call to a member function jurnal() on null – Ariando Sep 06 '16 at 06:17
  • Done, gave me this error : NotFoundHttpException in Router.php line 1006: with strange url : http://1mark.dev/edisi/%5B%5D – Ariando Sep 06 '16 at 06:23
  • This means code works and it executes normally until `return redirect()->route('edisi', ['id' => $id]);` which builds wrong URL. `%5B%5D` is `[]`. Read here: http://stackoverflow.com/questions/14160843/remove-5b5d-from-url-when-submitting-form – Alexey Mezenin Sep 06 '16 at 06:27
  • As I said above, Eloquent can't find `edisi` with specified ID. You can use `die` to terminate script. I've updated code above. – Alexey Mezenin Sep 06 '16 at 06:31
  • It's working now, seems like the problem is I forgot to request the edisi id to edit method .. Anyway, thanks man! – Ariando Sep 06 '16 at 08:18
1

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);
jaysingkar
  • 4,315
  • 1
  • 18
  • 26
  • yes I want to update single jurnal data that belongs to an edisi. So the problem is I want to keep the edisi id as well :) – Ariando Sep 06 '16 at 06:20
  • But, as the Jurnal has its own primary key and edisi-jurnal have 1 to many relationship. I think you won't need the edisi->id. right ? – jaysingkar Sep 06 '16 at 06:24
  • Yes the jurnal has its own primary key, and yes both table have 1 to many relationship. So you mean to tell me that I don't need the edisi id ? If so how would I save the jurnal data to specified edisi ? – Ariando Sep 06 '16 at 06:33
  • can you try above code . I think it would be enough to update – jaysingkar Sep 06 '16 at 07:23
  • It's working now, seems like the problem is I forgot to request the edisi id to edit method .. Anyway, thanks man! – Ariando Sep 06 '16 at 08:18
0

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.

jaysingkar
  • 4,315
  • 1
  • 18
  • 26
Zalo
  • 642
  • 12
  • 24