Why I have got 3 values when I attach "dresscodes"?
$event->dresscode()->attach($request->get('dressCodesValue'));
this is error message
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'dress_codes_id' at row 1 (SQL: insert into `dress_codes_events` (`dress_codes_id`, `events_id`) values (1,2, 28))
Here is a relation of Events table
public function dresscode()
{
return $this->belongsToMany(DressCodes::class);
}
Here are dresscode value when print out from controller $request->get('dressCodesValue') // 1,2
I fixed it.
Here is my problem
When I pass my data into FormData() and post it via axios.post It's converts my array value into string
Solution
just explode string back to array and pass that value into attach() method
Related
I have a eloquent model say Math which has a column in db say number, which is of type array
in db suppose,
for id =1 number = [1,2]
When I try to edit this entry from form, I have to select values 1,2 again. As in the data is not persisting
if($form->isEditing()){
$number = Math::where('id', 1)->pluck('number', 'number');
$form->multipleSelect('number')->options(NumberList::all()->pluck('id', 'id'))->default($number);
}
I have tried doing the above, but the data is not persisting in edit mode. How to solve this?
I want to update a totcosty field in the User table but it is throwing this error everytime and it is not updating the field
this is the function for execution:
public static function cost(){
$user = User::find($user_id);
$total = Helper::totcost();
// dd($tot_amt);
$user->totcosty = $total;
$user->save();
}
array to string means you are sending an array to the database but db will not accept it you have to explode() the array before sending it to db...
Hope it will help!
If you really want to store an array in some table field, then better declare it as a JSON field. For this, your DB should have support for JSON type columns.
See here how to do this.
Once this is done, you can save arrays in that column, you can assign an array value to the model property and laravel will convert it to JSON while saving and also it will be converted to array while retrieving.
In the following code, The Users table has a related table phoneNumbers. When I retrieve a list of all users like this,
return Person::with('phoneNumbers')->get();
everything works fine. However, when I attempt to specify a list of columns to return from the Person table, the phone_number returns empty.
return Person::with('phoneNumbers')
->get(['fname','lname', 'email']);
If I add the number field or phone_number.number to the get array, then I get an error as an undefined column. What is the laravel way of handling this.
Try this:
return Person::select(['your_foreign_key', 'fname','lname', 'email'])
->with('phoneNumbers')get();
I'm new to Laravel and having problems with ManyToMany relationships.
I have two tables:
members
groups
A member can belong to many groups, a group can have many members.
I've created the relationships in the models and the pivot table.
When I create a member, I have checkboxes for the groups.
In my store method I do this:
....
$member->save();
if(isset($request->groups)) {
$groups = implode(',', $request->groups);
$member->groups()->sync([$groups]);
}
dd($groups) gives: "2,7"
I get the error:
QueryException in Connection.php line 761: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'group_id' at row 1 (SQL: insert into group_member (group_id, member_id) values (2,7, 5))
Where is this 5 coming from and why do I get this error?
Thank you
This error is because of the data type you entered.
Using
implode()
You turn the array into a string while sync() method wants an array as input. Let's try just:
$member->save();
if(isset($request->groups)) {
$member->groups()->sync($request->groups);
}
To avoid the inconsistency that might arise due to the comma separated string, I'd convert
$request->groups, which is a string, to a standard array before adding it to the sync function.
if(isset($request->groups)) {
$groupIds = array($request->input('groups'));
$member->groups()->sync($groupdIds);
}
I'm using JTable to store a record in a table. My table has 3 primary keys(pid,eid,sid). I want to store (Insert,update) a record
my code:
$row =& JTable::getInstance('mytable', 'Table');
$row->load(
array(
'pid' =>$pid,
'eid' =>$eid,
'sid' =>$sid
)
);
$row->data = $data;
if (!$row->store()) {
JError::raiseError(500, $row->getError() );
}
The load function runs with warning:
Warning: mysql_real_escape_string() expects parameter 1 to be string, object given in ...\joomla\database\database\mysql.php on line 193
and the store function raise an error:
, but the store raise an error with the SQL statement. The SQL statement contains the field names and new values and 'WHERE' keyword but without a condition.
any help?
The load function takes an integer as an input (see here http://docs.joomla.org/JTable/load) so you cannot pass it an array. The integer you pass it should be the primary key of your table. Here you can use any one of your 3 primary keys, because being primary it will be unique.