Manish Agarwal
Manish Agarwal

Reputation: 178

How to call model function into my controller in laravel

I have a model folder inside the App folder named Models, in this folder I have created a UserModel, and my controller is in App/Http/Controller folder name UserController. How can I call to my UserModel into the UserController?

I have tried below code.

My Controller UserController.php: It is in App/Http/Controller Folder.

    <?php 

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\User;
use App\Models;


class UsersController extends Controller {

    public function __construct(){
        $users = new UserModel();
    }

    public function index(){
        $user_id = $_GET['id'];
        $token = $_GET['token'];
        $userInfo = $this->users->getUserInformation($user_id); 
        return view('profile.users')->with('user_details', $userInfo);
    }

}

and my Model: UserModel.php it is in App/Models folder.

<?php 
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class UserModel extends Model
{
    public function getUserInformation($id){
        return "User id is : ".$id;
    }   
}
?>

Upvotes: 3

Views: 11624

Answers (5)

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9703

If You will do like this

<?php 
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class UserModel extends Model
{
    public function getUserInformation(){
        return "User id is : ".$this->id;
    }   
}
?>

In controller

<?php 

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\UserModel;
use App\Models;


class UsersController extends Controller {

    public function index(){
        $user_id = $_GET['id'];
        $token = $_GET['token'];
        $user = UserModel::find($user_id);
        $userInfo = $user->getUserInformation(); 
        return view('profile.users')->with('user_details', $userInfo);
    }

}

please try and let me know.

Upvotes: 0

NoOorZ24
NoOorZ24

Reputation: 3277

You should really read documentation and make your style more Laravel-like

Controller:

<?php 

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UsersController extends Controller {
    public function index()
    {
        // this is where you show list of users
        // I recommend you reading this: 
        //https://laravel.com/docs/5.8/controllers#resource-controllers
    }

    public function show(User $user)
    {
        return view('profile.users.show')->with('user', $user);
    }
}

Route:

Route::get('users/{user}', 'UsersController@show');

View:

<span>User id is : {{ $user->id }}</span>

Upvotes: 0

Ray A
Ray A

Reputation: 1351

You did it right, just small tweak and you are good to go: the $users variable in your controller must be declared correctly to be accessible to all method, so update your controller to this:

<?php 

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\User;
use App\Models\UserModel;


class UsersController extends Controller {
    protected $users;
    public function __construct(){
        $this->users = new UserModel();
    }

    public function index(){
        $user_id = $_GET['id'];
        $token = $_GET['token'];
        $userInfo = $this->users->getUserInformation($user_id); 
        return view('profile.users')->with('user_details', $userInfo);
    }

}

Upvotes: 5

apokryfos
apokryfos

Reputation: 40730

I think the best way is to take advantage of Laravel's dependency injection and inject the dependency directly on the controller method:

class UsersController extends Controller {


    public function index(UserModel $users, Request $request){
        //Using the request object is recommended
        $user_id = $request->input('id');
        $token = $request->input('token');
        $userInfo = $users->getUserInformation($user_id); 
        return view('profile.users')->with('user_details', $userInfo);
    }

}

Upvotes: 1

Afraz Ahmad
Afraz Ahmad

Reputation: 5416

since $this->users is returning a collection so you will have to loop over it and then call your model function in it.

foreach($this->users as $user){
     $userInfo = $user->getUserInformation($user_id);
}

///

Upvotes: 1

Related Questions