i stack with livewire Update with checkbox array please help.
Form
<form wire:submit.prevent="Update" class="py-3 mb-6">
#foreach ($permissions as $key => $pms)
<input type="checkbox" model:wire="PermissionCheckbox.{{ $key }}" {{ in_array($pms->id , $PermissionCheckbox)? "checked":"" }} />
#endforeach
<button class="btn btn-primary" type="submit">Update</button>
</form>
Php Function
public function Edit($id){
$role = Role::find($id);
$this->roleId = $role->id;
$this->roleName = $role->name;
$this->PermissionCheckbox = json_decode($role->permissions->pluck('id'));
}
public function Update(){
$this->validate([
'roleName' => 'sometimes|required|unique:roles,name,'.$this->roleId,
'PermissionCheckbox' => 'required'
]);
dd($this->PermissionCheckbox);
}
The result not update with the PermissionCheckbox.
Thank you in advance guys.
check your input because it has the wire model inverted
the correct is wire:model
<input type="checkbox" wire:model="PermissionCheckbox.{{ $key }}" {{ in_array($pms->id , $PermissionCheckbox)? "checked":"" }} />
Related
Controller article
public function update(Request $request, Article $article){
$article->update($request->except('slug', 'image_path'));
if ($request->hasFile('image_path')) {
$image = $request->file('image_path');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
$article->image_path = $new_name;
$article->save();
}
$article->categories()->detach();
if ($request->has('categories')) {
$article->categories()->attach($request->input('categories'));
}
$user=auth()->user();
return redirect()->back()->with('message', 'Raksts atjaunots!');
}
public function edit(Article $article){
$user=auth()->user();
return view('edit',[
'article' => $article,
'categories' => Category::with('children')->where('parent_id',0)->get(),
'delimiter' => ''
]);
}
edit blade
<form class="form-horizontal" action="{{route('update', $article)}}" method="post" enctype="multipart/form-data" style="display: flex;justify-content: center;flex-direction: column;">
<input type="hidden" name="_method" value="put">
{{ csrf_field() }}
{{-- Form include --}}
<img src="{{URL::to('/images').'/'.$article->image_path}}" alt="">
#include('partials.form')
<input type="hidden" name="modified_by" value="{{Auth::id()}}">
</form>
link to the form
<tbody>
#foreach ($articles_suggest_user as $article)
<a class="btn btn-default" href="{{route('edit', $article)}}"><i class="fa fa-edit"></i></a>
<?php } ?>
#endforeach
</tbody>
web.php
Route::get('/home/edit/{id}','Controller_Article_parents#edit', function () {
return view('/home/edit');
})->name('edit');
Route::get('/home/update/','Controller_Article_parents#update', function () {
return view('/home');
})->name('update');
When i clicked the link, i move to for example this url http://127.0.0.1:8000/home/edit/190
But my form is empty... input empty. How I can do when I open form it's display me input information ?
When click my link its display me form, but form is empty.
form example
<div class="rtq">
<label for="parent_id">Cat</label>
<span class="required">*</span>
<select
id="parent_id"
class="form-control"
name="categories[]"
multiple=""
required
>
#include('admin.categories.partials.categories',
['categories' => $categories,
'current' => $article,
'delimiter' => $delimiter])
</select>
</div>
<div class="rtq">
<label for="">Descript</label>
<textarea
name="description_short"
class="form-control"
id="description_short"
>
{{ $article->description_short ?? "" }}
</textarea>
</div>
It my form . Mini example
If you want to redirect with form input, you can use the withInput() function
return back()->withInput();
https://laravel.com/docs/7.x/redirects#creating-redirects
Now, this function is designed to be used with form validation and may not be what you want.
What I have done in the past when combining create & edit views into a single template is something like this:
{!! Form::text('name', isset($model) ? $model->name : null, [
'class' => 'form-control',
'placeholder' => 'Please fill out this field &hellips;',
'required' => true
]) !!}
This uses the Laravel Collective HTML library. However the principle is the same if you are using raw hmtl like so:
<input type="text" value="{{ isset($model) ? $model->name : null }}">
Round 2 Electric Boogaloo
You aren't returning a variable called $article to your edit.blade.php.
Round 3 Apple Tree
Originally, you appeared to be calling both a controller action AND also a callback function. Stick to the controller action only like so:
Route::get('/home/edit/{id}','Controller_Article_parents#edit')->name('edit');
Then within your edit() function on the Controller you will want to do this:
public function edit ($id) {
return view('/home/edit', [
'article' => Article::find($id)
]);
}
I am trying to update just one column from'business_account' table. I tried something like bellow, When trying to get the form value to my 'packPurchasedMembers' controller function I am getting null value. What should be the right code to getting expected result.
Route::group(['prefix' => 'super', 'middleware' => 'super', 'as' => 'super.'], function () {
Route::post('verify-account', array('as' => 'verify-account', 'uses' => 'packPurchasedMembers#postVerifyAccount'));});
My Controller-
public function postVerifyAccount(Request $request){
$uid = $request->get('userid');
$verfiy = $request->get('verification');
DB::table('business_account')
->where('user_id', $uid)
->update(['verified' => $verfiy]);}
My Form -
<div class="pull-left">
<h4>Verify Account</h4>
#foreach ($verification as $verify)
<form action="{{ url('super/verify-account') }}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="userid" value="<?php echo $uid; ?>" />
<input type="radio" id="reinv1" name="verification" value="0"
<?php if ($verify->verified == '0') echo 'checked' ?> >
<label for="reinv1"> Not Verified</label>
<input type="radio" id="reinv2" name="verification" value="1"
<?php if ($verify->verified == '1') echo 'checked' ?> >
<label for="reinv2"> Verified</label>
<button type="submit" class="btn btn-success" value="Submit">Submit</button>
</form>
#endforeach
</div>
Please use the following function to retrieve input value
public function postVerifyAccount(Request $request){
$uid = $request->input('userid');
$verfiy = $request->input('verification');
DB::table('business_account')
->where('user_id', $uid)
->update(['verified' => $verfiy]);
}
Just in case if anyone lands up to this question. I would like to address the solution for the above question.
So the problem was he had not used the proper HTTP request. Instead of using POST request he was using GET request.
In routes/web.php
The request method is changed from GET -> POST.
Hope it may help someone.
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.
I have the following controller, which sends data to a Laravel Blade view:
Controller:
public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}
In that Laravel blade view there is a form:
<form method="GET" action="{{ route('invoices.choose-periods') }}">
<div class="form-group {{ $errors->has('school') ? 'has-error' : '' }}">
<label>School</label>
<select id="school" class="form-control" name="school[]" multiple size="{{ $schools->count() }}" required>
#foreach ($schools as $school)
<option value="{{ $school->id }}">{{ $school->name }}</option>
#endforeach
</select>
#if ($errors->has('school'))
<span class="help-block">
<strong>{{ $errors->first('school') }}</strong>
</span>
#endif
</div>
<button type="submit" class="btn btn-success btn-sm pull-right">Submit</button>
</form>
As you can see from the HTML, the form is a multi-select form, with the resulting data stored in a school[] array.
On submission of the form, I do a test die and dump on request('school') and see that for every option I have selected, the value seems to have been logged twice. For example, choosing only one option gives me:
array:2 [▼
0 => "15"
1 => "15"
]
Any ideas? Thanks!
I have only worked on laravel 5.7. Try this It is working for me.
Since you are passing 2 objects
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
It is obvious you will get 2 errors.
In your controller change this
public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}
to this
public function create(){
$schools = School::all()->sortBy('school_type');
return view('invoices.create', ['schools' => $schools]),
]);
}
I'm trying to make something that I'm not sure if is possible and how exactly can happen.
What I want is to have one table which is in form and one addition form inside. The depending of which button I hit to perform different actions in controller. Here is what I have so far
my blade
{{ Form::open(array('url' => 'admin/inv')) }}
{{ Form::open(array('url' => 'admin/inv/multiPC')) }}
<table class="table table-bordered">
<tbody>
<tr>
<td><input type="checkbox" name="delete[]" value="{{ $product->product_id }}"> </td>
<td><strong>${{ $product->price }}</strong><input type="number" name='price[]' class="form-control"/></td>
</tr>
</tbody>
</table>
<button type="submit" href="{{ URL::to('/admin/del') }}?_token={{ csrf_token() }}">Delete</button>
<button type="submit" href="{{ URL::to('/admin/multiPC') }}?_token={{ csrf_token() }}">Update Price</button>
{{ Form::close() }}
{{ Form::close() }}
Those are both functions
public function pDelete() {
$delete = Input::only('delete')['delete'];
$pDel = Product::whereIn('product_id', $delete)->delete();
return Redirect::to('/admin/inv')->with('message', 'Product(s) deleted.');
}
public function priceUpdate() {
$pchanges->price = Input::only('price')['price'];
$pChange = Product::whereIn('product_id', $pchanges);
$pChange->save();
return Redirect::to('/admin/inv')->with('message', 'Product(s) price changed.');
}
And route
Route::post('/admin/inv', ['uses' => 'AdminController#pDelete', 'before' => 'csrf|admin']);
Route::post ('/admin/inv/multiPC', ['uses' => 'AdminController#priceUpdate', 'before' => 'csrf|admin'])
What happen is when I check product and hit Delete button product is deleted. But when I input price in the input field for price and hit Update Price page only refreshed and price isn't changed.
Is there a way to accomplish this without using JS?
try this type of approach
<form method="POST" class="form-horizontal" action="myapplication/personal">
<input type="number" name='price[]' class="form-control"/>
<input type="checkbox" name="delete[]" value="{{ $product->product_id }}">
<button type="submit" name="step[0]" value="Delete">Delete</button>
<button type="submit" name="step[1]" value="Update">Update Price</button>
</form>
from your controller check the value of step and do as you like
public function formProcess() {
$action = request::get('step'); // i forgot laravel 4 syntex. used laravel 5 instead here :D
if($action == 'Delete')
{
// do delete operation
}
else
{
//do update operation
}
}
hope this helps