Answer the question
In order to leave comments, you need to log in
Laravel how to use DB class in model?
I created a model using php artisan make:model "Name" and created a function there that uses the DB class to work with the database, and I accessed the model of this function from the controller, but the error "Class 'App \ DB' not found" came out.
Model code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Hello extends Model
{
public function showName($id){
$users = DB::select('select name from users where id=?', [$id]);
return $users;
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App;
class HelloController extends Controller
{
public function show($id){
$usersObj = new App\Hello();
$users = $usersObj->showName($id);
return view('hello', compact('users'));
}
}
Answer the question
In order to leave comments, you need to log in
What the hell is this anyway? What the hell do you need a Hello model for?
Why can't you just get the user from his model?
public function show($id){
$userObj = User::find($id);
$userName = $userObj->name;
return view('hello', compact('userName'));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question