so i make laravel form for update
Index.blade
<div class="col-sm-12">
<div class="formrow row">
<div class="form-group">
<div class="divlabel col-sm-2">
<label>Kode Program Studi:</label>
<span class="required">*</span>
</div>
<div class="divinput col-sm-8">
<select id="id" data-plugin-selectTwo class="form-control populate placeholder" title="kode program studi harus diisi" name='id' required data-plugin-selecTwo>
<option value="">-PILIH NAMA USER-</option>
#foreach ($users as $user)
<option class="form-control" value = '{{$user->id}}'>{{$user->id.' | '.$user->name}}</option>
#endforeach
<label class="error" for="id"></label>
</select>
</div>
</div>
{!! Form::open(['url' => 'master/hakakses/'.$user->id,'method' => 'PATCH','class'=>'update']) !!}
<!-- {!! Form::model($user,['route'=>['master.hakakses.update', $user->id],'method' => 'PATCH','class'=>'update']) !!} -->
<div class="form-group">
<div class="divlabel col-sm-2">
<label>Kode Program Studi:</label>
<span class="required">*</span>
</div>
<div class="divinput col-sm-8">
<select id="role" data-plugin-selectTwo class="form-control populate placeholder" title="kode program studi harus diisi" name='role_id' required data-plugin-selecTwo>
<option value="">-PILIH HAK AKSES-</option>
#foreach ($roles as $role)
<option class="form-control" value = '{{$role->id}}'>{{$role->id.' | '.$role->role_akses.' | '.$role->role_name}}</option>
#endforeach
<label class="error" for="role"></label>
</select>
</div>
</div>
</div>
<div class="col-sm-offset-4 col-sm-50">
<input type="submit" value="Ubah" name = 'simpan' class = 'btn btn-primary'>
<td>Kembali</td>
</div>
</div>
{!! Form::close() !!}
Controller :
public function index()
{
$data=new HakAkses;
$users= $data->ListUser();
$roles= $data->ListRole();
return view ('Master.HakAkses.index',compact ('users','roles'));
}
public function update(Request $request, $id)
{
return $id;
}
Model:
public static function ListUser()
{
$table = DB::select( DB::raw("SELECT * FROM users"));
return $table;
}
public static function ListRole()
{
$table = DB::select( DB::raw("SELECT * FROM m_role"));
return $table;
}
The problem is I can't get the value of {{$role->id}} when I try to return $id , the value is the latest input of id in the database. I think the problem is in the FORM:
{!! Form::open(['url' => 'master/hakakses/'.$user->id,'method' => 'PATCH','class'=>'update']) !!}
Can anyone please help me?
I see the problem is that you're passing here:
{!! Form::open(['url' => 'master/hakakses/'.$user->id,'method' => 'PATCH','class'=>'update']) !!}
the $user->id but you want to get in your controller {{$role->id}}
So the solvation is in your controller to:
public function update(Request $request, $id)
{
// $id is a user's id
$roleId = $request->input('role_id');
}
Related
For example if there is a form which creates customers, and when you filled the form and submit the page should redirect and show the data which was inserted to that form by a specific individual id. in my case it redirects to a page and views all the data in the database.
Here's my Web.php
Route::get('customers','CustomersController#index');
Route::get('customers/create','CustomersController#create');
Route::post('customers','CustomersController#store');
Route::get('customers/{customer}','CustomersController#show');
Here's my Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Customer;
use App\Company;
class CustomersController extends Controller
{
public function index(){
$customers = Customer::all();
return view('customers.index',compact('customers'));
}
public function create(){
$companies = Company::all();
return view ('customers.create',compact('companies'));
}
public function store()
{
$data = request()->validate([
'name'=>'required | min:3',
'email'=>'required | email',
'active'=>'required ',
'company_id'=>'required',
]);
Customer::create($data);
return redirect('customers');
}
// * Route Model binding
public function show(Customer $customer){
return view('customers.show',compact('customer'));
}
}
Here's my Customer Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $guarded = [];
public function getActiveAttribute($attribute){
return [
0 => 'Inactive',
1 => 'Active',
] [$attribute];
}
public function scopeActive($query){
return $query->where('active',1);
}
public function scopeInactive($query){
return $query->where('active',0);
}
public function company()
{
return $this->belongsTo(Company::class);
}
}
Here's my Create Blade
#extends('layouts')
#section('title','Add New Customer')
#section('content')
<div class="row">
<div class="col-12">
<h1>Add New Customer</h1>
</div>
</div>
<div class="row">
<div class="col-12">
<form action="/customers" method="POST" >
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{old('name')}}">
<div>{{$errors->first('name')}}</div>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" name="email" value="{{old('email')}}">
<div>{{$errors->first('email')}}</div>
</div>
<div class="form-group">
<label for="">Customer Status</label>
<select name="active" id="active" class=" form-control">
<option value="" disabled>Select Customer Status</option>
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
</div>
<div class="form-group">
<label for="company_id">Company</label>
<select name="company_id" id="company_id" class=" form-control">
#foreach ($companies as $company)
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class=" btn btn-dark">Add Customer</button>
#csrf
</form>
</div>
</div>
#endsection
Here's my show blade
#extends('layouts')
#section('title','Details for ' . $customer->name)
#section('content')
<div class="row">
<div class="col-12">
<h1>Details for {{ $customer->name }}</h1>
</div>
</div>
<div class="row">
<div class="col-12">
<p><strong>Name : </strong> {{ $customer->name }}</p>
<p><strong>Email : </strong> {{ $customer->email }}</p>
<p><strong>Company : </strong> {{ $customer->company->name }}</p>
<p><strong>Status : </strong> {{ $customer->active }}</p>
</div>
</div>
#endsection
Redirect to the show route rather than the customers index route:
$customer = Customer::create($data);
return redirect('customers/' . $customer->id);
I am trying to pull data from a form and as per my screenshot below the data will not display, but it appears to be functioning to a certain extent as there are two users which exist in my instructor variable and there are two options to select from, can anyone help?
create.blade.php
#extends('layouts.app')
#section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Course</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
#csrf
<div class="form-group">
#if (Auth::user()->isAdmin())
{!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
{!! Form::select('Instructor[]', $Instructor, Input::get('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
#if($errors->has('Instructor'))
<p class="help-block">
{{ $errors->first('Instructor') }}
</p>
#endif
</div>
<div class="form-group">
<label class="required" for="name">Course Title</label>
<input class="form-control {{ $errors->has('title') ? 'is-invalid' : '' }}" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
#if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
#endif
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</div>
#endif
</form>
</div>
</div>
#endsection
CoursesController
protected function create()
{
{
$Instructor = \App\User::whereHas('role', function ($q) { $q->where('role_id', 2); } )->get()->pluck('title', 'id');
// $courses = Course::all()->pluck('title');
return view('admin.courses.create', compact('Instructor'));
}
}
User.php
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsToMany(Role::class, 'role_user');
}
public function isAdmin()
{
return $this->role()->where('role_id', 1)->first();
}
public function roles(){
return $this->belongsToMany('App\Role');
}
Thanks for any help.
The Laravel Collective Form::select requires the values to be in a [ key => value ] array pair. You are passing it a result collection.
You need to translate the result of $Instructor = \App\User::whereHas('role', function ($q) { $q->where('role_id', 2); } )->get()->pluck('title', 'id'); to an array that is (I assume) [ id => title, id => title ]...
So, before you pass it to your view something like this:
$flatInstructors = []
$Instructor->each(function($item, $key) {
$flatInstructors[$item->id] = $item->title;
});
return view('admin.courses.create', compact('flatInstructors'));
And use $flatInstructors in your Form::select generation.
I am trying to pass users with a specific role_id through a form on a create.blade.php see below;
#extends('layouts.app')
#section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Course</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
#csrf
<div class="panel-body">
#if (Auth::user()->isAdmin())
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
{!! Form::select('Instructor[]', $Instructor, old('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
<p class="help-block"></p>
#if($errors->has('Instructor'))
<p class="help-block">
{{ $errors->first('Instructor') }}
</p>
#endif
</div>
</div>
#endif
<div class="form-group">
<label class="required" for="name">Course Title</label>
<input class="form-control {{ $errors->has('title') ? 'is-invalid' : '' }}" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
#if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
#endif
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</form>
</div>
</div>
#endsection
User.php
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsToMany(Role::class, 'role_user');
}
public function isAdmin()
{
return $this->role()->where('role_id', 1)->first();
}
public function roles(){
return $this->belongsToMany('App\Role');
}
CoursesController.php
My create function:
protected function create()
{
{
$Instructor = \App\User::whereHas('role', function ($q) { $q->where('role_id', 2); } )->get()->pluck('title', 'id');
// $courses = Course::all()->pluck('title');
return view('admin.courses.create', compact('courses'));
}
}
The following error is being returned:
Undefined variable: Instructor (View:
C:\xampp\htdocs\test\resources\views\admin\courses\create.blade.php)
I am not sure where I have gone wrong. Thanks for any help.
Referring to my latest comment:
In the controller, you missing pass Instructor to your view:
return view('admin.courses.create', compact('courses', 'Instructor'));
I have edit page contains 3 fields company, dealership, branch, when I click one of the save list of the branch I need to show the details into the page but when I did this it shows undefined variable error throwing up, how to fix this and also need to update the form after listing the details in the corresponding fields
Edit Page
#include('theme.header')
<?php
use App\Company;
?>
<div class="page-content-wrapper ">
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div class="page-title-box">
<div class="btn-group float-right">
</div>
<h4 class="page-title">Branch Management</h4>
</div>
</div>
</div>
<!-- end page title end breadcrumb -->
<div class="row">
<div class="col-12">
<div class="card m-b-30">
<div class="card-body">
<h4 class="mt-0 header-title">Branch</h4>
<br>
<br>
{!! Form::open(['method' => 'PUT', 'route' => ['branchs.update',$branch->id]] ) !!}
<div class="form-group row">
<label class="col-sm-2 col-form-label">Company</label>
<div class="col-sm-10">
<?php
$comp=Company::where('comp_id',$branch->comp_id)->first();
$companies=Company::where('status','0')
->get();
?>
<input type="hidden" name="br_id" id="br_id" value="{{$branch->br_id}}">
<select class="form-control" id="company" name="company">
<option selected value="{{$branch->br_id}}">{{$comp->name}}</option>
#foreach($companies as $company)
<option value="{{$company->comp_id}}">{{$company->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Dealership</label>
<div class="col-sm-10">
<?php
$cn = App\Dealership::where('dlr_id', $branch->dlr_id)->first();
$companies =App\Dealership::where('status', '0')
->get();
?>
<select class="form-control" id="dealer" name=" dealer">
<option>Select Dealership</option>
#foreach($dealership as $dealerships)
<option value="{{$dealerships->dlr_id}}">{{$dealerships->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label for="example-text-input" class="col-sm-2 col-form-label">Branch Name</label>
<div class="col-sm-10">
<input class="form-control" type="text" id="branch" name="branch" value="{{$branch->name}}">
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="page-title-box">
<div class="btn-group float-right">
<button class="btn btn-primary" type="submit">Button</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div> <!-- end col -->
</div> <!-- end row -->
</div>
</div>
#include('theme.footer')
Controller File
<?php
namespace App\Http\Controllers;
use App\Branch;
use App\Company;
use App\Dealership;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BranchController extends Controller
{
public function index()
{
$companies = Company::where('status', '0')->get();
$dealership = Dealership::where('status', '0')->get();
$branches = Branch::where('status', '0')->get();
return view('branch.index', compact('branches'));
}
public function store(Request $request)
{
$branch_id = new Branch;
$branch_id = Branch::orderBy('br_id', 'desc')->take(1)->get();
if (count($branch_id) > 0) {
$id = $branch_id[0]->br_id;
$id = $id + 1;
} else {
$id = 1;
}
$branch = new Branch;
$branch->br_id = $id;
$branch->name = $request->input('branch');
$branch->dlr_id = $request->input('dealer');
$branch->comp_id = $request->input('company');
$branch->created_id = '0';
$branch->save();
return redirect()->back()->with('message', 'Successfully saved');
}
public function edit(Branch $branch)
{
$branch=Branch::where('id',$branch->id)->first();
return view('branch.edit',['branches'=>$branch]);
}
public function update(Request $request,$id)
{
$branch = Branch::findOrFail($id);
$branch->status = '1';
$branch->save();
if ($branch) {
$branchs = new Branch();
$branchs->comp_id = $request->input('company');
$branchs->dlr_id = $request->input('dealer');
$branchs->name = $request->input('branch');
$branchs->created_id = '0';
$branchs->save();
if ($branchs) {
return redirect('/branch')->with('message', 'Successfully saved');
}
}
}
public function destroy(Branch $branch)
{
DB::table('branches')
->where('id', $branch->id)
->update(['status' => '-1']);
return back()->with('message', 'Successfully Deleted');
}
}
You are passing branch with parameter name branches. Change your controller code to:
public function edit(Branch $branch)
{
$branch=Branch::where('id',$branch->id)->first();
return view('branch.edit',['branch'=>$branch]);
}
You are sending 'branches' from your controller and used $branch in your view! Try to change it in your edit() function like:
public function edit(Branch $branch)
{
$branch=Branch::where('id',$branch->id)->first();
return view('branch.edit',['branch' => $branch]);
}
And also, in your index() function change
return view('branch.index', compact('branches'));
To
return view('branch.index', compact(['branches', 'companies', 'dealership']));
Hope this will helps you!
Everything seems to be correct just that you are sending branches instead of branch and when the view looks up for branch it's not available .. that's the error you are getting
I have this form in the Laravel admin backend which adds new products to database.
productCreate.blade.php
{{ Form::open(['files' => true]) }}
<div class="form-group">
<label for="title" class="control-block">Product title:</label>
{{ Form::text('title', null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
<label for="title" class="control-block">Product description:</label>
{{ Form::textarea('description', null, ['class' => 'form-control']) }}
<span class="help-block">HTML is allowed.</span>
</div>
<div class="form-group">
<label for="title" class="control-block">Product price:</label>
{{ Form::text('price', null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
<label for="title" class="control-block">Assign Product to Category:</label>
<select class="form-control">
<option value="one">Category 1</option>
<option value="two">Category 2</option>
<option value="three">Category 3</option>
<option value="four">Category 4</option>
<option value="five">Category 5</option>
</select>
</div>
<div class="form-group">
<label for="title" class="control-block">Product image:</label>
{{ Form::file('image', ['class' => 'form-control']) }}
</div>
<hr />
<button type="submit" class="btn btn-primary">Create new product</button>
{{ Form::close() }}
My question is how to show this select/options values from database. I want to be able to assign products to categories. Where I should make query and how to add them here in the view?
In admincontroller i have functions for product creation which load the view
public function productsCreate() {
return View::make('site.admin.products_create');
}
and I tried to make it like this
public function productsCreate() {
$categories = Categories::paginate(15);
return View::make('site.admin.products_create', [
'categories' => $categories
]);
//return View::make('site.admin.products_create');
}
but then I got message
production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Cannot redeclare AdminController::productsCreate() ...
So how exactly I can query database table categories and show categories in the form?
May be because I already have categories selection in admin controller?
public function categories() {
$categories = Categories::all();
return View::make('site.admin.categories', [
'categories' => $categories
]);
}
public function productsCreate() {
// $categories = Categories::paginate(15);
return View::make('site.admin.products_create', [
'categories' => $categories
]);
//return View::make('site.admin.products_create');
}
You pass the categories in theproductsCreate function, and you must remove the old productsCreate function, you declared it twice :
public function productsCreate() {
$categories = Categories::get();
return View::make('site.admin.products_create', [
'categories' => $categories
]);
}
For the html :
<div class="form-group">
<label for="title" class="control-block">Assign Product to Category:</label>
<select class="form-control">
#foreach($categories as $categorie)
<option value="{{ $categorie->id }}">{{ $categorie->name }}</option>
#endforeach
</select>
I hope that will help you.