@bpGusar
*spoiler*

Почему не работает update данных в базе laravel 5.4?

контроллер загрузки страницы редактирования
public function edit($id)
    {
        $post = Posts::find($id);
        $category = DB::table('post_category')->pluck('cat_name', 'id_cat');

        return view('admin.posts.editpost', compact('post','category'));
    }


котроллер update
public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'required|max:255',
            'short_story' => 'required|unique:posts',
            'full_story' => 'required',
            'id_cat' => 'required',
            'poster' => 'image',
        ]);
        $post = Posts::find($id);
        $post->title=$request->title;
        $post->short_story=$request->short_story;
        $post->full_story=$request->full_story;
        $post->id_cat=$request->id_cat;
        if($request->hasFile('poster')){
            $posterimg=$request->file('poster');
            $postername=time().'.'.$posterimg->getClientOriginalExtension();
            $posterlocation=public_path('img/' . $postername);
            Image::make($posterimg)->save($posterlocation);
            $oldPosterName=$post->image;
            $post->image=$postername;
            Storage::delete($oldPosterName);
        }
        $post->save();
        Session::flash('success', 'Новость опубликована! ');
        return view('admin.posts.msgGlobal');
    }


view
{!! Form::model($post, ['route' => ['admin.update', $post->id], 'method'=>'PUT', 'files'=>true]) !!}

            {!! Form::label('title', 'Название поста') !!}
            {!! Form::text('title', null, ['class'=>'form-control', 'required'=>'']) !!}

            {!! Form::label('short_story', 'Краткая новость, предисловие' ) !!}
            {!! Form::textarea('short_story', null, ['class'=>'form-control', 'required'=>'']) !!}

            {!! Form::label('full_story', 'Полная новость') !!}
            {!! Form::textarea('full_story', null, ['class'=>'form-control', 'required'=>'']) !!}

                    {!! Form::label('id_cat', 'Категория') !!}
                    {!! Form::select('id_cat', $category, null, ['class' => 'form-control', 'required'=>'']) !!}


                    {!! Form::label('poster', 'Постер') !!}
                    {!! Form::file('poster',['class' => 'form-control']) !!}
                    <img src="{{ asset('img/' . $post->poster)}}" class="rounded img-fluid"/>

                    {!! Form::label('created_at', 'Дата публикации') !!}
                    {!! Form::date('created_at', \Carbon\Carbon::now(), ['class' => 'form-control', 'disabled'=>'']) !!}

                {!! Form::submit('Опубликовать', ['class'=>'btn btn-primary']) !!}

    {!! Form::close() !!}


уже 2 дня бьюсь об камень головой но не пойму что не так!
  • Вопрос задан
  • 698 просмотров
Решения вопроса 1
shindakioku
@shindakioku
Не сайтоклепатор
Ошибку пишет?
Во-первых:
public function edit($id)
    {
        $post = Posts::find($id);
        $category = DB::table('post_category')->pluck('cat_name', 'id_cat');

        return view('admin.posts.editpost', compact('post','category'));
    }

Это что за запрос для $category? Да и почему в pluck названия ячеек из 'posts'?
Во-вторых:
public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'required|max:255',
            'short_story' => 'required|unique:posts',
            'full_story' => 'required',
            'id_cat' => 'required',
            'poster' => 'image',
        ]);

У Вас обновление и стоит unique:posts
Это меня смущает, уберите и попробуйте обновить запись
$post->save();
Можно использовать update()
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы