Updating a record

Published 1 month ago by behnampmdg3

Let's say we wanna update a record and make sure the column 'name' is unique.

This validation advised on Larvel website won't work because it finds the same record and the name is already taken obviously.

Which doesn't really make sense because the name is the name of the current book being updated of course it's taken.

public function update(Request $request, books $books)
    {
        $request->validate([
            'name' => 'bail|required|unique:books|max:255'
        ]);
        $book = \App\Books::find($request->id);
        $book->name = $request->name;
        $book->save();
        //
    }

I am thinking something like this. Which does the job but then I have to deal with redirects etc.

public function update(Request $request, books $books)
    {
        $books = \App\Books::where('id', '!=' , $request->id)
               ->where('name', $request->name)
               ->get()->count();
        if($books==0)
            {
                $request->validate([
                'name' => 'bail|required|max:255'
                ]);
                //$books = new Books;
                $book = \App\Books::find($request->id);
                $book->name = $request->name;
                $book->save();
                //
            }    
    }

Advice?

tykus
tykus
1 month ago (540,240 XP)

This is a solved problem, which a quick look at the docs would have shown:

$request->validate([
    'name' => [
        'bail',
        'required',
        Rule::unique('books')->ignore($books->id),
        'max:255'
    ]
]);
behnampmdg3

Beautiful

behnampmdg3

When I add

Rule::unique('books')->ignore($books->id),

I get error:

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class 'App\Http\Controllers\Rule' not found
<?php

namespace App\Http\Controllers;

use App\books;
use Illuminate\Http\Request;

class BooksController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $books = \App\books::all();
        return view('books',['books' => $books]);

        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('add_books');
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'bail|required|unique:books|max:255'
        ]);
        $books = new Books;
        $books->name = $request->name;
        $books->save();
        return redirect('/books');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\books  $books
     * @return \Illuminate\Http\Response
     */
    public function show(books $books)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\books  $books
     * @return \Illuminate\Http\Response
     */
    public function edit(books $books)
    {
        $books = $books->toArray();
        return view('edit_books', ['book' => $books]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\books  $books
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, books $books)
    {
            
               $request->validate([
                    'name' => [
                        'required',
                        Rule::unique('books')->ignore($books->id),
                        'max:255'
                    ]
                ]);
                //$books = new Books;
                $book = \App\Books::find($request->id);
                $book->name = $request->name;
                $book->save();
                //
                return redirect('/books')->with('update_message', 'Updated Successfully!');
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\books  $books
     * @return \Illuminate\Http\Response
     */
    public function destroy(books $books)
    {
        //
    }
}

topvillas

You might want to learn how to import classes.

use Rule;
tykus
tykus
1 month ago (540,240 XP)

You need to import it.

behnampmdg3

None of these work:

use App\Rule;
use \App\Rule;
use Rule;

use \Rule;
tykus
tykus
1 month ago (540,240 XP)
use Illuminate\Validation\Rule;

It's in the docs link I gave you earlier

behnampmdg3

Thanks got it

Please sign in or create an account to participate in this conversation.