I'm having a trouble using a model in a controller in Laravel 5.0. I created the model in a folder model under App. The code of the model id described as:
<?php namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Observacion extends Model{
protected $table = 'obs_usuarios';
protected $fillable = ['observaciones', 'usuario_id','autor_id','tipo'];
}
Part of the code of the controller where I pretend to use it, is:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\models\Contrato;
use app\models\Observacion;
use App\models\Perfil;
use Illuminate\Http\Request;
use App\models\Configuracion;
use App\models\Usuario;
use App\models\Categ_profesores;
use Carbon\Carbon;
use Illuminate\Support\Facades\Input;
use Auth;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class SecretariaController extends Controller {
public function observacion($msg,$user_id,$autor_id,$type){
$observacion = new Observacion();
$observacion->observaciones=$msg;
$observacion->usuario_id=$user_id;
$observacion->autor_id=$autor_id;
$observacion->tipo=$type;
$observacion->save();
}
I even made dump-autoload but I get this error:
FatalErrorException in SecretariaController.php line 155:
Class 'app\models\Observacion' not found
The line 155 is where I do: $observacion = new Observacion();
So, I don't know what else to check.
Change:
use app\models\Observacion;
to:
use App\models\Observacion;
The first letter is a capital. In PHP names are case-sensitive.
Related
For my Laravel assignment I am using models to create add products. However, the postman gives me the error of class not found. I am a bit stumped on that to be honest. Here is the AProductController I use:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\AProduct;
class AProductController extends Controller
{
//
function addAProduct(Request $req)
{
$aProduct=new AProduct;
$aProduct->name=$req->input('name');
$aProduct->price=$req->input('price');
$aProduct->color=$req->input('color');
$aProduct->file_path=$req->file('file')->store('aproducts');
$aProduct->save();
return $aProduct;
}
}
AProduct Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class AProduct extends Model
{
use HasFactory;
}
I am getting this error in postman
Error: Class 'App\Models\AProduct' not found in file D:\Savindi\University\4th year\Project\Our
Project\Project\ecomm-backend\app\Http\Controllers\AProductController.php on line 12
Thank you in advance for your help!
On AProduct Model, change your namespace :
namespace App;
to
namespace App\Models;
Here is my code in controller, am trying to sort columns in view file by just clicking on corresponding column name
<?php
namespace App\Http\Controllers;
use App\Http\Controllers;
use Illuminate\Http\Request;
use App\Service;
use DB;
use JsValidator;
use Validator;
use Session;
use Redirect;
use Carbon\Carbon;
use App\Libraries\GlobalHelpers;
use App\Libraries\ImageHelpers;
use Auth;
use Response;
class ServicesController extends Controller
{
protected $serviceValidationRules = [
'service_name' => 'required'
];
public function index()
{
$services = Service::all()->sortable()->get();
return view('services.index')->with('services', $services);
}
?>
Am getting following error
Method Illuminate\Database\Eloquent\Collection::sortable does not exist.
Kindly help me to resolve
Hello i am trying to use a trait from controller in my register controller but it can't seem to find it
the error message:
Trait 'MailVerification' not found
The class in which i want to use the trait
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
use MailVerification;
Here i call the function
protected function create(array $data)
{
$mail = $data['email'];
$this->sendVerification($mail);
Here is the trait in the class i am trying to import it from
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\TestMail;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Session;
trait MailVerification
{
public function sendVerification($mail)
{
$verification_code = str_random(30);
Mail::send('mail.verify', ['verification_code' => $verification_code, 'mail' => $mail], function ($message) use ($mail)
{
$message->from('test#laravel.com');
$message->to($mail);
});
Session::flash('message', "Please check you're email to verify your account");
return redirect('/');
}
}
class MailController extends Controller
{
I have the trait outside of my class i don't know if this is correct but it was giving me an error while it was inside the class.
The namespace of your controller RegisterController and your trait MailVerification is different...
So, you'll have to add this line to your RegisterController
use App\Http\Controllers\MailVerification;
Also, I suggest you to put all your traits inside App\Traits folder instead of your controller. Try following a simpler way if possible :)
Edit --
This is how you register controller should look like
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Controllers\MailVerification;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers, MailVerification;
//Your code here....
}
I have a controller in Laravel 5.0 like this-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CustomersController extends CustomerLayoutController
{
public function getDashboard()
{
return $this->view('layouts.customer.dashboard', []);
}
public function getTest()
{
return $this->view('layouts.admin.webinar', ['qustions' => DB::table('qustions')->get()]);
}
}
So, I want to pass DB::table('qustions')->get() as a parameter to my views, but I m getting error.
What I am doing wrong?
You're getting an error because Laravel is searching the class is the wrong namespace (it's "appending" the class to the current class's namespace, if you note).
You either import the DB class with the use keywords:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
or let it know that DB resides in the "global" application namespace, so call it with a backslash:
return $this->view('layouts.admin.webinar', ['questions' => \DB::table('qustions')->get()]);
So, in L5 I created folder like app/Models/Blog where is file Posts.php which looks like:
<?php namespace App\Models\Blog;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model {
protected $table = 'posts';
}
After it I executed composer dump and then in my controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Models\Blog\Posts as Posts;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
It throws me an error:
FatalErrorException in BlogController.php line 14: Class 'Models\Blog\Posts' not found
Try changing this:
use Models\Blog\Posts as Posts;
To this:
use App\Models\Blog\Posts;
In Laravel 5.2 it's just:
use App\Blog;
or
use App\Blog\Posts;
Change the following
class Posts extends Model {
to
class Posts extends \Eloquent {
You need to check two points :
the namespace have to be in first
the using must be use App\Models\Blog in your case
Like this :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Blog;
class BlogController extends Controller {
public function index()
{
$post = Posts::all()->toArray();
dd($post);
}
}
(tested with Laravel 5.4)