How to pass a function in laravel view?Best Practices for Custom Helpers in Laravel 5Laravel 5 Failed opening required bootstrap/../vendor/autoload.phpHow to pass data to all views in Laravel 5?Trouble getting attribute of relation in Laravelproblems to add data in pivot table laravel 5.1Menu in laravel 5 Multiple levels?Get Nested json array of data Laravel Eloquent model with RelationshipLaravel 5 - Grouping, Counting and Outputting eager loaded relationshipsLaravel 5.6 API Resource not showing Relationship dataGet all objects that exist in two collections
What is the best translation for "slot" in the context of multiplayer video games?
How can I get through very long and very dry, but also very useful technical documents when learning a new tool?
Sequence of Tenses: Translating the subjunctive
Why didn't Theresa May consult with Parliament before negotiating a deal with the EU?
Is the destination of a commercial flight important for the pilot?
Is it appropriate to ask a job candidate if we can record their interview?
Implement the Thanos sorting algorithm
Is `x >> pure y` equivalent to `liftM (const y) x`
How did Arya survive the stabbing?
What can we do to stop prior company from asking us questions?
How do I find the solutions of the following equation?
What is the difference between "behavior" and "behaviour"?
Lay out the Carpet
How to Reset Passwords on Multiple Websites Easily?
Why does indent disappear in lists?
How can I kill an app using Terminal?
What is the intuitive meaning of having a linear relationship between the logs of two variables?
Is HostGator storing my password in plaintext?
How to be diplomatic in refusing to write code that breaches the privacy of our users
Unreliable Magic - Is it worth it?
Short story about space worker geeks who zone out by 'listening' to radiation from stars
Tiptoe or tiphoof? Adjusting words to better fit fantasy races
Class Action - which options I have?
Roman Numeral Treatment of Suspensions
How to pass a function in laravel view?
Best Practices for Custom Helpers in Laravel 5Laravel 5 Failed opening required bootstrap/../vendor/autoload.phpHow to pass data to all views in Laravel 5?Trouble getting attribute of relation in Laravelproblems to add data in pivot table laravel 5.1Menu in laravel 5 Multiple levels?Get Nested json array of data Laravel Eloquent model with RelationshipLaravel 5 - Grouping, Counting and Outputting eager loaded relationshipsLaravel 5.6 API Resource not showing Relationship dataGet all objects that exist in two collections
I have the following a Course Model with a function of trainers and I'm trying to pass it to the view so I can see the trainers for the course.
Here is my model function
public function trainers()
return $this->belongsToMany(User::class, 'course_user');
This is my view where I'm trying to pass the Model Course and the function trainers.
<h6 class="card-subtitle text-muted">Trainer: $course->trainers()</h6>
And this is the error I'm getting:
htmlspecialchars() expects parameter 1 to be string, object given (View: /Applications/MAMP/htdocs/hs-03/re
sources/views/admin/courses/showCourse.blade.php)
Here is my controller:
public function show($id)
$created_bies = AppUser::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$trainers = AppUser::get()->pluck('name', 'id');
$tests = AppTest::where('course_id', $id)->get();$lessons = AppLesson::where('course_id', $id)->get();
// $date = Carbon::now();
$date = Carbon::now()->addDays(30)->toFormattedDateString();
$user = User::find(1);
$user->name;
$course = Course::findOrFail($id);
return view('admin.courses.showCourse', compact('course', 'tests', 'lessons', 'date', 'user'));
laravel-5
add a comment |
I have the following a Course Model with a function of trainers and I'm trying to pass it to the view so I can see the trainers for the course.
Here is my model function
public function trainers()
return $this->belongsToMany(User::class, 'course_user');
This is my view where I'm trying to pass the Model Course and the function trainers.
<h6 class="card-subtitle text-muted">Trainer: $course->trainers()</h6>
And this is the error I'm getting:
htmlspecialchars() expects parameter 1 to be string, object given (View: /Applications/MAMP/htdocs/hs-03/re
sources/views/admin/courses/showCourse.blade.php)
Here is my controller:
public function show($id)
$created_bies = AppUser::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$trainers = AppUser::get()->pluck('name', 'id');
$tests = AppTest::where('course_id', $id)->get();$lessons = AppLesson::where('course_id', $id)->get();
// $date = Carbon::now();
$date = Carbon::now()->addDays(30)->toFormattedDateString();
$user = User::find(1);
$user->name;
$course = Course::findOrFail($id);
return view('admin.courses.showCourse', compact('course', 'tests', 'lessons', 'date', 'user'));
laravel-5
Share your controller code.
– Vinod Joshi
Mar 8 at 11:44
In your view file $course->trainers() its object you can't print that like this. It would have multiple ojects.
– Vinod Joshi
Mar 8 at 11:46
I think I need something like $course->trainers()->get() since I'm getting the data but I have no idea how to get just the name. This get me everything Trainer: ["id":4,"name":"Eddy Murphy","email":"murphy@admin.com","email_verified_at":null,"created_at":"2019-03-03 13:40:58","updated_at":"2019-03-03 13:40:58","created_by_id":1,"pivot":"course_id":54,"user_id":4]
– Simona Buga
Mar 8 at 12:07
add a comment |
I have the following a Course Model with a function of trainers and I'm trying to pass it to the view so I can see the trainers for the course.
Here is my model function
public function trainers()
return $this->belongsToMany(User::class, 'course_user');
This is my view where I'm trying to pass the Model Course and the function trainers.
<h6 class="card-subtitle text-muted">Trainer: $course->trainers()</h6>
And this is the error I'm getting:
htmlspecialchars() expects parameter 1 to be string, object given (View: /Applications/MAMP/htdocs/hs-03/re
sources/views/admin/courses/showCourse.blade.php)
Here is my controller:
public function show($id)
$created_bies = AppUser::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$trainers = AppUser::get()->pluck('name', 'id');
$tests = AppTest::where('course_id', $id)->get();$lessons = AppLesson::where('course_id', $id)->get();
// $date = Carbon::now();
$date = Carbon::now()->addDays(30)->toFormattedDateString();
$user = User::find(1);
$user->name;
$course = Course::findOrFail($id);
return view('admin.courses.showCourse', compact('course', 'tests', 'lessons', 'date', 'user'));
laravel-5
I have the following a Course Model with a function of trainers and I'm trying to pass it to the view so I can see the trainers for the course.
Here is my model function
public function trainers()
return $this->belongsToMany(User::class, 'course_user');
This is my view where I'm trying to pass the Model Course and the function trainers.
<h6 class="card-subtitle text-muted">Trainer: $course->trainers()</h6>
And this is the error I'm getting:
htmlspecialchars() expects parameter 1 to be string, object given (View: /Applications/MAMP/htdocs/hs-03/re
sources/views/admin/courses/showCourse.blade.php)
Here is my controller:
public function show($id)
$created_bies = AppUser::get()->pluck('name', 'id')->prepend(trans('global.app_please_select'), '');
$trainers = AppUser::get()->pluck('name', 'id');
$tests = AppTest::where('course_id', $id)->get();$lessons = AppLesson::where('course_id', $id)->get();
// $date = Carbon::now();
$date = Carbon::now()->addDays(30)->toFormattedDateString();
$user = User::find(1);
$user->name;
$course = Course::findOrFail($id);
return view('admin.courses.showCourse', compact('course', 'tests', 'lessons', 'date', 'user'));
laravel-5
laravel-5
edited Mar 8 at 12:05
Simona Buga
asked Mar 8 at 11:29
Simona BugaSimona Buga
237
237
Share your controller code.
– Vinod Joshi
Mar 8 at 11:44
In your view file $course->trainers() its object you can't print that like this. It would have multiple ojects.
– Vinod Joshi
Mar 8 at 11:46
I think I need something like $course->trainers()->get() since I'm getting the data but I have no idea how to get just the name. This get me everything Trainer: ["id":4,"name":"Eddy Murphy","email":"murphy@admin.com","email_verified_at":null,"created_at":"2019-03-03 13:40:58","updated_at":"2019-03-03 13:40:58","created_by_id":1,"pivot":"course_id":54,"user_id":4]
– Simona Buga
Mar 8 at 12:07
add a comment |
Share your controller code.
– Vinod Joshi
Mar 8 at 11:44
In your view file $course->trainers() its object you can't print that like this. It would have multiple ojects.
– Vinod Joshi
Mar 8 at 11:46
I think I need something like $course->trainers()->get() since I'm getting the data but I have no idea how to get just the name. This get me everything Trainer: ["id":4,"name":"Eddy Murphy","email":"murphy@admin.com","email_verified_at":null,"created_at":"2019-03-03 13:40:58","updated_at":"2019-03-03 13:40:58","created_by_id":1,"pivot":"course_id":54,"user_id":4]
– Simona Buga
Mar 8 at 12:07
Share your controller code.
– Vinod Joshi
Mar 8 at 11:44
Share your controller code.
– Vinod Joshi
Mar 8 at 11:44
In your view file $course->trainers() its object you can't print that like this. It would have multiple ojects.
– Vinod Joshi
Mar 8 at 11:46
In your view file $course->trainers() its object you can't print that like this. It would have multiple ojects.
– Vinod Joshi
Mar 8 at 11:46
I think I need something like $course->trainers()->get() since I'm getting the data but I have no idea how to get just the name. This get me everything Trainer: ["id":4,"name":"Eddy Murphy","email":"murphy@admin.com","email_verified_at":null,"created_at":"2019-03-03 13:40:58","updated_at":"2019-03-03 13:40:58","created_by_id":1,"pivot":"course_id":54,"user_id":4]
– Simona Buga
Mar 8 at 12:07
I think I need something like $course->trainers()->get() since I'm getting the data but I have no idea how to get just the name. This get me everything Trainer: ["id":4,"name":"Eddy Murphy","email":"murphy@admin.com","email_verified_at":null,"created_at":"2019-03-03 13:40:58","updated_at":"2019-03-03 13:40:58","created_by_id":1,"pivot":"course_id":54,"user_id":4]
– Simona Buga
Mar 8 at 12:07
add a comment |
1 Answer
1
active
oldest
votes
This is due to the fact that the trainers
methods isn't return a String. The method like It is define in your Model is Supposed to return a Illuminate/Database/Eloquent/Relations/BelongsToMany
object like you can see here. The error is because your are trying to show that object directly within your template as a String which is not possible. I suppose you want to show some thing like number of trainers. Which you can get like
<h6 class="card-subtitle text-muted">Trainer: $course->trainers()->count()</h6>
This is possible because the count
method return an integer and not a object like trainers
.
On another hand If you want to stick to that usage, you must insert a loop within your view which will walk through the collection of trainers which is being return by the call to $course->trainers()
@foreach($course->trainers() as $trainer)
<h6 class="card-subtitle text-muted">Trainer: $trainer->name </h6>
@endforeach
looping worked, I was using the wrong naming conventions. Thanks so much
– Simona Buga
Mar 8 at 12:39
1
You are Welcome
– Yves Kipondo
Mar 8 at 12:51
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%2f55062337%2fhow-to-pass-a-function-in-laravel-view%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
This is due to the fact that the trainers
methods isn't return a String. The method like It is define in your Model is Supposed to return a Illuminate/Database/Eloquent/Relations/BelongsToMany
object like you can see here. The error is because your are trying to show that object directly within your template as a String which is not possible. I suppose you want to show some thing like number of trainers. Which you can get like
<h6 class="card-subtitle text-muted">Trainer: $course->trainers()->count()</h6>
This is possible because the count
method return an integer and not a object like trainers
.
On another hand If you want to stick to that usage, you must insert a loop within your view which will walk through the collection of trainers which is being return by the call to $course->trainers()
@foreach($course->trainers() as $trainer)
<h6 class="card-subtitle text-muted">Trainer: $trainer->name </h6>
@endforeach
looping worked, I was using the wrong naming conventions. Thanks so much
– Simona Buga
Mar 8 at 12:39
1
You are Welcome
– Yves Kipondo
Mar 8 at 12:51
add a comment |
This is due to the fact that the trainers
methods isn't return a String. The method like It is define in your Model is Supposed to return a Illuminate/Database/Eloquent/Relations/BelongsToMany
object like you can see here. The error is because your are trying to show that object directly within your template as a String which is not possible. I suppose you want to show some thing like number of trainers. Which you can get like
<h6 class="card-subtitle text-muted">Trainer: $course->trainers()->count()</h6>
This is possible because the count
method return an integer and not a object like trainers
.
On another hand If you want to stick to that usage, you must insert a loop within your view which will walk through the collection of trainers which is being return by the call to $course->trainers()
@foreach($course->trainers() as $trainer)
<h6 class="card-subtitle text-muted">Trainer: $trainer->name </h6>
@endforeach
looping worked, I was using the wrong naming conventions. Thanks so much
– Simona Buga
Mar 8 at 12:39
1
You are Welcome
– Yves Kipondo
Mar 8 at 12:51
add a comment |
This is due to the fact that the trainers
methods isn't return a String. The method like It is define in your Model is Supposed to return a Illuminate/Database/Eloquent/Relations/BelongsToMany
object like you can see here. The error is because your are trying to show that object directly within your template as a String which is not possible. I suppose you want to show some thing like number of trainers. Which you can get like
<h6 class="card-subtitle text-muted">Trainer: $course->trainers()->count()</h6>
This is possible because the count
method return an integer and not a object like trainers
.
On another hand If you want to stick to that usage, you must insert a loop within your view which will walk through the collection of trainers which is being return by the call to $course->trainers()
@foreach($course->trainers() as $trainer)
<h6 class="card-subtitle text-muted">Trainer: $trainer->name </h6>
@endforeach
This is due to the fact that the trainers
methods isn't return a String. The method like It is define in your Model is Supposed to return a Illuminate/Database/Eloquent/Relations/BelongsToMany
object like you can see here. The error is because your are trying to show that object directly within your template as a String which is not possible. I suppose you want to show some thing like number of trainers. Which you can get like
<h6 class="card-subtitle text-muted">Trainer: $course->trainers()->count()</h6>
This is possible because the count
method return an integer and not a object like trainers
.
On another hand If you want to stick to that usage, you must insert a loop within your view which will walk through the collection of trainers which is being return by the call to $course->trainers()
@foreach($course->trainers() as $trainer)
<h6 class="card-subtitle text-muted">Trainer: $trainer->name </h6>
@endforeach
edited Mar 8 at 11:53
answered Mar 8 at 11:48
Yves KipondoYves Kipondo
1,521715
1,521715
looping worked, I was using the wrong naming conventions. Thanks so much
– Simona Buga
Mar 8 at 12:39
1
You are Welcome
– Yves Kipondo
Mar 8 at 12:51
add a comment |
looping worked, I was using the wrong naming conventions. Thanks so much
– Simona Buga
Mar 8 at 12:39
1
You are Welcome
– Yves Kipondo
Mar 8 at 12:51
looping worked, I was using the wrong naming conventions. Thanks so much
– Simona Buga
Mar 8 at 12:39
looping worked, I was using the wrong naming conventions. Thanks so much
– Simona Buga
Mar 8 at 12:39
1
1
You are Welcome
– Yves Kipondo
Mar 8 at 12:51
You are Welcome
– Yves Kipondo
Mar 8 at 12:51
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%2f55062337%2fhow-to-pass-a-function-in-laravel-view%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
Share your controller code.
– Vinod Joshi
Mar 8 at 11:44
In your view file $course->trainers() its object you can't print that like this. It would have multiple ojects.
– Vinod Joshi
Mar 8 at 11:46
I think I need something like $course->trainers()->get() since I'm getting the data but I have no idea how to get just the name. This get me everything Trainer: ["id":4,"name":"Eddy Murphy","email":"murphy@admin.com","email_verified_at":null,"created_at":"2019-03-03 13:40:58","updated_at":"2019-03-03 13:40:58","created_by_id":1,"pivot":"course_id":54,"user_id":4]
– Simona Buga
Mar 8 at 12:07