Error: “Object of class Closure could not be converted to string” while using a factory in Laravel
I am trying to create some dummy data to testunit my appication in Laravel 5.7.
I am using a factory to create:
- Profiles
- 5 Events that belongs to that profile
- 10 Eventcomments that belongs to each of the previous events.
All in one.
My factory looks like this:
$factory->define(AppProfile::class, function (Faker $faker) {
$profile = Profile::create([
'title' => $faker->userName,
'gender_id' => 0,
'role_id' => 1,
'slug' => str_slug($faker->userName),
]);
for ($i=0; $i < 5; $i++) {
$event = Event::create([
'profile_id' => $profile->id,
'title' => $faker->sentence,
'subtitle' => $faker->sentence,
'slug' => str_slug($faker->sentence),
'category_id' => 1,
'language_id' => 1,
'video_url' => 'video',
'description' => $faker->paragraph,
]);
for ($i=0; $i < 10; $i++) {
$eventcomment = Eventcomment::create([
'profile_id' => $profile->id,
'event_id' => $event->id,
'body' => $faker->paragraph,
]);
}
}
return $profile;
});
But when I run it in Tinker like this:
php artisan tinker
>>>factory('AppProfile', 20)->create();
I get the following error:
PHP Recoverable fatal error: Object of class Closure could not be converted to string in
C:/laragon/www/definitive/vendor/laravel/framework/src/Illuminate/Support/Str.php
on line 338
Any idea how to solve this problem?
php laravel factory
add a comment |
I am trying to create some dummy data to testunit my appication in Laravel 5.7.
I am using a factory to create:
- Profiles
- 5 Events that belongs to that profile
- 10 Eventcomments that belongs to each of the previous events.
All in one.
My factory looks like this:
$factory->define(AppProfile::class, function (Faker $faker) {
$profile = Profile::create([
'title' => $faker->userName,
'gender_id' => 0,
'role_id' => 1,
'slug' => str_slug($faker->userName),
]);
for ($i=0; $i < 5; $i++) {
$event = Event::create([
'profile_id' => $profile->id,
'title' => $faker->sentence,
'subtitle' => $faker->sentence,
'slug' => str_slug($faker->sentence),
'category_id' => 1,
'language_id' => 1,
'video_url' => 'video',
'description' => $faker->paragraph,
]);
for ($i=0; $i < 10; $i++) {
$eventcomment = Eventcomment::create([
'profile_id' => $profile->id,
'event_id' => $event->id,
'body' => $faker->paragraph,
]);
}
}
return $profile;
});
But when I run it in Tinker like this:
php artisan tinker
>>>factory('AppProfile', 20)->create();
I get the following error:
PHP Recoverable fatal error: Object of class Closure could not be converted to string in
C:/laragon/www/definitive/vendor/laravel/framework/src/Illuminate/Support/Str.php
on line 338
Any idea how to solve this problem?
php laravel factory
Have you modified any config files recently? Sometimes this error can be hard to track when a closure is mistakenly used in a configuration file.
– DigitalDrifter
Jan 1 at 10:48
no, I did not modify any config file.
– Rafael Munoz
Jan 1 at 11:37
add a comment |
I am trying to create some dummy data to testunit my appication in Laravel 5.7.
I am using a factory to create:
- Profiles
- 5 Events that belongs to that profile
- 10 Eventcomments that belongs to each of the previous events.
All in one.
My factory looks like this:
$factory->define(AppProfile::class, function (Faker $faker) {
$profile = Profile::create([
'title' => $faker->userName,
'gender_id' => 0,
'role_id' => 1,
'slug' => str_slug($faker->userName),
]);
for ($i=0; $i < 5; $i++) {
$event = Event::create([
'profile_id' => $profile->id,
'title' => $faker->sentence,
'subtitle' => $faker->sentence,
'slug' => str_slug($faker->sentence),
'category_id' => 1,
'language_id' => 1,
'video_url' => 'video',
'description' => $faker->paragraph,
]);
for ($i=0; $i < 10; $i++) {
$eventcomment = Eventcomment::create([
'profile_id' => $profile->id,
'event_id' => $event->id,
'body' => $faker->paragraph,
]);
}
}
return $profile;
});
But when I run it in Tinker like this:
php artisan tinker
>>>factory('AppProfile', 20)->create();
I get the following error:
PHP Recoverable fatal error: Object of class Closure could not be converted to string in
C:/laragon/www/definitive/vendor/laravel/framework/src/Illuminate/Support/Str.php
on line 338
Any idea how to solve this problem?
php laravel factory
I am trying to create some dummy data to testunit my appication in Laravel 5.7.
I am using a factory to create:
- Profiles
- 5 Events that belongs to that profile
- 10 Eventcomments that belongs to each of the previous events.
All in one.
My factory looks like this:
$factory->define(AppProfile::class, function (Faker $faker) {
$profile = Profile::create([
'title' => $faker->userName,
'gender_id' => 0,
'role_id' => 1,
'slug' => str_slug($faker->userName),
]);
for ($i=0; $i < 5; $i++) {
$event = Event::create([
'profile_id' => $profile->id,
'title' => $faker->sentence,
'subtitle' => $faker->sentence,
'slug' => str_slug($faker->sentence),
'category_id' => 1,
'language_id' => 1,
'video_url' => 'video',
'description' => $faker->paragraph,
]);
for ($i=0; $i < 10; $i++) {
$eventcomment = Eventcomment::create([
'profile_id' => $profile->id,
'event_id' => $event->id,
'body' => $faker->paragraph,
]);
}
}
return $profile;
});
But when I run it in Tinker like this:
php artisan tinker
>>>factory('AppProfile', 20)->create();
I get the following error:
PHP Recoverable fatal error: Object of class Closure could not be converted to string in
C:/laragon/www/definitive/vendor/laravel/framework/src/Illuminate/Support/Str.php
on line 338
Any idea how to solve this problem?
php laravel factory
php laravel factory
edited Jan 1 at 15:34
Bhoomi patel
506218
506218
asked Jan 1 at 10:43


Rafael MunozRafael Munoz
2111416
2111416
Have you modified any config files recently? Sometimes this error can be hard to track when a closure is mistakenly used in a configuration file.
– DigitalDrifter
Jan 1 at 10:48
no, I did not modify any config file.
– Rafael Munoz
Jan 1 at 11:37
add a comment |
Have you modified any config files recently? Sometimes this error can be hard to track when a closure is mistakenly used in a configuration file.
– DigitalDrifter
Jan 1 at 10:48
no, I did not modify any config file.
– Rafael Munoz
Jan 1 at 11:37
Have you modified any config files recently? Sometimes this error can be hard to track when a closure is mistakenly used in a configuration file.
– DigitalDrifter
Jan 1 at 10:48
Have you modified any config files recently? Sometimes this error can be hard to track when a closure is mistakenly used in a configuration file.
– DigitalDrifter
Jan 1 at 10:48
no, I did not modify any config file.
– Rafael Munoz
Jan 1 at 11:37
no, I did not modify any config file.
– Rafael Munoz
Jan 1 at 11:37
add a comment |
1 Answer
1
active
oldest
votes
If you want to customize any string, it should be outside of create method.
$factory->define(AppProfile::class, function (Faker $faker) {
$slugName = str_slug($faker->userName);
$profile = Profile::create([
'title' => $faker->userName,
'gender_id' => 0,
'role_id' => 1,
'slug' => $slugName,
]);
$slugSentence = str_slug($faker->sentence);
$profileId = $profile->id;
for ($i=0; $i < 5; $i++) {
$event = Event::create([
'profile_id' => $profileId,
'title' => $faker->sentence,
'subtitle' => $faker->sentence,
'slug' => $slugSentence,
'category_id' => 1,
'language_id' => 1,
'video_url' => 'video',
'description' => $faker->paragraph,
]);
$eventId = $event->id;
for ($i=0; $i < 10; $i++) {
$eventcomment = Eventcomment::create([
'profile_id' => $profileId,
'event_id' => $eventId,
'body' => $faker->paragraph,
]);
}
}
return $profile;
});
Or You can use Closure attributes
1
Ok. But it does not solve the error
– Rafael Munoz
Jan 1 at 11:36
Try my updated answer. please
– Md.Sukel Ali
Jan 1 at 11:47
Is your error still showing ?
– Md.Sukel Ali
Jan 1 at 12:05
Yes. Unfortunately it still shows the same error
– Rafael Munoz
Jan 1 at 12:22
Exit from tinker. and run your command again. It's working in my environment.
– Md.Sukel Ali
Jan 1 at 14:36
|
show 1 more 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%2f53994814%2ferror-object-of-class-closure-could-not-be-converted-to-string-while-using-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to customize any string, it should be outside of create method.
$factory->define(AppProfile::class, function (Faker $faker) {
$slugName = str_slug($faker->userName);
$profile = Profile::create([
'title' => $faker->userName,
'gender_id' => 0,
'role_id' => 1,
'slug' => $slugName,
]);
$slugSentence = str_slug($faker->sentence);
$profileId = $profile->id;
for ($i=0; $i < 5; $i++) {
$event = Event::create([
'profile_id' => $profileId,
'title' => $faker->sentence,
'subtitle' => $faker->sentence,
'slug' => $slugSentence,
'category_id' => 1,
'language_id' => 1,
'video_url' => 'video',
'description' => $faker->paragraph,
]);
$eventId = $event->id;
for ($i=0; $i < 10; $i++) {
$eventcomment = Eventcomment::create([
'profile_id' => $profileId,
'event_id' => $eventId,
'body' => $faker->paragraph,
]);
}
}
return $profile;
});
Or You can use Closure attributes
1
Ok. But it does not solve the error
– Rafael Munoz
Jan 1 at 11:36
Try my updated answer. please
– Md.Sukel Ali
Jan 1 at 11:47
Is your error still showing ?
– Md.Sukel Ali
Jan 1 at 12:05
Yes. Unfortunately it still shows the same error
– Rafael Munoz
Jan 1 at 12:22
Exit from tinker. and run your command again. It's working in my environment.
– Md.Sukel Ali
Jan 1 at 14:36
|
show 1 more comment
If you want to customize any string, it should be outside of create method.
$factory->define(AppProfile::class, function (Faker $faker) {
$slugName = str_slug($faker->userName);
$profile = Profile::create([
'title' => $faker->userName,
'gender_id' => 0,
'role_id' => 1,
'slug' => $slugName,
]);
$slugSentence = str_slug($faker->sentence);
$profileId = $profile->id;
for ($i=0; $i < 5; $i++) {
$event = Event::create([
'profile_id' => $profileId,
'title' => $faker->sentence,
'subtitle' => $faker->sentence,
'slug' => $slugSentence,
'category_id' => 1,
'language_id' => 1,
'video_url' => 'video',
'description' => $faker->paragraph,
]);
$eventId = $event->id;
for ($i=0; $i < 10; $i++) {
$eventcomment = Eventcomment::create([
'profile_id' => $profileId,
'event_id' => $eventId,
'body' => $faker->paragraph,
]);
}
}
return $profile;
});
Or You can use Closure attributes
1
Ok. But it does not solve the error
– Rafael Munoz
Jan 1 at 11:36
Try my updated answer. please
– Md.Sukel Ali
Jan 1 at 11:47
Is your error still showing ?
– Md.Sukel Ali
Jan 1 at 12:05
Yes. Unfortunately it still shows the same error
– Rafael Munoz
Jan 1 at 12:22
Exit from tinker. and run your command again. It's working in my environment.
– Md.Sukel Ali
Jan 1 at 14:36
|
show 1 more comment
If you want to customize any string, it should be outside of create method.
$factory->define(AppProfile::class, function (Faker $faker) {
$slugName = str_slug($faker->userName);
$profile = Profile::create([
'title' => $faker->userName,
'gender_id' => 0,
'role_id' => 1,
'slug' => $slugName,
]);
$slugSentence = str_slug($faker->sentence);
$profileId = $profile->id;
for ($i=0; $i < 5; $i++) {
$event = Event::create([
'profile_id' => $profileId,
'title' => $faker->sentence,
'subtitle' => $faker->sentence,
'slug' => $slugSentence,
'category_id' => 1,
'language_id' => 1,
'video_url' => 'video',
'description' => $faker->paragraph,
]);
$eventId = $event->id;
for ($i=0; $i < 10; $i++) {
$eventcomment = Eventcomment::create([
'profile_id' => $profileId,
'event_id' => $eventId,
'body' => $faker->paragraph,
]);
}
}
return $profile;
});
Or You can use Closure attributes
If you want to customize any string, it should be outside of create method.
$factory->define(AppProfile::class, function (Faker $faker) {
$slugName = str_slug($faker->userName);
$profile = Profile::create([
'title' => $faker->userName,
'gender_id' => 0,
'role_id' => 1,
'slug' => $slugName,
]);
$slugSentence = str_slug($faker->sentence);
$profileId = $profile->id;
for ($i=0; $i < 5; $i++) {
$event = Event::create([
'profile_id' => $profileId,
'title' => $faker->sentence,
'subtitle' => $faker->sentence,
'slug' => $slugSentence,
'category_id' => 1,
'language_id' => 1,
'video_url' => 'video',
'description' => $faker->paragraph,
]);
$eventId = $event->id;
for ($i=0; $i < 10; $i++) {
$eventcomment = Eventcomment::create([
'profile_id' => $profileId,
'event_id' => $eventId,
'body' => $faker->paragraph,
]);
}
}
return $profile;
});
Or You can use Closure attributes
edited Jan 1 at 11:46
answered Jan 1 at 11:21


Md.Sukel AliMd.Sukel Ali
1,1701716
1,1701716
1
Ok. But it does not solve the error
– Rafael Munoz
Jan 1 at 11:36
Try my updated answer. please
– Md.Sukel Ali
Jan 1 at 11:47
Is your error still showing ?
– Md.Sukel Ali
Jan 1 at 12:05
Yes. Unfortunately it still shows the same error
– Rafael Munoz
Jan 1 at 12:22
Exit from tinker. and run your command again. It's working in my environment.
– Md.Sukel Ali
Jan 1 at 14:36
|
show 1 more comment
1
Ok. But it does not solve the error
– Rafael Munoz
Jan 1 at 11:36
Try my updated answer. please
– Md.Sukel Ali
Jan 1 at 11:47
Is your error still showing ?
– Md.Sukel Ali
Jan 1 at 12:05
Yes. Unfortunately it still shows the same error
– Rafael Munoz
Jan 1 at 12:22
Exit from tinker. and run your command again. It's working in my environment.
– Md.Sukel Ali
Jan 1 at 14:36
1
1
Ok. But it does not solve the error
– Rafael Munoz
Jan 1 at 11:36
Ok. But it does not solve the error
– Rafael Munoz
Jan 1 at 11:36
Try my updated answer. please
– Md.Sukel Ali
Jan 1 at 11:47
Try my updated answer. please
– Md.Sukel Ali
Jan 1 at 11:47
Is your error still showing ?
– Md.Sukel Ali
Jan 1 at 12:05
Is your error still showing ?
– Md.Sukel Ali
Jan 1 at 12:05
Yes. Unfortunately it still shows the same error
– Rafael Munoz
Jan 1 at 12:22
Yes. Unfortunately it still shows the same error
– Rafael Munoz
Jan 1 at 12:22
Exit from tinker. and run your command again. It's working in my environment.
– Md.Sukel Ali
Jan 1 at 14:36
Exit from tinker. and run your command again. It's working in my environment.
– Md.Sukel Ali
Jan 1 at 14:36
|
show 1 more 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%2f53994814%2ferror-object-of-class-closure-could-not-be-converted-to-string-while-using-a%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
Have you modified any config files recently? Sometimes this error can be hard to track when a closure is mistakenly used in a configuration file.
– DigitalDrifter
Jan 1 at 10:48
no, I did not modify any config file.
– Rafael Munoz
Jan 1 at 11:37