so how can I make a default checked/unchecked checkbox, with values from the data base? I'm using Form model from laravel collective and my checkbox field is this:
Form::model($role, ['route' => ['the_route', $role->slug], 'method' => 'patch'])
#foreach ($permissions as $permission)
Form::checkbox('permission['.$permission->slug.']', 'true', null, ['class' => 'square'])
#endforeach
Form::close()
The thing is that $role->permissions returns an array like this:
array:3 [
"dashboard.view" => "false"
"user.view" => "true"
"user.edit" => "false"
]
The third parameter is a boolean $checked, so you may write it like this:
Form::model($role, ['route' => ['the_route', $role->slug], 'method' => 'patch'])
#foreach ($permissions as $slug => $value)
Form::checkbox('permission['.$slug.']', 'true', (bool) $value, ['class' => 'square'])
#endforeach
Form::close()
Lavel Collective have one intriguing resource that is not documented, as least I never found it in any site. Name your checkbox with them same name that you gave for relation between your two models, like "permissions", and then Laravel Collective will check all input that are in that relation. In your specific case, $role->permission should return an model, not array, like normally is in any Laravel app.
Check an sample code:
{!! Form::model($role, ['route' => ['roles.update', $user->id], 'method' => 'put']) !!}
<div class="row form-group">
#foreach($permissions as $permission)
<div class="col-sm-3">
{!! Form::checkbox('permissions[]', $permission->id) !!}
{!! Form::label('permissions', $permission->name) !!}
</div>
#endforeach
</div>
{!! Form::close() !!}
// Role model
class Role extends Model
{
public function permissions()
{
return $this->belongsToMany(Permission::class, 'permission_role');
}
}
// Permission model
class Permission extends Model
{
public function roles()
{
return $this->belongsToMany(Role::class, 'permission_role');
}
}
Related
I'm new to Laravel. I've created a form and trying to delete one of the post. I just want to know how opening a form from Laravel collectives works.
Currently, this is how my form looks like in show.blade.php
{!! Form::open(['action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{!! Form::close() !!}
The above form gives me the error Action PostsController#destroy not defined.
But when I add 'url' => 'posts/' in Form i.e.
{!! Form::open(['url' => 'posts/', 'action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
The above error disappears.
Below is the destroy() function in PostsController
class PostsController extends Controller
{
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect('/posts')->with('success','Post removed');
}
}
web.php
// --- Posts Routing
Route::get('/posts', [App\Http\Controllers\PostsController::class, 'index'])->name('posts');
MY QUESTIONS
I'm redirecting in the destroy() function in PostsController, why url is necessary with action in Form from Laravel
collectives to avoid the above error?
When to use 'url' and when to use action?
I've laravel 4.2.10.
I'm trying to update an edit to a post without using the resource. I tried parsing the variable from my Form to my route using {id} but it gets ignored. This is the form I'm trying to post from.
{!! Form:: open(['action'=> ['ManageBooksController#updateBook', $book->id], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('Book_NAME', 'Name')}}
{{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::hidden('_method', PUT)}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
This is my route
Route::put('manageBooks', 'ManageBooksController#updateBook');
This is my method in my controller
public function updateBook(Request $request, $id)
{
$this->validate($request, ['Book_NAME' => 'required']);
$books = Books::find($id);
$books->Book_NAME =$request->input('Book_NAME');
$books->save();
return redirect('/manageBook')->with('success', 'Book Edited');
}
Your route is expecting a PATCH. Try updating your route to:
Route::post('/manageBooks/{id}', 'ManageBooksController#updateBook');
Or include the Laravel's #method('PATCH') within your form.
Also, your controller names don't match :)
Consider changing sequence of the arguments to the function:
public function updateBook($id, Request $request) // Notice the sequence of the arguments
{
......
}
If you want to use Route, you have to specifics like this
{!! Form:: open(['route'=> ['manage_book', $book->id], 'method' => 'POST']) !!}
In your route, you may need to name it properly
Route::post('/manageBooks/{id}', array('as'=>'manage_book','uses'=>'ManageBooksController#updateBook'));
Hope it helps.
Update without using resource :
your route :
Route::get('/manageBooks', 'ManageBooksController#whateverer')->name('manageBooks');
Route::post('/manageBooks/{id}/edit', 'ManageBooksController#updateBook')->name('updateBook');
your blade:
{!! Form:: open(['route'=> ['updateBook', $book->id], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('Book_NAME', 'Name')}}
{{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::hidden('id', $book->id)}} //hidden field is not required
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
your controller:
public function updateBook(Request $request, $id)
{
$this->validate($request, ['Book_NAME' => 'required']);
$books = Books::where('id',$id)->update(['Book_NAME'=>$request->Book_NAME]);
return redirect()->route('manageBooks')->with('success', 'Book Edited');
}
In the end I added another hidden field where i parse the ID through of the post I'm editing. I also changed the find method to take the request variable pointing to the ID.
My Form:
{{Form::hidden('Book_ID', $book->Book_ID)}}
{{Form::hidden('_method', PUT)}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
My function:
public function updateBook(Request $request)
{
$this->validate($request, ['Book_NAME' => 'required']);
$books = Books::find($request->Book_ID);
$books->Book_NAME =$request->input('Book_NAME');
$books->save();
return redirect('/manageBook')->with('success', 'Book Edited');
}
Change your method POST to PUT in your from first,
{!! Form:: open(['action'=> ['ManageBooksController#updateBook', $book->id],'method' => 'PUT']) !!}
<div class="form-group">
{{Form::label('Book_NAME', 'Name')}}
{{Form::text('Book_NAME', $book->Book_NAME, ['class' => 'form-control', 'placeholder' => 'Name'])}}
</div>
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!!Form:: close() !!}
Then You Have to Pass a parameter in your route, as your method expecting $id
Route::put('manageBooks/{id}/update', 'ManageBooksController#updateBook');
I'm learning Laravel, and I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car, which is identified with the modelesc variable. This form sends the data to a "orders" table. But I keep getting this error "Action App\Http\Controllers\orders not defined." on catalog.blade.php
This is the code that I have made.
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders($modelesc) {
$cars = DB::select('select * from cars where Model = modelesc');
return view('orders', compact('cars'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'orders', 'method' => 'GET')) !!}
{!! Form::hidden(modelesc, $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
{!! Form::open(array('action' => 'index', 'method' => 'POST')) !!}
{!! Form::text(Model, $modelesc) !!}
{!! Form::hidden(users_id, Auth::user()->id) !!}
{!! Form::hidden(Fabrication_date, date(Y-m-d)) !!}
{!! Form::select('colour', [
#foreach($colours as $colour)
'$colour->Colour' => '$colour->Colour'
#endforeach
]) !!}
{!! Form::hidden(Order_status_id, '1' !!}
{!! Form::close() !!}
This is the structure of the table 'orders'. The _id come from other tables, and I want to fill some values of the forms with this id's.
id,
users_id,
Model,
Fabrication_date,
Colour_id,
Order_status_id
Laravel does not know where to find the action orders, because you did not specify a controller (see the thrown exception: it is looking for some class named orders in the namespace App\Http\Controllers\). To do so, you just need to replace
{!! Form::open(array('action' => 'orders', 'method' => 'GET')) !!}
with
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
If you want to use a route instead (e.g. you have defined a route named orders which links to a Controller action) you need to replace action with route in the Form::open method:
{!! Form::open(array('route' => 'orders', 'method' => 'GET')) !!}.
Controller
public function store( Request $request,$id)
{
$new = Car::find($id);
$new->status = $request ->input('field');
$new->save();
redirect('home');
}
View
#foreach($users as $user)
#foreach($user->cars as $users)
{!! Form::open( ['route' => 'user.store', 'method'=>'post','id'=> '$users->id']) !!}
<th scope="row">1</th>
<td>{!! Form::label($cars->name)!!}<td>
<td>{!! Form::label($cars->age)!!}<td>
<td>{{Form::select($cars->status, ['R' => 'Requested', 'C' => 'Coming', ['name' => 'field']])}} // name is worong but I dont know the alternative
<td>{!! Form::submit('Update Profile', ['class' => 'btn btn-primary']) !!}</td>
{{ Form::close() }}
Route
Route::Resource('user', 'UserController');
Problem
Trying to save the selected value from status in Car model. but I get an error about the parameters. Can someone show me where my mistake is? I'm new to Laravel.
You can't pass additional data to store action this way.
You can look at this route by using php artisan route:list command. As you can see, it doesn't expect and doesn't pass any data.
So, you need to pass ID in hidden input:
{!! Form::hidden('userId', $user->id) !!}
And get data in controller with $request->userId
Don't forget to remove $id from store() and $users->id from Form::open()
Also, correct syntax (with fixed typos) for Form::open() is:
{!! Form::open(['method' => 'post', 'route' => 'user.store']) !!}
I stumbled upon a problem while creating a CRUD application in Laravel 5. In the edit section of a product I have 2 inputs that are populated from 'products' table and I have a dropdown list that needs to be populated with data from the categories table. The problem is that I don't know how to aproch the dropdown list. I generated the Form::model for the two inputs but I'm stuck at the dropdown list.
Controller
public function edit($id)
{
$products_edit = products::find($id);
return View::make('products.edit')->with('products_edit', $products_edit);
}
View
{!! Form::model($products_edit, array('route' => array('products.update', $products_edit->id), 'method' => 'PUT')) !!}
<div class="form-group">
{!! Form::label('name', 'Nume') !!}
{!! Form::input('text', 'name', $products_edit->product_name, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('price', 'Cost') !!}
{!! Form::input('number', 'price', $products_edit->product_price, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label('category', 'Categorie') !!} <br />
{!! Form::select('category', <!-- insert var here -->, array('class' => 'form-control') !!}
</div>
You can do this at your controller:
public function edit($id)
{
$products_edit = Product::findOrFail($id);
$categories = Category::lists('name', 'id');
return view('product.edit', compact('products_edit', 'categories'));
}
In the view you still make the form model bind:
{!! Form::model($products_edit, array('route' => array('products.update', $products_edit->id), 'method' => 'PUT')) !!}
And to create the category dropdown try this:
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
your can do thi controller:
public function edit($id)
{
$products_edit = Product::find($id);
$categories = Category::pluck('name', 'id');
return view('product.edit', compact('products_edit', 'categories'));
}