简体   繁体   中英

Can't delete a record in Laravel 5

I'm having trouble in deleting a record that has file in it. Below is the code.

delete file method :

private function deletePDF(Journal $journal) {
        $exist = Storage::disk('file')->exists($journal->file);

        if (isset($journal->file) && $exist) {
            $delete = Storage::disk('file')->delete($journal->file);
            if ($delete) {
                return true;
            }
            return false;
        }
    }

Destroy method :

public function destroy(Journal $journal, EditionRequest $request) {
    $this->deletePDF($journal);
    $journal->delete();

    return redirect()->route('edition', ['id' => $request->id]);
}

The result game me nothing, it's just return to the page where the record belongs and does not deleting the record. I used the same code for another project with the same laravel version and it's working, but for some reasons it doesn't work here and I'm a lil bit confused.

Update :

EditionRequest :

public function rules() {
        // Cek apakah CREATE atau UPDATE
        $id = $this->get('id');
        if ($this->method() == 'PATCH') {
            $volume_rules = 'required|integer|unique_with:edition,number,' . $id;
            $number_rules = 'required|integer';
        } else {
            $volume_rules = 'required|integer|unique_with:edition,number';
            $number_rules = 'required|integer';
        }
        return [
            'volume' => $volume_rules,
            'number' => $number_rules,
            'cover' => 'sometimes|image|max:15000|mimes:jpeg,jpg,bmp,png',
        ];
    }

If it returns to the same page as before, you probably have a validation error in your request!

You can check the errors easily by adding the following snippet to your view:

@if(count($errors) > 0)
    <ul>
        @foreach($errors->all() as $error)
            <li>{{$error}}</li>
        @endforeach
    </ul>
@endif

With this you should be able to see what's going wrong. Let me know if it worked :)

edit

public function rules() {
        // Cek apakah CREATE atau UPDATE
        $id = $this->get('id');
        if ($this->method() == 'PATCH') {
            $volume_rules = 'required|integer|unique_with:edition,number,' . $id;
            $number_rules = 'required|integer';
        }  

        else if($this->method() == 'DELETE'){
             //your delete validation rules rules
             $volume_rules = ''
             $number_rules= '';              
        } 

        else {
            $volume_rules = 'required|integer|unique_with:edition,number';
            $number_rules = 'required|integer';
        }
        return [
            'volume' => $volume_rules,
            'number' => $number_rules,
            'cover' => 'sometimes|image|max:15000|mimes:jpeg,jpg,bmp,png',
        ];
    }

You might even want to not use the request youre using now, which would give you this:

public function destroy(Journal $journal, Request $request) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM