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

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived