I have this error :
ErrorException in helpers.php line 748:
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
SerialController.php
public function createSerial(Request $request)
{
$serial = new Serial();
$serial->nume_serial = $request['numeSerial'];
$serial->claritate = $request['claritate'];
$serial->aparitie = $request['aparitie'];
$serial->genuri = $request['genuri'];
$serial->save();
return redirect('/admin');
}
view
<div class="checkbox">
SF<input type="checkbox" name="genuri[1]" value="sf" id="">
Biografic<input type="checkbox" name="genuri[2]" value="biografic" id="">
Animat<input type="checkbox" name="genuri[3]" value="animat" id="">
</div>
I guess a problem is you're trying to insert an array into a DB table.
$serial->genuri = $request['genuri']; // it's an array
You could convert an array to a json data:
$serial->genuri = json_encode($request['genuri']);
Of course you have to change genuri data type to JSON:
$table->json('genuri');
Apparently from looking at your view, $request['genuri'] is an array, and the error is this line $serial->genuri = $request['genuri']; since you are assigning an array to an object property that is string (I think?) in your DB table.
Not sure what you are trying to accomplish, if you provide more info I might help more.
Related
My model (table with 2 primary keys and are foreign key on the same time) :
class FormationPersonnel extends Model
{
protected $fillable = ['cin', 'id_form'];
public $incrementing = false;
}
I create checkboxes with JQuery, Ajax like so :
var grabData = "";
for (let i = 0; i < data.length; i++) {
grabData +=
`<div class="col-lg-4 col-sm-6">
<div class="custom-control custom-checkbox">
<input type="checkbox" name="cin[]" id="`+data[i]["cin"]+`" class="custom-control-input" value="`+data[i]["cin"]+`">
<label for="`+data[i]["cin"]+`" class="custom-control-label">`+data[i]["cin"]+' '+data[i]["nom"]+' '+data[i]["prenom"]+`</label>
</div>
</div>`;
}
personnelsInput.html("");
personnelsInput.append(grabData);
which I give the checkboxes the same name as :
name="cin[]"
In My controller I use this code, but is not storing any data from checkboxes :
$cins = $request->cin; //I get inputs like this
foreach ($cins as $cin) {
$form_pers = new FormationPersonnel; //create new record
if ($request->has($cin)) {
$form_pers->id_form = $request->input('id_form');
$form_pers->cin = $request->input($cin);
$form_pers->save(); //save data
}
}
What is the correct way ?
The correct way to check whether the Model has cin or not is
Edited
based on the comment we put, I think I know what you're trying to do, so do it like below code to get what you need.
note
you don't know what $cins values so that's why you use if(). No need to check whether the user check (checkbox) or not. because the checked value only send by form. please try to know what data you have in your variable before coding.
I recommended you to use dd() or print_r() or others https://laraveldaily.com/echoing-dd-vs-var_dump-vs-print_r/ to check the data you got in your variable.
$cins = $request->cin; //getting all checked (from checkbox) data
foreach ($cins as $cin) {
$form_pers = new FormationPersonnel; //new model
$form_pers->id_form = $request->id_form; //get name="id_form" from form
$form_pers->cin = $cin; //put cin value (loop)
$form_pers->save(); //save
}
How to send the values of checked items in a dynamic list from a database displayed in a view to controller ?
A concrete solution depends on your data, but if you are talking about a list of checkboxes, you can give them a common name in array notation:
<form type="post" action="...">
#foreach($elements as $elem)
<input type="checkbox" name="my_input[{{ $elem->id }}]" value="1">
#endforeach
</form>
On the server-side, you can then query the data like this:
use Illuminate\Http\Request;
class MyController
{
public function postData(Request $request)
{
$myInput = (array) $request->get('my_input', []);
// ... remaining logic
}
}
The line $myInput = (array) $request->get('my_input', []); will read the POST variable my_input as array and, if no such post variable is given, an empty array will be returned. In other words, $myInput will always be an array, where the key is $elem->id and the value '1' as defined by value="1".
I am receiving this error when I try to get the value from database to edit bus type form text box.
Property [name] does not exist on this collection instance. (View: E:\fyp\resources\views\buses\editbustype.blade.php)
Here is the code for edit section of controller:
public function edit(addbustype $id)
{
$bustype = addbustype::find($id);
return view('buses.editbustype', compact('bustype','id'));
}
When I changed $id to any number (1) it gives me the perfect result for that id, but only for a single one. The code for edit bus type section is here:
<div class="form-group col-md-6">
<label for="name">Company Name</label>
<input type="text" class="form-control" name="name" placeholder="Company Name" value="{{$bustype->name}}">
</div>
Note That You Are Using Route Model Binding
Than You Are Trying To Get The addbustype With $id Watch is $id Now Become An Instance Of addbustype Model So It Well Give You An Error Because
$bustype Well Now Become A Collection Of addbustype Model
So You Have To Decide Eather Use Route Model Binding Or Simply Find Function
In Your Web.php Route
The Parameter Name Must Matches With Parameter Both On Route And Edit Function
Route::get('addbustype/{addbustype}','yourCountroller#edit')
public function edit(addbustype $addbustype)
{
return view('buses.editbustype', compact('bustype'));
}
Or Simply Use Find Method
public function edit($id)
{
$bustype = addbustype::find($id);
return view('buses.editbustype', compact('bustype','id'));
}
You Can Revel To Route Model Binding For More
First off, thank you for your valuable time and interest - any input to a beginner is greatly appreciated!
How do I insert multiple inputs with (in most cases) multiple same names? Here is my code:
View:
//Simplified version, original is a jquery append script
Passes: <input type="text" name="passes[]">
Points: <input type="text" name="points[]">
Passes: <input type="text" name="passes[]">
Points: <input type="text" name="points[]">
Controller:
//Loops through a table like form for inputting stats
function add_stat() {
$i = 0;
foreach ($this->input->post('points') as $points) {
$dataSet1[$i++] = array (
'points' => $points
);
}
foreach ($this->input->post('passes') as $passes) {
$dataSet2[$i++] = array (
'passes' => $passses
);
}
//Not sure how to pass multiple arrays, or if even possible
$this->sport_model->insert_stat($dataSet1, $dataSet2);
}
Model:
//Passing multiple params error out due to "String to Array Conversion"
function insert_stat($dataSet1, $dataSet2) {
$this->db->insert_batch('table', $dataSet1, $dataSet2);
return $this->db->insert_id();
}
set your controller and model as:
controller:
//Loops through a table like form for inputting stats
function add_stat() {
$points = $this->input->post('points');
$passes = $this->input->post('passes');
for($i=0;$i<sizeof($points);$i++)
{
$dataSet[$i] = array ('points' => $points[$i], 'passes' => $passses[$i]);
}
// $dataSet is an array of array
$this->sport_model->insert_stat($dataSet);
}
model:
function insert_stat($dataSet)
{
$this->db->insert_batch('table', $dataSet);
return $this->db->insert_id(); // this will return the id of last item inserted.
}
First, insert_batch() only takes two params: the table name and a data set.
Second, insert_id() isn't going to do you much good on a batch insert, but that's not a huge deal.
I assume you want points and passes to be in the same record, and that you're appending another set of points/passes inputs via JS.
View:
Passes: <input type="text" name="scores[0][passes]">
Points: <input type="text" name="scores[0][points]">
Passes: <input type="text" name="scores[1][passes]">
Points: <input type="text" name="scores[1][points]">
// etc...
In your controller, you would then just use one $dataSet array and loop through $this->input->post('scores')
When $dataSet is complete, you'd batch insert with $this->db->insert_batch('table', $dataSet)
I am working with a registered_member_data_edit page?
My controller function is
function edit_profile()
{
$data['title']= 'Edit Profile';
$member_id = $this->session->userdata('member_id');
$this->load->model('user_model','',TRUE);
$data['row'] = $this->user_model->edit_user($member_id)->result();
$this->load->view('edit_profile.php', $this->data);
}
My model function is
public function edit_user($member_id)
{
$this->db->select('fullname','country','district','address','nominee_name','nominee_relation','mobile_no','password','bank_acc_no','bank_acc_name','bank_name','branch_name');
return $this->db->get_where('user', array('member_id'=> $member_id));
}
My view page is like
<p><label>User Name</label>
<input type="text" class="input-short" value="" /></p>
What should I put in the value to show the db data in edit page?
public function edit_user($member_id)
{
$this->db->select('fullname','country','district','address','nominee_name','nominee_relation','mobile_no','password','bank_acc_no','bank_acc_name','bank_name','branch_name');
return $this->db->get_where('user', array('member_id'=> $member_id));
}
this will return the object, which you have in $data['row'] and that variable you are sending to view file.
So in view file, you can access the all data of the object using $row.
So from here you have 2 options
Even that function returning only 1 row, you have to access using foreach($row->result() as $_data) and after that you can access each and every column as $_data->fullname like that.
You can convert that object to array using $row = $row->result_array() and you can access the database column using $row[0]['fullname'] like that.
Best luck.