can't feed to database table via eloquent - php

i am new to laravel.I have a project table in my database and i created a eloquent model using php artisan make:model project;
it created the following codes in project.php page
namespace App;
use Illuminate\Database\Eloquent\Model;
class project extends Model
{
//
}
when i attempt to feed data in my projects table laravel is giving me the error like
Route::get('/', function ()
{
$project = new project;
$project->name = 'Leonardo Da Vinci';
$project->save();
return View::make('welcome');
});
class project not found

Note that your project model has a namespace.
Either you import your model class on top of your routes.php:
use App\project
or use the full qualified name directly in code:
$project = new App\project;
I recommend to use upper camel case syntax for your class name like the whole framework do App\Project

Related

FatalThrowableError Class 'App\Models\Patient' not found in laravel 5.4

I have created a model by using the command php artisan make:model Patient. The command creates a model named Patient in the app folder.
In Model Patient:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Patient extends Model
{
//
public function getPurchaseOrder(){
return "Hello World";
}
}
In Controller PatientController:
namespace App\Http\Controllers;
class PatientController extends Controller
{
protected $patientModel;
public function __construct(Request $request, PatientInterface $patient)
{
parent::__construct($request);
$this->patientModel = new \App\Patient();
}
public function test(){
echo $this->patientModel->getPurchaseOrder();
}
}
It's working fine. The problem is when I create a folder named Models inside the app folder and move Patient model then call the model function it gives an error:
FatalThrowableError Class 'App\Models\Patient' not found
Any help will be appreciated.
When you move a class to a different directory, for it to be loaded by composer with the PSR-4 standard, you must also update the class' namespace to match.
namespace App\Models;
In addition, when you run the make command, you can include a namespace in that to automatically put it in the directory with the correct namespace:
php artisan make:model Models\\Patient

Laravel 5 eloquent model auto add an underscore

I am using laravel 5 for my small project and it seems like i'm having a problem with my model.
I have created this as my Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class tOrders extends Model
{
//
}
and this code from my controller to retrieve the data :
$test = tOrders::where('f_bank','=','ICBC')->orderBy('f_bank','ASC')->limit(10)->get();
after running the application, an error message came out saying
Invalid object name 't_orders'
i wonder where t_orders came from since my model is tOrders
Your model name should start with an uppercase letter and your file name should be the same. And I suspect Laravel is picking your model name as t_orders because your model name is starting with a lowercase letter. However, in your case for example, your filename should be Torders.php and code should be:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Torders extends Model
{
//
}
Cheers,

Using Laravel 5 Model

I have created a model using
php artisan make:model Sessions
which creates a model named Sessions in the App\ folder. My Model (Sessions) and the controller(School) calling the model are below
Model (Sessions):
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Sessions extends Model {
//
protected $table = "sch_sessions";
protected $primaryKey = "session_id";
}
Controller (School)
<?php
namespace App\Http\Controllers;
use App;
class SchoolController extends Controller {
public function sessionManagement(){
$Sessions = Sessions::all();
}
}
I get the following error
FatalErrorException in SchoolController.php line 7: Class
'App\Http\Controllers\Session' not found
I've tried putting the model in a folder and "use"ing it but nada. I'm stumped as to why the error is referring to a controller in the first place. Can anyone see something I don't (can't) please?
add on the top
use App\Sessions;
or use it with prefix
\App\Sessions::all();
If user Ali Mohammed's answer doesn't clear it up for you, it looks to me like your error is happening elsewhere. App\Http\Controllers\Session note Session is not plural.
Are you attempting to use the Session class elsewhere in your controller? If so, make sure to prefix it so it's using the Session declared in the global namespace. You can do this with \Session.

Save data into database in Laravel 5

First
I wrote a migration script.
Second
I run the php artisan migrate to migrate the table into my database.
Database
Now, I have a subscribes table in my database.
It has 2 fields : id, and email.
Route
Route::post('/subscribe', array('as' =>'subscribe','uses'=>'AccountController#postSubscribe'));
Model
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class Subscribe extends Model {
protected $table = 'subscribes';
//Validation Rules and Validator Function
public static function validator($input){
$rules = array(
'email' =>'required|email'
);
return Validator::make($input,$rules);
}
}
Controller
<?php namespace App\Http\Controllers;
use Input, Validator, Auth, Redirect;
class AccountController extends Controller {
public function postSubscribe() {
$subscribe = new Subscribe; <-------- Line#46 (BUG HERE)
$subscribe->email = Input::get('email');
$subscribe->save();
dd("Hi");
return Redirect::to('/')
->with('success','You have been successfully subscribe to us.');
}
}
?>
Error
Questions
Why can't I do $subscribe = new Subscribe;?
What is the best practice to insert data into database using Laravel 5 ?
Update
Thanks to #Mark Baker.
It seems that I have an issue with my namespace.
This namspacing is a bit confusing to me right now.
Can someone please clarify or explain that a bit ?
Anything is appreciated.
Thanks in advance.
Here is a high level overview of how namespaces work in PHP to try and help you understand this and give you a solution to your problem.
<?php
// This is the namespace of this file, as Laravel 5 uses PSR-4 and the
// App namespace is mapped to the folder 'app' the folder structure is
// app/Http/Controllers
namespace App\Http\Controllers;
// Use statements. You can include classes you wish to use without having
// to reference them by namespace when you use them further on in this
// namespaces scope.
use App\Subscribe;
class MyController extends BaseController
{
public function postSubscribe()
{
// You can now use the Subscribe model without its namespace
// as you referenced it by its namespace in a use statement.
$subscribe = new Subscribe();
// If you want to use a class that is not referenced in a use
// statement then you must reference it by its full namespace.
$otherModel = new \App\Models\Other\Namespace\OtherModel();
// Note the prefixed \ to App. This denotes that PHP should get this
// class from the root namespace. If you leave this off, you will
// reference a namespace relative to the current namespace.
}
}
You can try this, Simply use it :
$subscribe = new App\Subscribe;
Use App\Subscribe;.
Then you can use $subscribe = new Subscribe; in your code.

Yii 2 Namespace missing?

I have a namespace missing problem in Yii 2. I installed the advanced application. I am referencing a backend model from my frontend controller. Below is a code snippet of my backend model, frontend controller and error message.
Error
Unable to find 'backend\models\PaymentsMethod\TermsAndConditions' in file: C:\inetpub\wwwroot\jobmanager/backend/models/PaymentsMethod/TermsAndConditions.php. Namespace missing?
Backend Model
namespace app\models\PaymentsMethod;
use Yii;
class TermsAndConditions extends \yii\db\ActiveRecord
{
Frontend Model
public function actionCreate()
{
$model = new estimate();
$tnc = new \backend\models\PaymentsMethod\TermsAndConditions();
I have resolved my problem. I was trying to access a backend model class from a frontend controller. I resolved this by moving the backend model class to the common folder and from there I can reference it from both the backend and frontend.
Thanks
In your frontend, first include the namespace and then instantiate:
use app\models\PaymentsMethod\TermsAndConditions;
$tnc = new TermsAndConditions();
OR
As alfallouji said you can directly use:
$tnc = new \app\models\PaymentsMethod\TermsAndConditions();
If you are accessing from frontend then use frontend instead of app
i.e
namespace frontend\models\PaymentsMethod;
and if you are accessing from backend then use as below
namespace backend\models\PaymentsMethod;
You defined the model using this namespace app\models\PaymentsMethod and then you are trying to instantiate \backend\models\PaymentsMethod\TermsAndConditions.
You should be doing that in your frontend model :
$tnc = new \app\models\PaymentsMethod\TermsAndConditions();
Namespace declaration statement has to be the very first statement in the script
123456789101112
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\users;
class UserController extends Controller{
public function actionIndex()
{
echo "working on .....";
replace "backend" for "app" only models search
ex: app\models\PaymentsMethod;

Categories