I have a Laravel database migration that is configured in MySql and I am trying to figure out how to call database queries from the model so that they can be accessed by different classes.
This is what one of the functions inside my app/models file looks like:
public function insertUsers(string $userName, string $email)
{
return DB::table('users')->insertGetId(
array('username' => '$userName', 'useremail' => '$email')
);
}
I am getting a class DB not found error when trying to call this function with a controller and I can't figure out a solution, any help or steps in the right direction would be great. Thanks for your time, and I can provide more information if necessary.
Edit: I think I need to extend my class, can anyone verify if this is correct? This is what the class declaration looks like:
<?php
class GenericQueries{
I tried
class GenericQueries extends Migration{
but it still does not work. Any help is appreciated.
We can do this by making Helpers or simply it can be done in Model Under library folder make a file called Helper.php and inside it:
<?php
class Helper{
public static function GetResult($param1, $param2)
$data = DB::table('users')->insertGetId(
array('username' => '$param1', 'useremail' => '$param2')
);
return $data;
}
And call in controllers
Helper::GetResult($usernaem,$email);
Also in same way it can be done in model .But best practices and way to do this would be making class in library. As illustrated above.