1

I am new to Laravel. I want use some Own Functions. Where do Write the Function.

<?php  function userrole1($roleid) {
    $userrole=DB::table('roles')->where('id', '=', $roleid)->get(); 
    ?>
   @foreach($userrole as $val)
    <?php   echo $val->role_title; ?>
  @endforeach
  <?php  
}
 ?>
2
  • which laravel version you are using
    – codeGig
    Mar 9, 2016 at 11:56
  • @Karthik, if my answer was helpful, please upvote and choose my answer as best answer. Mar 21, 2016 at 15:07

3 Answers 3

1

New Way to add Helpers

1: I created folder app/Helpers

2: In app/Providers I created new provider file HelperServiceProvider.php

3: In this file I registered all helpers classes I need

$this->app->bind('dateHelper', function()
{
    return new \App\Helpers\DateHelper;
});
  1. In config/app.php I added this new provider

    'App\Providers\HelperServiceProvider',

Use This helper function dateHelper

Old Way

Create a helpers.php file in your app folder and load it up with composer:

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/helpers.php" // <---- ADD THIS
    ]
},

After adding this run composer dump-autoload command in cmd

0

You need to create and register your own helpers file:

http://laravel-recipes.com/recipes/50/creating-a-helpers-file

After that you'll be able to use custom helpers (functions) in your app.

0
Just make a function in the model class and include model and call it from the controller as pass from there to the view using variable. thats it.

In Model User(you can make any):

public function userrole1($roleid) {
    $userrole=DB::table('roles')->where('id', '=', $roleid)->get(); 
     return $userrole

 }

In Controller:
use App\User

public function __construct(User $user){
  $this->user_model =  $user;
}

public function index(){

    $userRole = $this->user_model->userrole1()
    return view('admin/index', ['userRole' => $userRole]);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.