how to store drop-down list select into database? - mysql

I have a form for creating projects. It contains 2 foreign keys domain and owner (domain_id and owner_id). in fact these are 2 drop-down lists. when i try to submit the form, and check my database, I found out that owner and domain have NULL as value, eventhough i selected values from the drop-down list.
this is projectController :
public function create()
{
$domains = Domain::all('nameDomaine', 'id');
$owners = Owner::all('nameOwner', 'id');
return view('projectss.create', compact('domaines', 'owners'));
}
public function store(Request $request)
{
$domain_id = Domain::all()->pluck('nameDomain', 'id');
$owner_id = Owner::all()->pluck('nameOwner', 'id');
$this->validate($request, [
'title' => 'required',
'code' => 'required',
'domain_id' => 'required',
'owner_id' => 'required',
]);
Project::create($request->all());
return redirect()->route('projects.index')
->with('success', 'Project created successfully');
}
and this is create.blade.php :
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> Domain : </strong>
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $id => $domain)
<option value="{{ $id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> owner : </strong>
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $id => $owner)
<option value="{{ $id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
</select>
</div>
</div>
I read a lot of posts about this problem, but none of them is working for me.

You can try this
in controller's store function
$domain_id = Domain::all('nameDomain', 'id');
$owner_id = Owner::all('nameOwner', 'id');
In you view
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $id => $domain)
<option value="{{ $domain->id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
and for second select option
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $id => $owner)
<option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
You was not fetching id of your object. The $id you were fetching will just give you the index.
This should work :)

Change
#foreach($owners as $id => $owner)
<option value="{{ $id }}">{{ $owner->nameOwner }}</option>
#endforeach
to
#foreach($owners as $owner)
<option value={{ $owner->id }}>{{ $owner->nameOwner}</option>
#endforeach
And repeat with the other foreach loop.
If that doesn't work, in your controller add dd($request); to see what info is being passed to it and share with us, please.

Seems close but a few tweaks are needed. Also check your Project model and see if there is a $fillable array. If there is you'll need all the fields in your validate call in it e.g.
protected $fillable= [
'title'
'code'
'domain_id'
'owner_id'
];
Remove the calls in your store function.
public function create()
{
$domains = Domain::all('nameDomaine', 'id');
$owners = Owner::all('nameOwner', 'id');
return view('projectss.create', compact('domaines', 'owners'));
}
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'code' => 'required',
'domain_id' => 'required',
'owner_id' => 'required',
]);
Project::create($request->all());
return redirect()->route('projects.index')
->with('success', 'Project created successfully');
}
Make sure you're adding the right ID.
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> Domain : </strong>
<select class="form-control" name="domain_id">
#if (!count($domains) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($domains as $domain)
<option value="{{ $domain->id }}">{{ $domain->domain }}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong> owner : </strong>
<select class="form-control" name="owner_id">
#if (!count($owners) > 0)
<strong> Whoops! Something went wrong </strong>
#else
#foreach($owners as $owner)
<option value="{{ $owner->id }}">{{ $owner->nameOwner }}</option>
#endforeach
#endif
</select>
</div>
</div>

Related

Old Value/Search Value on search select button in Laravel

i make a select button for search specific data on my table.
but there is problem when i click the button, the button doesn't remain or stay from past request value that i click, or it reset back to select all
so this is my form in my blade file
<form action="">
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">Select All</option>
#foreach($rumahs as $rumah)
#if(old('rumah_id') == $rumah->id)
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
#else
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
#endif
#endforeach
</select>
</form>
this is my Controller
public function index(Request $request){
$rumah = Rumah::all();
$mobil = Mobil::with(['rumah'])->orderBy('rumah_id', 'asc')
->when($request->rumah_id != null, function($q) use($request){
return $q->where('rumah_id', 'like', '%'. $request->rumah_id. '%');
})
return view('mobil/index',[
'mobils' => $mobil,
'rumahs' => $rumah,
]);
}
also this is my migration database for rumah look like
Schema::create('rumahs', function (Blueprint $table) {
$table->id();
$table->string('alamat');
$table->timestamp('published_at')->nullable();
$table->timestamps();
});
Try this :
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">Select All</option>
#foreach($rumahs as $rumah)
<option value="{{ $rumah->id }}" {{ old('rumah_id') == $rumah->id ? 'selected' : '' }}>{{ $rumah->alamat }}</option>
#endforeach
</select>

drop-down show selected value when editing a row laravel

<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>From Destination:</strong>
<select name="from_destination_data" class="form-control">
#if ($destinationsData != [])
#foreach($destinationsData as $info)
<option value="{{$destinationsData[$index]}}">{{$info}}</option>
#endforeach
#endif
</select>
</div>
</div>
I retrieve the $index of the selected $destinationData and when dd($destinationsData[$index]); I get the result that I need but it doesn't appear when I put it in the value as shown above
When constructing your <select> and <options>, you need to set a value for each that can be referenced when editing your record. See the following:
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>From Destination:</strong>
<select name="from_destination_data" class="form-control>
#foreach($destinationsData as $key => $info)
<option value="{{ $key }}">{{ $info }}</option>
#endforeach
</select>
</div>
</div>
Setting value="{{ $key }}" will, in this instance, set the value to a 0-based index, which can be referenced when editing:
#foreach($destinationsData as $key => $info)
<option value="{{ $key }}" {{ $record->from_destination_data == $key ? 'selected' : '' }}>{{ $info }}</option>
#endforeach
As long as $record->from_destination_data equals any of the $key iterations, that <option> will have selected="selected", setting it as the default selected option.

populate data from database into select dropbox

I have a select box which needs to fetch from the database.
view blade:
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($service as $sn)
{
<option value="">dd($sn)</option>
}
#endforeach
</select>
controller:
public function index()
{
$service = DB::select('select service_type from services');
return view("service",['service'=>$service]);
}
I am getting an error like
Undefined variable: service (View: /home/devadmin/.config/composer/vendor/laravel/installer/serenseprj/resources/views/package.blade.php)
You can use Eloquent instead of doing this
$service = DB::select('select service_type from services');
Using Eloquent,
$services = ModelName::pluck('name', 'id')->get();
You can pass the variable to the targeted view like below:
return view('view_name', ['variable_name' => $variable]);
And, you can access the variable name like below
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($variable_name as $var)
{
<option value="{{$var->id}}">{{$var->name}}</option>
}
#endforeach
</select>
You can use this code as the references. Hope you have service model.
$services= Service::pluck('name', 'id');
return view('services', compact('services'));
then in view file you can use laravel collevtives like.
<div class="form-group col-sm-6">
{!! Form::label('service_id', 'service Id:') !!}
{!! Form::select('service_id', $services, null, ['class' => 'form-control'])!!}
</div>
you can use this too. in you blade file
<select name="service " id="service " class="form-control">
<option value=""> -- Select One --</option>
#foreach ($services as $service )
<option value="{{ $service->id }}">{{ $service->name }}</option>
#endforeach

Pass data from class select to get route

how can I pass data from this form to my route?
<div class="panel-body">
{!! Form::open(array('route'=>'show.exclusion')) !!}
<div class="form-group">
{{Form::label('choosegroup', 'Wähle eine Gruppe')}}
<select class="form-control m-bot15" name="idgroup">
#foreach($groups as $group)
<option value="{{ $group->id }}">{{ $group->name }}</option>
#endforeach
</select>
{{ csrf_field() }}
</div>
<div>
{{Form::submit('Search',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
</div>
This is my route...
Route::get('exclusion/show/{id}', 'ExclusionController#show')->name('show.exclusion');
First I put the $group->id parameter into the array route part. But I cannot use them at this point of my code.
Any Ideas?
EDIT:
public function show($id)
{
$member = Nerd::find($id);
return view('groups.test')->with('member', $member);
}
You need to pass the whole Request to the function. Than it is possible to get the Values of your Formdata.
public function update(Request $request, $id)
{
$idgroup = $request->input('idgroup');
}
For more details have a look here:
https://laravel.com/docs/5.6/requests
You can use request() method to fetch form data without any class dependencies.
For example:
request()->id;

Keep the option selected in search form - Laravel

I have a search form that searches for responsible, date and device name. When someone searches for a resposible with a date, I want that search data to be maintained in the result
The entire search form
{{ Form::open(['route' => 'reviews.index', 'method' => 'GET', 'class' => 'form-inline pull-right']) }}
#csrf
{{ Form::text('device_name', null, ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
<select name="name">
<option></option>
<option>OK</option>
<option>NOK</option>
</select>
{{ Form::date('created_at', null, ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
<select name="responsable">
<option ></option>
#foreach($users as $user)
<option>{!! $user->name !!}</option>
#endforeach
</select>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Buscar</button>
<button style="border: none;padding: 4px 20px;background-color: #d61628;color: white">Reset</button>
{{ Form::close() }}
And the index method in Controller
public function index(Request $request)
{
$responsable = $request->get('responsable');
$created_at = $request->get('created_at');
$device_name = $request->get('device_name');
$name = $request->get('name');
$devices = Device::all();
$reviews = Review::orderBy('id', 'DESC')
->responsable($responsable)
->created_at($created_at)
->device_name($device_name)
->name($name)
->paginate(30);
$users = User::all();
return view('reviews.index', compact('devices', 'reviews', 'users'));
}
Thanks
You may use old() function for re-populate the form with the last submitted data.
See https://laravel.com/docs/5.6/requests#old-input
So, in your blade code it would be something like this:
{{ Form::text('device_name', old('device_name'), ['class' => 'form-control', 'placeholder' => 'Nombre Equipo'])}}
//...
{{ Form::date('created_at', old('created_at'), ['class' => 'form-control', 'placeholder' => 'Creacion'])}}
For select, it's different, basically you compare each rendered option in the loop and if it's the one submitted you mark it as selected with html.
<select name="responsable">
<option></option>
#foreach($users as $user)
<option value="{{ $user->name }}" {{ old('responsable') == $user->name ? 'selected' : '' }}>{!! $user->name !!}</option>
#endforeach
</select>
Also, consider that old() also accepts a second parameter for default value in case you don't want it to be just null.