Im Trying to update my table through Laravel Updation,i get no error when i tried to print the result i just got an null value ,i have pasted my model and controller.
controller
public function update_FAQ_submit()
{
$data = Input::except(array(
'_token'
));
$rule = array(
'faq_ques' => 'required',
'faq_ans' => 'required'
);
$id = Input::get('id');
$validator = Validator::make($data, $rule);
if ($validator->fails()) {
return Redirect::to('Coin_lion/FAQ')->withErrors($validator->messages())->withInput();
} else {
$entry = array(
'faq_ques' => Input::get('faq_ques'),
'faq_ans' => Input::get('faq_ans')
);
$faq=FAQ::update_faq($id, $entry);
return Redirect::to('Coin_lion/manage_FAQ');
}
}
Model
public static function update_faq($id, $entry)
{
return DB::table('faq')->where('id', '=', $id)->update($entry);
}
Routes:
Route::post('Coin_lion/update_FAQ_submit','AdminController#update_FAQ_submit');
my DB
My form
here is my form ,i have included what method i used previously
">
{!! Form::hidden('id', $id) !!}
#foreach ($faq as $info)
<label>Enter your question:</label><br>
<input type="text" class="form-control" name="faq_ques" value="<?php echo $info->faq_ques ?>"><br>
<label>Enter your Answer:</label>
<textarea class="form-control" name="faq_ans" cols="10" rows="5" ><?php echo $info->faq_ans ?></textarea>
<input type="submit" class="btn btn-success" name="submit" value="ADD">
<input type="reset" class="btn btn-danger" name="submit" value="Cancel">
#endforeach
</body>
First try to check that you are getting values in your controller and passing it correctly in you model then you need to replace your query with this. And replace your model code with this.
public function update_faq($id, $entry)
{
DB::table('faq')->where('id',$id)->update($entry);
}
public static function update_faq($id, $entry) {
DB::table('faq')
->where('id', $id) // find your faq by their id
->update($entry); // update the record in the DB.
}
Related
the problem is when i click on submit button of the form it appears
The category.0.abbr field is required
This is my request code
public function authorize()
{
return true;
}
public function rules()
{
return [
'photo' => 'required_without:id|mimes:jpg,jpeg,png',
'category' => 'required|array|min:1',
'category.*.name' => 'required',
'category.*.abbr' => 'required',
'category.*.active' => 'required',
];
}
And this is my code in controller file
public function store(MainCategoryRequest $request)
{
//return request;
$main_categories = collect($request -> category);
$filter = $main_categories->filter(function ($value,$key){
return $value['$abbr'] == get_default_lang();
});
return $default_category = array_values($filter -> all()) ;
}
And this is my html code
<form class="form" action="{{route('admin.maincategories.store')}}" method="POST" enctype="multipart/form-data">
#csrf
{{__('messages.'.$lang -> abbr)}}</label>
<input type="text" value="" id="abbr" class="form-control"placeholder=""name="category[{{$index}}][abbr]">
#error("category.$index.abbr")
<span class="text-danger">{{$message}}</span>
#enderror
<button type="submit" class="btn btn-primary">
<i class="la la-check-square-o"></i> save
</button>
It need to be like this
<input type="text" value="" id="abbr" class="form-control"placeholder=""name="category[{{$index}}][abbr]">
I have a small problem with my Controller action. I can't update my "link" in Database, bt dd method work is correctly when I'm try to check data.
Form
<form class="col-lg-push-6" action="/admin/links/{{$link->id}}/update" method="POST">
#csrf
<div class="form-group bmd-form-group">
<label class="bmd-label-floating">New Link Value</label>
<input type="text" class="form-control" size="100" name="value">
<button class="btn btn-primary" type="submit">Update</button>
</div>
</form>
Controller
public function update(Request $request, $id)
{
$this->validate($request, [
'value' => 'required'
]);
$link=RedirectUrl::AllLinks()->where('id', $id);
$link->value = $request->input('value');
return redirect()->back()->with('message', 'Link Updated!');
}
Model
public function scopeAllLinks($query){
return $query->get();
}
Route
Route::prefix('admin')->middleware('auth')->group(function(){
Route::get('/', 'Admin\IndexController#index');
Route::get('dashboard', 'Admin\IndexController#index')->name('dashboard');
Route::get('links', 'Admin\LinkController#index')->name('links');
Route::get('links/{id}', 'Admin\LinkController#linkById');
Route::post('links/{id}/update', 'Admin\LinkController#update');
});
Few things here:
Your scopeAllLinks scope is incorrect, you don't call get inside a scope, instead you return the query builder instance.
You can use find since you're passing in a record id:
$link = RedirectUrl::find($id);
You never call save or update on the record:
$link->value = $request->input('value');
$link->save(); // <--- add this
#extends('layouts.admin')
#section('content')
#foreach($galerys as $data)
<form method="post" enctype="multipart/form-data" action="{{url('/ubahGaleryPost')}}">
{{ csrf_field() }}
<h4>Ubah Gambar</h4>
<input name="id" id="id" type="hidden" value="{{$data->id}}" >
<h5>Gambar</h5>
<input name="gambar" id="gambar" type="file">
<h5>Deskripsi</h5>
<input name="deskripsi" id="deskripsi" class="form-control" type="text" value="{{$data->deskripsi}}">
<button type="submit" class="btn btn-primary">Ubah</button>
</form>
#endforeach
#endsection
controller
public function ubahGalery($id){
$data = Galery::findOrFail($id);
return view('admin.updategalery',['galerys'=>$data]);
}
public function ubahGaleryPost(Request $request){
$data = Galery::where('id',$request->id)->first();
if (empty($request->file('gambar'))){
$data->gambar = $data->gambar;
}
else{
unlink('img/galery/'.$data->gambar);
$file = $request->file('gambar');
$ext = $file->getClientOriginalExtension();
$newName = rand(100000,1001238912).".".$ext;
$file->move('img/galery',$newName);
$data->gambar= $newName;
}
$data->deskripsi = $request->deskripsi;
$data->update();
return redirect('/galeryAdmin');
}
$data = Galery::findOrFail($id); this will return Galery object instead of a Collection of Galery (except if $id is an array).
So in your HTML you don't need a foreach and instead could just do $data->id, $data->deskripsi etc.
You don't need to loop on galerys as you are fetching only one result so you don't need to loop on your result so instead of that just fetch data directly.
Like this,
$galerys->deskripsi
Hope this helps:)
This is my route :
Route::post('/store', 'EditlinkController#store');
My controller
public function index()
{
$id = $_GET['id'];
$links = DB::table('slugs')->where('slugs.id',$id)
->join('links','links.sid','=','slugs.id')
->get();
return view('editlink', ['links' => $links]);
}
public function store(Request $request, $id)
{
$url = $request->input('url');
$data =new link;
$sid = $id;
$data->sid = $sid;
$data ->url = $url;
$data ->save();
return redirect('/links');
}
And my view:
<form role="form" method='POST' action="{{url('store')}}">
<div class="entry input-group col-xs-3">
<input class="form-control" name="url" type='url' placeholder="https://..." size="100"/>
<input type="hidden" name="_Token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary" type="button">
<span class="material-icons md-12">add</span>
</button>
{{ csrf_field() }}
</div>
</form>
So basically here I want to call $id from index to store. I also have tried
Route::post('/store/{id}', 'EditlinkController#store');
But still doesn't work. Thank you.
Your route, /store/{id}, requires you to have a route parameter. Make sure you have an $id available to you before you generate your url for your form.
An example of what your open form tag should look like with the $id included:
<form role="form" method='POST' action="{{url('store', $id)}}">
I'm assuming the view editlink is where the markup for the form resides. If so, then you can simply pass the value of the id to your view from your controller:
return view('editlink', ['links' => $links, 'id' => $id]);
Im new in Laravel and i dont understand how to make action forms and routing for editing post
Here is my routes --
Route::post('/menu', 'MenuController#store');
Route::resource('/menu', 'MenuController');
here is controller --
public function update(Request $request, $id)
{
$this->validate($request, [
'menuName' => 'required',
'menuLink' => 'required'
]);
//create new menu
return $id;
// $menus->name = $request->input('menuName');
//$menus->link = $request->input('menuLink');
//$menus->save();
//return redirect('/menu')->with('success', 'Menu updated');
}
Return $id is for check what "id" will give me and he gives me this- "{id}"
here is form --
<form action = "/menu/{id}" method = "POST">
{{ csrf_field() }}
<input name = "_method" type = "hidden" value = "PUT">
<input type="text" id="menuName" name="menuName" class="input-block-level" placeholder="Menu name">
<input type="text" id="linkName" name="menuLink" class="input-block-level" placeholder="Menu link ">
<button type="submit" class="btn btn-success pull-right">Submit</button>
</form>
i am really messed up allready and dont know what i am doing wrong. I cant understand why update funtion returns me "{id}" not value of ID and how can i make this all works
Your are not passing id in your view , you are passing it as a string , u need it create a variable and set it to the right id and pass it to your form.
Change your form action from
action = "/menu/{id}"
to
action = "/menu/".$id"
or you can use laravel blade
Form::open(['route' => ['menu.update', $id]])
and don't forget to close the form at the end
{!!Form::close!!}
You did something wrong with the action attribute. Change it to:
action="/menu/{{ $id }}"
Okay,
you have an edit method which will show the form, that method should return the menu object
public function update($id)
{
// get the menu you wan to edit
$menu = Menu::find($id);
// return the form with the menu object
return view('your.form.view', compact('menu'));
}
Now the form should be like this
// use that object u returned in the form action
<form action = "/menu/{{ $menu->id }}" method = "POST">
{{ csrf_field() }}
<input name = "_method" type = "hidden" value = "PUT">
<input type="text" id="menuName" name="menuName" class="input-block-level" placeholder="Menu name">
<input type="text" id="linkName" name="menuLink" class="input-block-level" placeholder="Menu link ">
<button type="submit" class="btn btn-success pull-right">Submit</button>
</form>
Now the update method should be something like this
public function update(Request $request, $id)
{
$this->validate($request, [
'menuName' => 'required',
'menuLink' => 'required'
]);
// update the data
$bool = Menu::where('id',$id)->update([
'menuName'=> $request->menuName,
'menuLink'=> $request->menuLink,
]);
if(!$bool){
Session::flash('alert','error');
return view('page.index');
}
Session::flash('alert','success');
return view('page.index');
}
This first method is "get" edit that should show the form,
the second is put update that should receive the data from the form(after the show method) and use i to update.
I resolved the error ---
in update blade befor form i added foreach
<div class="container">
<div class="row">
#foreach($menus as $menus)
<form action="/menu/{{$menus->id}}" method="POST">
#endforeach
{{ csrf_field() }}
<input name = "_method" type = "hidden" value = "PUT">
<input type="text" id="menuName" name="menuName" class="input-block-level" placeholder="Menu name">
<input type="text" id="linkName" name="menuLink" class="input-block-level" placeholder="Menu link ">
<button type="submit" class="btn btn-success pull-right">Submit</button>
</form>
</div>
</div>
But now he is editing only 1st post and no matter wich post edit im switching
here is controller --
public function edit($id)
{
$menus = Menu::all();
return view('admin.editmenu')->with('menus', $menus);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'menuName' => 'required',
'menuLink' => 'required'
]);
//create new menu
$menus = Menu::find($id);
$menus->name = $request->input('menuName');
$menus->link = $request->input('menuLink');
$menus->save();
return redirect('/menu')->with('success', 'Menu updated');
}
menu list blade ---
#if(count($menus) > 1)
#foreach($menus as $menus)
<tr>
<th scope="row">1</th>
<td>{{$menus->name}}</td>
<td>{{$menus->link}}</td>
<td>{{$menus->created_at}}</td>
<td>{{$menus->updated_at}}</td>
<td>
<button type = "button"class = "btn btn-outline-danger btn-sm">Delete</button>
<a href = "/menu/{{$menus->id}}/edit" class = "btn btn-outline-warning btn-sm">Edit</button>
</td>
</tr>
#endforeach
#else
Ok ive got it and resolved.
I will start with routes
routes----------
Route::resource('menu', 'MenuController');
that post route was unnecessary, but that didnt affect anything
controller -------
public function edit($id)
{
$menus = Menu::find($id);
return view('admin.editmenu')->with('menus', $menus);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'menuName' => 'required',
'menuLink' => 'required'
]);
//create new menu
$menus = Menu::find($id);
$menus->name = $request->input('menuName');
$menus->link = $request->input('menuLink');
$menus->save();
return redirect('/menu')->with('success', 'Menu updated');
}
Where in edit function i must find ID witch needs to be editing
menu list -----------------
#if(count($menus) > 1)
#foreach($menus as $menu)
<tr>
<th scope="row">1</th>
<td>{{$menu->name}}</td>
<td>{{$menu->link}}</td>
<td>{{$menu->created_at}}</td>
<td>{{$menu->updated_at}}</td>
<td>
<button type = "button"class = "btn btn-outline-danger btn-sm">Delete</button>
<a href = "/menu/{{$menu->id}}/edit" class = "btn btn-outline-warning btn-sm">Edit</button>
</td>
</tr>
#endforeach
#else
<p class = "well"> No menu items created!</p>
#endif
I only changed $menus as $menus on "$menus as $menu" to be clear.
edit blade ------------
#extends('admin.main')
#section('content')
<div class="container">
<div class="row">
<form action="/menu/{{$menus->id}}" method="POST">
{{ csrf_field() }}
<input name = "_method" type = "hidden" value = "PUT">
<input type="text" id="menuName" name="menuName" class="input-block-level" placeholder="Menu name" value="{{ $menus->name }}">
<input type="text" id="linkName" name="menuLink" class="input-block-level" placeholder="Menu link " value="{{ $menus->link }}">
<button type="submit" class="btn btn-success pull-right">Submit</button>
</form>
</div>
</div>
#endsection
Here i deleted #foreach like #Snapey told and added the value option.
I guess value option gave me the errrors all the time.
Thank you all for attention and willing to help! Hope that someday i could help others with my knowledge.