saving check box values to the database in laravel - php

I'm new to laravel. I'm saving some checkbox value to the database using loop the loop work but it only saves the last value in the array to the database.
this is my form
<form action="{{url('resortf')}}" method="post" enctype="multipart/form-data">
<input hidden name="h_id" value="{{ $hotel->id}}">
#foreach($facility as $facilities)
<div class="col-md-4">
<img src="{{$facilities->image}}" width="50px" height="50px;">
<label>{{$facilities->name}}</label>
<input type="checkbox" value="{{$facilities->id}}" name="facilities[]">
</div>
#endforeach
<div class="row">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-success" value="Next">
<input type="reset" class="btn btn-success" value="Reset">
</div>
</form>
form working fine; $facilities and $hotel are passed from the controller.
this is the store function
public function store(Request $request) {
$resortfacility = new Resortfacility;
$loop = $request->get('facilities');
foreach ($loop as $value){
$resortfacility->h_id = $request->get('h_id');
$resortfacility->f_id = $value;
$resortfacility->save();
}
}
is there any other way to do this that works?

Your problem occurs because you create one instance of Resortfacility, and then you fill in its values and save. The problem is, every time you perform changes to this object in a loop, you are updating existing object and that's why there is only one record in your database, the last one from the loop.
Try making new instance of Resortfacility inside the loop like this:
public function store(Request $request) {
$loop = $request->get('facilities');
foreach ($loop as $value){
$resortfacility = new Resortfacility;
$resortfacility->h_id = $request->get('h_id');
$resortfacility->f_id = $value;
$resortfacility->save();
}
}

The other answer is correct but, bulk insertion might be helpful, as creating new object within foreach loop will make query for every record.
public function store(Request $request)
{
$facilities = $request->get('facilities');
$data=array();
foreach($facilities as $facility)
{
$data[] =[
'f_id' => $facility,
'h_id' => $request->get('h_id'),
];
}
}
Resortfacility::insert($data);

Related

Trying to get property 'deskripsi' of non-object (View:

#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:)

Need to insert a single data in the database

I'm having a problem to insert a single data from my database
Example: I have two data's which is the question1 with ID:1 and question2: with ID:2. Those 2 questions does have different button. The problem is once I click the question1 or question2 button it inserting both of the ID's in my database.
Like this:
Here's My Controller
$data['posts'] = $this->Post_Model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
$this->load->library('form_validation');
if($this->form_validation->run()==FALSE) {
if($this->input->post("add")) {
$this->Post_Model->count_up();
redirect('posts');
}
}
My Model
function count_up(){
for($i=0; $i<count($this->input->post('hidden')); $i++){
$data = array(
'post_id' => $this->input->post("hidden[$i]")
);
$this->db->insert("userspost", $data);
}
}
My View
<?php
$iq = 0;
$i = 0;
$arrtry = array();
foreach($posts->result() as $post){
?>
<br>
<div class="card card-nav-tabs">
<div class="card-header card-header-primary">
<!-- colors: "header-primary", "header-info", "header-success", "header-warning", "header-danger" -->
<div class="nav-tabs-navigation">
<input class="form-control" value="<?php echo $arrtry[$iq++] = $post->id; ?>" name="hidden1[<?php $i; ?>]" type="hidden">
<input class="btn btn-primary" type="submit" name="add" value="UP" />
</div>
</div>
</div>
<?php
$i++;
}
?>
My problem is i want to insert question2 ID without inserting question1 ID. Hope you guys can help thanks!
What is happening is that you have all your inputs within the same form with multiple submit buttons.
I can imagine a few ways you could solve this:
Ajax way. Require some JS and another controller method.
One form for each button: So you will always have one $this->input->post('hidden'). No need to iterate over it.
With only one form You could set the button for something like this:
<input class="btn btn-primary" type="submit" name="add-up" value="<?= $i; ?>" />
So on your controller/model you can get the index that was clicked:
/* controller/model */
$index = $this->input->post('add-up');
$hidden_value = $this->input->post("hidden")[$index]

Pass id from controller index to store - Laravel

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]);

How to include multiple inputs in a foreach statement

I have two input fields that a user can repeat to insert multiple records in a single form submission. I'm currently using a foreach loop to loop through one of those inputs and grab the value however now I need to loop through both inputs at the same time to save the data together.
I want to loop through both the input-type & input-label together and save the stored values to the db.
I guess I could create two foreach loops but that just seems like a horrible way to go about things.
Form -
<form method="post" action="{{ route('savePage') }}">
{{ csrf_field() }}
<ul class="field-list">
<li v-for="(input, index) in inputs">
<input type="text" name="input-type[]" v-model="input.one" style="display: none">
<input type="text" name="input-label[]" v-model="input.two" placeholder="Label name">
<p><span class="codesnippet">text</span></p>
<button class="p-btn secondary" #click.prevent="deleteRow(index)" >Remove field</button>
</li>
</ul>
<button>Submit</button>
</form>
Controller -
public function saveAtt(Request $request)
{
foreach ($request->input('input-label') as $label) {
$field = new Attribute;
$field->page_id = 1;
$field->label = $label;
$field->type = 2;
$field->save();
}
return redirect()->route('indexPage');
}
Use this:
$types = $request->input('input-type');
foreach ($request->input('input-label') as $i => $label) {
$field = new Attribute;
$field->page_id = 1;
$field->label = $label;
$field->type = $types[$i];
$field->save();
}

Laravel 5 ,Table not updating

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.
}

Categories