I´m trying to get the date from an user in my DDBB, them, I have 2 customer in a table.
The schema is called - customers
The table is called - names
And names has two columns, id and name, its very simple. I have added two persons in my table, Tony and Bob.
In laravel, in the app folder, I have created a new php file, and I put this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model{
}
And I have this code in my .env file:
DB_HOST=localhost
DB_DATABASE=customers
DB_USERNAME=root
DB_PASSWORD=root
Then, I just want get my first customer, Tony, and in routes.php I have write this:
Route::get('customer', function(){
$customer = App\Customer::find(1);
print_r($customer);
});
When I run the application, I get this message:
FatalErrorException in routes.php line 23:
Class 'App\Customer' not found
I would like to get something, but I don´t know what happen. Thanks.
$customer = new Customer;
$customer->name = $request->name;
$customer->mobile_number = $request->mobile_number;
$customer->mail = $request->mail;
$request->name.......this name and $request->Mobile_number,$request->mail is .... form field name.
It says - Class 'App\Customer' not found.
Seems your class Customer is not loaded. Make sure, your Customer model is in App folder. Also the file name must be Customer.php. Also the class name must start with C(Capital) - Customer. And also make sure, you have defined the namespace as follows in Customer model.
namespace App;
Example
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $table = 'customer';
}
And try like this and see ?
Route::get('customer', function(){
$customer = \App\Customer::find(1);
print_r($customer);
});
Related
I am new to the laravel framework. I tried to pass data from the database to my view but it gives me the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.aanvragens' doesn't exist (SQL: select * from aanvragens).
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Aanvragen;
use Carbon\Carbon;
class aanvragenController extends Controller
{
public function index() {
$aanvragen = Aanvragen::all();
return view('aanvragen.index', compact('aanvragen'));
}
}
Route
Route::get('/overzicht', 'aanvragenController#index')->name('aanvragen.index');
Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Aanvragen extends Model
{
}
I know my database connection is working as I can migrate my migrations (the default ones). What is strange to me is that I can't recall I typed aanvragens somewhere. Not in my controller, view, model etc. My database table name is aanvragen. Is there someone who can help me? Maybe I forgot to include something or made a typo..
This is the normal convention, singular model names, and plural table names.
You can override this in your model.
public $table = 'aanvragen'
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
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,
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.
I am just getting started with Laravel 5. I am trying to set up a basic page that retrieves all data from a database table. I have a table call people and I have a controller called ContactController.php. The controller has the following code:
<?php namespace App\Http\Controllers;
use App\Contact;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ContactController extends Controller {
public function index()
{
$people = Contact::all();
return $people;
}
}
When I access my page, I get the following error:
QueryException in /home/vagrant/sites/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php line 614:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.contacts' doesn't exist (SQL: select * from `contacts`)
Why is it trying to access homestead.contacts? My table is called people. I never created a table called contacts. Where is it drawing the table name from and how do I correct this?
Thanks.
Quote from the docs:
Note that we did not tell Eloquent which table to use for our User model. The lower-case, plural name of the class will be used as the table name unless another name is explicitly specified.
So if you define nothing else, Laravel will take the plural snake_case version of your class name:
Contact => contacts
FooBar => foo_bars
To fix your issue either change the model name accordingly. In this case Person will let Laravel search for the table people.
Or explicitly specify the table in your model:
class Contact extends Eloquent {
protected $table = 'people';
}
Do you have the right table name in your app/Contact.php model?
protected $table = 'people';
Or are you using the wrong model?
Add the following line to your Contact model (app/Contact.php).
protected $table = 'people';
This will work.