J
J
Johnny Show2020-05-27 13:26:19
Laravel
Johnny Show, 2020-05-27 13:26:19

How to update table field record?

Previous entries are being edited.
Unable to edit phone record.
There are two tables: a Post table (with fields: id, name, surname) and a linking table. Phones( with fields: id,post_id,number) with foreign key post_id, one-to-many relationships are used.
The number field was unique, but when editing the phone, a duplicate key error constantly popped up.
The Phone input fields are dynamic so that the user can add multiple phones. After that, I will add a limit on the number of numbers.
Then I removed the uniqueness of the field and decided to check the editing without it - there are no errors, but editing does not occur, the data remains the same. I hope I can decide with you.

public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|max:255',
            'surname' => 'required|max:255',
            'phone' => 'required',
        ]);

        $post = Post::find($id);
        $post->name = $request->get('name');
        $post->surname = $request->get('surname');

       foreach ($request->get('phone') as $phone) {

          $post->phones()->where('post_id', $id)->update(['number' => $phone]);
            $post->save();
            }
        return redirect('/posts')->with('success', 'Запись отредактирована');
    }

class Post extends Model
{
    protected $fillable = [
        'name', 'surname'
    ];

    public $timestamps = false;
    
    public function phones()
    {
        return $this->hasMany('App\Models\Phone');
    }


class Phone extends Model
{
    protected $fillable = [
      'number', 'post_id'
    ];
    public $timestamps = false;
    
    public function post()
    {
        return $this->belongsTo('App\Models\Post', 'post_id', 'id');
    }
}


<form method="POST" action="{{route('posts.update', $post)}}">
                {{ csrf_field() }}
                @method('PATCH')
                <button type="submit" class="btn btn-success">Отредактировать запись</button>
                <div class="form-group">
                    <label for="post-name">Имя</label>
                    <input type="text" name="name" value="{{$post->name}}" class="form-control" id="post-name">
                </div>
                <div class="form-group">
                    <label for="post-surname">Фамилия</label>
                    <input type="text" name="surname" value="{{$post->surname}}"  class="form-control" id="post-surname">
                </div>
                <label for="post-phone">Телефон</label>
                @foreach ($post->phones as $data)

                <div class="entry input-group" id="dynamic_field">
                    <input type="text" name="phone[]" value="{{$data->number}}" class="form-control" id="post-phone">
                    <span class="input-group-btn">
                            <button class="btn btn-success btn-add" name="add" id="add" type="button">
                                <span class="glyphicon glyphicon-plus"></span>
                            </button>
                        </span>
                </div>
                @endforeach
            </form>


5ece40124e149694107312.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question