Answer the question
In order to leave comments, you need to log in
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'));
}
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');
}
{!! 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() !!}
Answer the question
In order to leave comments, you need to log in
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'));
}
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',
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question