B
B
bpGusar2017-08-06 19:48:56
Laravel
bpGusar, 2017-08-06 19:48:56

Why is update data not working in laravel 5.4 database?

edit page load controller

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'));
    }

controller 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() !!}

I've been banging my head against a stone for 2 days now, but I don't understand what's wrong!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
First Last, 2017-08-06
@bpGusar

Does he write an error?
First of all:

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'));
    }

What is this query for $category? And why in pluck are the cell names from 'posts'?
Secondly:
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',
        ]);

You have an update and it costs unique:posts
This confuses me, remove it and try to update the post
You can use update()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question