laravel Error: Array to string conversion
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have gone through every solution I could find over youtube, stakoverflow and other websites. I am using Select2 to add multiple roles but I a consistently getting the same error.
<select id="role" name="role_id" multiple='multiple'
class="form-control js-example-basic-multiple">
@foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
@endforeach
</select>
DD function is showing perfect result but after that it shows error.
It works perfectly untill I add with the name="role_id".
form action is as under.
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->role_id = $request->role_id;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
If I validate for integer 'role_id'=> 'required|integer', it shows error
Migration is as under
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('role_id')->default(1);
$table->boolean('status')->default(0);
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
php sql laravel jquery-select2
add a comment |
I have gone through every solution I could find over youtube, stakoverflow and other websites. I am using Select2 to add multiple roles but I a consistently getting the same error.
<select id="role" name="role_id" multiple='multiple'
class="form-control js-example-basic-multiple">
@foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
@endforeach
</select>
DD function is showing perfect result but after that it shows error.
It works perfectly untill I add with the name="role_id".
form action is as under.
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->role_id = $request->role_id;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
If I validate for integer 'role_id'=> 'required|integer', it shows error
Migration is as under
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('role_id')->default(1);
$table->boolean('status')->default(0);
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
php sql laravel jquery-select2
1
Change it to'role_id.*'=> 'required',
and role_id is collection of array you should run that with loop and then insert inusers role
table since user have more than one role you need to keep them in another table like user_roles
– JYoThI
Nov 29 '18 at 5:00
1
error is here$user->role_id = $request->role_id;
in this line you are assigning array ($request->role_id
) to single variable. So, you should first create array and then used foreach loop on$request->role_id
to assign value to array
– Bilal Ahmed
Nov 29 '18 at 5:11
I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer".
– Ali Anwar
Nov 29 '18 at 5:25
1
@AliAnwar whats the error?? You should include the actual error message in your question
– Sohel0415
Nov 29 '18 at 5:45
error is "The role id is not integer" when I validate for Integer.
– Ali Anwar
Nov 29 '18 at 6:08
add a comment |
I have gone through every solution I could find over youtube, stakoverflow and other websites. I am using Select2 to add multiple roles but I a consistently getting the same error.
<select id="role" name="role_id" multiple='multiple'
class="form-control js-example-basic-multiple">
@foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
@endforeach
</select>
DD function is showing perfect result but after that it shows error.
It works perfectly untill I add with the name="role_id".
form action is as under.
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->role_id = $request->role_id;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
If I validate for integer 'role_id'=> 'required|integer', it shows error
Migration is as under
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('role_id')->default(1);
$table->boolean('status')->default(0);
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
php sql laravel jquery-select2
I have gone through every solution I could find over youtube, stakoverflow and other websites. I am using Select2 to add multiple roles but I a consistently getting the same error.
<select id="role" name="role_id" multiple='multiple'
class="form-control js-example-basic-multiple">
@foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
@endforeach
</select>
DD function is showing perfect result but after that it shows error.
It works perfectly untill I add with the name="role_id".
form action is as under.
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->role_id = $request->role_id;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
If I validate for integer 'role_id'=> 'required|integer', it shows error
Migration is as under
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('role_id')->default(1);
$table->boolean('status')->default(0);
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
<select id="role" name="role_id" multiple='multiple'
class="form-control js-example-basic-multiple">
@foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
@endforeach
</select>
<select id="role" name="role_id" multiple='multiple'
class="form-control js-example-basic-multiple">
@foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
@endforeach
</select>
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->role_id = $request->role_id;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->role_id = $request->role_id;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('role_id')->default(1);
$table->boolean('status')->default(0);
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('role_id')->default(1);
$table->boolean('status')->default(0);
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
php sql laravel jquery-select2
php sql laravel jquery-select2
edited Nov 29 '18 at 6:26
Ali Anwar
asked Nov 29 '18 at 4:57
Ali AnwarAli Anwar
195
195
1
Change it to'role_id.*'=> 'required',
and role_id is collection of array you should run that with loop and then insert inusers role
table since user have more than one role you need to keep them in another table like user_roles
– JYoThI
Nov 29 '18 at 5:00
1
error is here$user->role_id = $request->role_id;
in this line you are assigning array ($request->role_id
) to single variable. So, you should first create array and then used foreach loop on$request->role_id
to assign value to array
– Bilal Ahmed
Nov 29 '18 at 5:11
I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer".
– Ali Anwar
Nov 29 '18 at 5:25
1
@AliAnwar whats the error?? You should include the actual error message in your question
– Sohel0415
Nov 29 '18 at 5:45
error is "The role id is not integer" when I validate for Integer.
– Ali Anwar
Nov 29 '18 at 6:08
add a comment |
1
Change it to'role_id.*'=> 'required',
and role_id is collection of array you should run that with loop and then insert inusers role
table since user have more than one role you need to keep them in another table like user_roles
– JYoThI
Nov 29 '18 at 5:00
1
error is here$user->role_id = $request->role_id;
in this line you are assigning array ($request->role_id
) to single variable. So, you should first create array and then used foreach loop on$request->role_id
to assign value to array
– Bilal Ahmed
Nov 29 '18 at 5:11
I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer".
– Ali Anwar
Nov 29 '18 at 5:25
1
@AliAnwar whats the error?? You should include the actual error message in your question
– Sohel0415
Nov 29 '18 at 5:45
error is "The role id is not integer" when I validate for Integer.
– Ali Anwar
Nov 29 '18 at 6:08
1
1
Change it to
'role_id.*'=> 'required',
and role_id is collection of array you should run that with loop and then insert in users role
table since user have more than one role you need to keep them in another table like user_roles– JYoThI
Nov 29 '18 at 5:00
Change it to
'role_id.*'=> 'required',
and role_id is collection of array you should run that with loop and then insert in users role
table since user have more than one role you need to keep them in another table like user_roles– JYoThI
Nov 29 '18 at 5:00
1
1
error is here
$user->role_id = $request->role_id;
in this line you are assigning array ($request->role_id
) to single variable. So, you should first create array and then used foreach loop on $request->role_id
to assign value to array– Bilal Ahmed
Nov 29 '18 at 5:11
error is here
$user->role_id = $request->role_id;
in this line you are assigning array ($request->role_id
) to single variable. So, you should first create array and then used foreach loop on $request->role_id
to assign value to array– Bilal Ahmed
Nov 29 '18 at 5:11
I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer".
– Ali Anwar
Nov 29 '18 at 5:25
I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer".
– Ali Anwar
Nov 29 '18 at 5:25
1
1
@AliAnwar whats the error?? You should include the actual error message in your question
– Sohel0415
Nov 29 '18 at 5:45
@AliAnwar whats the error?? You should include the actual error message in your question
– Sohel0415
Nov 29 '18 at 5:45
error is "The role id is not integer" when I validate for Integer.
– Ali Anwar
Nov 29 '18 at 6:08
error is "The role id is not integer" when I validate for Integer.
– Ali Anwar
Nov 29 '18 at 6:08
add a comment |
4 Answers
4
active
oldest
votes
As you have pivot table for roles than you dont need role_id column in your users table
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required|array',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
foreach($request->input('role_id') as $role)
{
$user->assign($role);
}
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
Thanks for replying to my question but I am getting error again with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
what is error ?
– Leena Patel
Nov 29 '18 at 5:33
1
That is because you have set length in your database columnrole_id
that is exceeding when you are selecting more than one roles in your dropdown. so make it larger asvarchar(256)
– Leena Patel
Nov 29 '18 at 5:45
1
userequired|array
to validate as an array
– Leena Patel
Nov 29 '18 at 6:36
1
I am following ur suggestions.
– Ali Anwar
Nov 29 '18 at 6:54
|
show 11 more comments
Problem:
$request->role_id
is an array. not a single data.
Suggestion:
Here two things
You allocating multiple rules with an user
You should not store your rules in users table
You should do that like this:
create another table to store users role
and store user rules separately
Example:
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
$roleAssigns = ;
foreach($request->role_id as $role){
$roleAssigns = [
'role_id' => $role,
'user_id' => $user->id
]
}
//UserRole is the model of user_roles table
UserRole::insert($roleAssigns);
You have pointed out the problem rightly. I tried your code but again I am getting syntax error. I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer". I have seen many people using many to many relationships adding data the same way.
– Ali Anwar
Nov 29 '18 at 5:28
1
if controller want integer type role_id then you should pass integer from view. its the view issue.
– Emtiaz Zahid
Nov 29 '18 at 5:32
I am following the process as Alex is following in many to many relations in his youtube tutorial. I have also seen in many tutorials for the many to many relationships. But in my case its not working. Please check this 33:00 to 33:35 in this tutorial youtube.com/watch?v=BNUYaLWdR04&t=232s –
– Ali Anwar
Nov 29 '18 at 5:48
add a comment |
$request->role_id
is array so you can't store array to database directly so you can use following,
$user->role_id = json_encode($request->role_id);
Later you can use json_decode
function to get array of role_id.
Thanks for answering to my question. I am getting another error with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
@AliAnwar Can you add the error please
– Sagar Gautam
Nov 29 '18 at 5:34
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["2","3"]' for column 'role_id' at row 1 (SQL: insert intousers
(name
,status
,role_id
,email
,password
,updated_at
,created_at
) values (Admin, 0, ["2","3"], admin@xyz.com, $2y$10$DEzjq.XtHPL06FnbtPTa9Oht0Bm5k9eJQT1dh6.A/5TBmVqMGi3yy, 2018-11-29 06:22:48, 2018-11-29 06:22:48))
– Ali Anwar
Nov 29 '18 at 6:23
add a comment |
You cannot "echo" an array, that is the error.
you will need to collect the lines in an array, and then return that array
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53532112%2flaravel-error-array-to-string-conversion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
As you have pivot table for roles than you dont need role_id column in your users table
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required|array',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
foreach($request->input('role_id') as $role)
{
$user->assign($role);
}
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
Thanks for replying to my question but I am getting error again with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
what is error ?
– Leena Patel
Nov 29 '18 at 5:33
1
That is because you have set length in your database columnrole_id
that is exceeding when you are selecting more than one roles in your dropdown. so make it larger asvarchar(256)
– Leena Patel
Nov 29 '18 at 5:45
1
userequired|array
to validate as an array
– Leena Patel
Nov 29 '18 at 6:36
1
I am following ur suggestions.
– Ali Anwar
Nov 29 '18 at 6:54
|
show 11 more comments
As you have pivot table for roles than you dont need role_id column in your users table
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required|array',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
foreach($request->input('role_id') as $role)
{
$user->assign($role);
}
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
Thanks for replying to my question but I am getting error again with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
what is error ?
– Leena Patel
Nov 29 '18 at 5:33
1
That is because you have set length in your database columnrole_id
that is exceeding when you are selecting more than one roles in your dropdown. so make it larger asvarchar(256)
– Leena Patel
Nov 29 '18 at 5:45
1
userequired|array
to validate as an array
– Leena Patel
Nov 29 '18 at 6:36
1
I am following ur suggestions.
– Ali Anwar
Nov 29 '18 at 6:54
|
show 11 more comments
As you have pivot table for roles than you dont need role_id column in your users table
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required|array',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
foreach($request->input('role_id') as $role)
{
$user->assign($role);
}
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
As you have pivot table for roles than you dont need role_id column in your users table
public function store(Request $request)
{
$this->validate($request, [
'name'=> 'required|string|max:225',
'status'=> 'required',
'role_id'=> 'required|array',
'email'=> 'required|string|email|max:225|unique:users',
'password'=> 'required|string|min:6|confirmed'
]);
$password = Hash::make($request->password);
// dd($request->all());
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
foreach($request->input('role_id') as $role)
{
$user->assign($role);
}
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}
edited Nov 29 '18 at 6:35
answered Nov 29 '18 at 5:03
Leena PatelLeena Patel
1,6861720
1,6861720
Thanks for replying to my question but I am getting error again with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
what is error ?
– Leena Patel
Nov 29 '18 at 5:33
1
That is because you have set length in your database columnrole_id
that is exceeding when you are selecting more than one roles in your dropdown. so make it larger asvarchar(256)
– Leena Patel
Nov 29 '18 at 5:45
1
userequired|array
to validate as an array
– Leena Patel
Nov 29 '18 at 6:36
1
I am following ur suggestions.
– Ali Anwar
Nov 29 '18 at 6:54
|
show 11 more comments
Thanks for replying to my question but I am getting error again with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
what is error ?
– Leena Patel
Nov 29 '18 at 5:33
1
That is because you have set length in your database columnrole_id
that is exceeding when you are selecting more than one roles in your dropdown. so make it larger asvarchar(256)
– Leena Patel
Nov 29 '18 at 5:45
1
userequired|array
to validate as an array
– Leena Patel
Nov 29 '18 at 6:36
1
I am following ur suggestions.
– Ali Anwar
Nov 29 '18 at 6:54
Thanks for replying to my question but I am getting error again with this code.
– Ali Anwar
Nov 29 '18 at 5:31
Thanks for replying to my question but I am getting error again with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
1
what is error ?
– Leena Patel
Nov 29 '18 at 5:33
what is error ?
– Leena Patel
Nov 29 '18 at 5:33
1
1
That is because you have set length in your database column
role_id
that is exceeding when you are selecting more than one roles in your dropdown. so make it larger as varchar(256)
– Leena Patel
Nov 29 '18 at 5:45
That is because you have set length in your database column
role_id
that is exceeding when you are selecting more than one roles in your dropdown. so make it larger as varchar(256)
– Leena Patel
Nov 29 '18 at 5:45
1
1
use
required|array
to validate as an array– Leena Patel
Nov 29 '18 at 6:36
use
required|array
to validate as an array– Leena Patel
Nov 29 '18 at 6:36
1
1
I am following ur suggestions.
– Ali Anwar
Nov 29 '18 at 6:54
I am following ur suggestions.
– Ali Anwar
Nov 29 '18 at 6:54
|
show 11 more comments
Problem:
$request->role_id
is an array. not a single data.
Suggestion:
Here two things
You allocating multiple rules with an user
You should not store your rules in users table
You should do that like this:
create another table to store users role
and store user rules separately
Example:
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
$roleAssigns = ;
foreach($request->role_id as $role){
$roleAssigns = [
'role_id' => $role,
'user_id' => $user->id
]
}
//UserRole is the model of user_roles table
UserRole::insert($roleAssigns);
You have pointed out the problem rightly. I tried your code but again I am getting syntax error. I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer". I have seen many people using many to many relationships adding data the same way.
– Ali Anwar
Nov 29 '18 at 5:28
1
if controller want integer type role_id then you should pass integer from view. its the view issue.
– Emtiaz Zahid
Nov 29 '18 at 5:32
I am following the process as Alex is following in many to many relations in his youtube tutorial. I have also seen in many tutorials for the many to many relationships. But in my case its not working. Please check this 33:00 to 33:35 in this tutorial youtube.com/watch?v=BNUYaLWdR04&t=232s –
– Ali Anwar
Nov 29 '18 at 5:48
add a comment |
Problem:
$request->role_id
is an array. not a single data.
Suggestion:
Here two things
You allocating multiple rules with an user
You should not store your rules in users table
You should do that like this:
create another table to store users role
and store user rules separately
Example:
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
$roleAssigns = ;
foreach($request->role_id as $role){
$roleAssigns = [
'role_id' => $role,
'user_id' => $user->id
]
}
//UserRole is the model of user_roles table
UserRole::insert($roleAssigns);
You have pointed out the problem rightly. I tried your code but again I am getting syntax error. I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer". I have seen many people using many to many relationships adding data the same way.
– Ali Anwar
Nov 29 '18 at 5:28
1
if controller want integer type role_id then you should pass integer from view. its the view issue.
– Emtiaz Zahid
Nov 29 '18 at 5:32
I am following the process as Alex is following in many to many relations in his youtube tutorial. I have also seen in many tutorials for the many to many relationships. But in my case its not working. Please check this 33:00 to 33:35 in this tutorial youtube.com/watch?v=BNUYaLWdR04&t=232s –
– Ali Anwar
Nov 29 '18 at 5:48
add a comment |
Problem:
$request->role_id
is an array. not a single data.
Suggestion:
Here two things
You allocating multiple rules with an user
You should not store your rules in users table
You should do that like this:
create another table to store users role
and store user rules separately
Example:
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
$roleAssigns = ;
foreach($request->role_id as $role){
$roleAssigns = [
'role_id' => $role,
'user_id' => $user->id
]
}
//UserRole is the model of user_roles table
UserRole::insert($roleAssigns);
Problem:
$request->role_id
is an array. not a single data.
Suggestion:
Here two things
You allocating multiple rules with an user
You should not store your rules in users table
You should do that like this:
create another table to store users role
and store user rules separately
Example:
$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();
$roleAssigns = ;
foreach($request->role_id as $role){
$roleAssigns = [
'role_id' => $role,
'user_id' => $user->id
]
}
//UserRole is the model of user_roles table
UserRole::insert($roleAssigns);
edited Nov 29 '18 at 5:08
answered Nov 29 '18 at 5:06
Emtiaz ZahidEmtiaz Zahid
1,068617
1,068617
You have pointed out the problem rightly. I tried your code but again I am getting syntax error. I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer". I have seen many people using many to many relationships adding data the same way.
– Ali Anwar
Nov 29 '18 at 5:28
1
if controller want integer type role_id then you should pass integer from view. its the view issue.
– Emtiaz Zahid
Nov 29 '18 at 5:32
I am following the process as Alex is following in many to many relations in his youtube tutorial. I have also seen in many tutorials for the many to many relationships. But in my case its not working. Please check this 33:00 to 33:35 in this tutorial youtube.com/watch?v=BNUYaLWdR04&t=232s –
– Ali Anwar
Nov 29 '18 at 5:48
add a comment |
You have pointed out the problem rightly. I tried your code but again I am getting syntax error. I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer". I have seen many people using many to many relationships adding data the same way.
– Ali Anwar
Nov 29 '18 at 5:28
1
if controller want integer type role_id then you should pass integer from view. its the view issue.
– Emtiaz Zahid
Nov 29 '18 at 5:32
I am following the process as Alex is following in many to many relations in his youtube tutorial. I have also seen in many tutorials for the many to many relationships. But in my case its not working. Please check this 33:00 to 33:35 in this tutorial youtube.com/watch?v=BNUYaLWdR04&t=232s –
– Ali Anwar
Nov 29 '18 at 5:48
You have pointed out the problem rightly. I tried your code but again I am getting syntax error. I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer". I have seen many people using many to many relationships adding data the same way.
– Ali Anwar
Nov 29 '18 at 5:28
You have pointed out the problem rightly. I tried your code but again I am getting syntax error. I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer". I have seen many people using many to many relationships adding data the same way.
– Ali Anwar
Nov 29 '18 at 5:28
1
1
if controller want integer type role_id then you should pass integer from view. its the view issue.
– Emtiaz Zahid
Nov 29 '18 at 5:32
if controller want integer type role_id then you should pass integer from view. its the view issue.
– Emtiaz Zahid
Nov 29 '18 at 5:32
I am following the process as Alex is following in many to many relations in his youtube tutorial. I have also seen in many tutorials for the many to many relationships. But in my case its not working. Please check this 33:00 to 33:35 in this tutorial youtube.com/watch?v=BNUYaLWdR04&t=232s –
– Ali Anwar
Nov 29 '18 at 5:48
I am following the process as Alex is following in many to many relations in his youtube tutorial. I have also seen in many tutorials for the many to many relationships. But in my case its not working. Please check this 33:00 to 33:35 in this tutorial youtube.com/watch?v=BNUYaLWdR04&t=232s –
– Ali Anwar
Nov 29 '18 at 5:48
add a comment |
$request->role_id
is array so you can't store array to database directly so you can use following,
$user->role_id = json_encode($request->role_id);
Later you can use json_decode
function to get array of role_id.
Thanks for answering to my question. I am getting another error with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
@AliAnwar Can you add the error please
– Sagar Gautam
Nov 29 '18 at 5:34
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["2","3"]' for column 'role_id' at row 1 (SQL: insert intousers
(name
,status
,role_id
,email
,password
,updated_at
,created_at
) values (Admin, 0, ["2","3"], admin@xyz.com, $2y$10$DEzjq.XtHPL06FnbtPTa9Oht0Bm5k9eJQT1dh6.A/5TBmVqMGi3yy, 2018-11-29 06:22:48, 2018-11-29 06:22:48))
– Ali Anwar
Nov 29 '18 at 6:23
add a comment |
$request->role_id
is array so you can't store array to database directly so you can use following,
$user->role_id = json_encode($request->role_id);
Later you can use json_decode
function to get array of role_id.
Thanks for answering to my question. I am getting another error with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
@AliAnwar Can you add the error please
– Sagar Gautam
Nov 29 '18 at 5:34
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["2","3"]' for column 'role_id' at row 1 (SQL: insert intousers
(name
,status
,role_id
,email
,password
,updated_at
,created_at
) values (Admin, 0, ["2","3"], admin@xyz.com, $2y$10$DEzjq.XtHPL06FnbtPTa9Oht0Bm5k9eJQT1dh6.A/5TBmVqMGi3yy, 2018-11-29 06:22:48, 2018-11-29 06:22:48))
– Ali Anwar
Nov 29 '18 at 6:23
add a comment |
$request->role_id
is array so you can't store array to database directly so you can use following,
$user->role_id = json_encode($request->role_id);
Later you can use json_decode
function to get array of role_id.
$request->role_id
is array so you can't store array to database directly so you can use following,
$user->role_id = json_encode($request->role_id);
Later you can use json_decode
function to get array of role_id.
answered Nov 29 '18 at 5:05
Sagar GautamSagar Gautam
4,38431644
4,38431644
Thanks for answering to my question. I am getting another error with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
@AliAnwar Can you add the error please
– Sagar Gautam
Nov 29 '18 at 5:34
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["2","3"]' for column 'role_id' at row 1 (SQL: insert intousers
(name
,status
,role_id
,email
,password
,updated_at
,created_at
) values (Admin, 0, ["2","3"], admin@xyz.com, $2y$10$DEzjq.XtHPL06FnbtPTa9Oht0Bm5k9eJQT1dh6.A/5TBmVqMGi3yy, 2018-11-29 06:22:48, 2018-11-29 06:22:48))
– Ali Anwar
Nov 29 '18 at 6:23
add a comment |
Thanks for answering to my question. I am getting another error with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
@AliAnwar Can you add the error please
– Sagar Gautam
Nov 29 '18 at 5:34
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["2","3"]' for column 'role_id' at row 1 (SQL: insert intousers
(name
,status
,role_id
,email
,password
,updated_at
,created_at
) values (Admin, 0, ["2","3"], admin@xyz.com, $2y$10$DEzjq.XtHPL06FnbtPTa9Oht0Bm5k9eJQT1dh6.A/5TBmVqMGi3yy, 2018-11-29 06:22:48, 2018-11-29 06:22:48))
– Ali Anwar
Nov 29 '18 at 6:23
Thanks for answering to my question. I am getting another error with this code.
– Ali Anwar
Nov 29 '18 at 5:31
Thanks for answering to my question. I am getting another error with this code.
– Ali Anwar
Nov 29 '18 at 5:31
1
1
@AliAnwar Can you add the error please
– Sagar Gautam
Nov 29 '18 at 5:34
@AliAnwar Can you add the error please
– Sagar Gautam
Nov 29 '18 at 5:34
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["2","3"]' for column 'role_id' at row 1 (SQL: insert into
users
(name
, status
, role_id
, email
, password
, updated_at
, created_at
) values (Admin, 0, ["2","3"], admin@xyz.com, $2y$10$DEzjq.XtHPL06FnbtPTa9Oht0Bm5k9eJQT1dh6.A/5TBmVqMGi3yy, 2018-11-29 06:22:48, 2018-11-29 06:22:48))– Ali Anwar
Nov 29 '18 at 6:23
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["2","3"]' for column 'role_id' at row 1 (SQL: insert into
users
(name
, status
, role_id
, email
, password
, updated_at
, created_at
) values (Admin, 0, ["2","3"], admin@xyz.com, $2y$10$DEzjq.XtHPL06FnbtPTa9Oht0Bm5k9eJQT1dh6.A/5TBmVqMGi3yy, 2018-11-29 06:22:48, 2018-11-29 06:22:48))– Ali Anwar
Nov 29 '18 at 6:23
add a comment |
You cannot "echo" an array, that is the error.
you will need to collect the lines in an array, and then return that array
add a comment |
You cannot "echo" an array, that is the error.
you will need to collect the lines in an array, and then return that array
add a comment |
You cannot "echo" an array, that is the error.
you will need to collect the lines in an array, and then return that array
You cannot "echo" an array, that is the error.
you will need to collect the lines in an array, and then return that array
answered Feb 27 at 6:26
Junaid AbbasJunaid Abbas
62
62
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53532112%2flaravel-error-array-to-string-conversion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Change it to
'role_id.*'=> 'required',
and role_id is collection of array you should run that with loop and then insert inusers role
table since user have more than one role you need to keep them in another table like user_roles– JYoThI
Nov 29 '18 at 5:00
1
error is here
$user->role_id = $request->role_id;
in this line you are assigning array ($request->role_id
) to single variable. So, you should first create array and then used foreach loop on$request->role_id
to assign value to array– Bilal Ahmed
Nov 29 '18 at 5:11
I have another error in validation as well. If I use 'role_id'=> 'required|integer', instead of 'role_id'=> 'required', I get error "The role id is not integer".
– Ali Anwar
Nov 29 '18 at 5:25
1
@AliAnwar whats the error?? You should include the actual error message in your question
– Sohel0415
Nov 29 '18 at 5:45
error is "The role id is not integer" when I validate for Integer.
– Ali Anwar
Nov 29 '18 at 6:08