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













0















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/resources/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'));










share|improve this question
























  • 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















0















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/resources/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'));










share|improve this question
























  • 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













0












0








0








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/resources/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'));










share|improve this question
















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/resources/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|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















1














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





share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









1














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





share|improve this answer

























  • 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















1














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





share|improve this answer

























  • 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













1












1








1







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page