Laravel 5 eloquent model auto add an underscore - php

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,

Related

LARAVEL 9 Target class does not exist

After creating a Model and a Controller using artisan and defining the action in the controller.
I created a route Route::get('/showStudents', 'StudentsController#index');
But the problem is the controller is not defined by laravel .
I checked the possibility of typos so many times , namespaces ,controller name ,model name
that path was so usefull , I asked a friend who said that channging the controller name after creating it using artisan makes that error , but thing is i recreated the controller name another time and i left the original name . But unfortunately that didn't help
thank in advance
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StudentsController extends Controller
{
//
}
models
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Students extends Model
{
use HasFactory;
public funtion index(){
echo 'hhh';
}
route
Route::get('/showStudents', 'StudentsController#index');
I tried this syntax in
[StudentsController::class, 'index']
instead of
'StudentsControlle#index '
and it worked.

How can I mock a different model to retrieve a polymorphic relationship?

I have 3 data models, one of which extends the other:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Opinion extends Model
{
public function reactions()
{
return $this->morphMany('App\Models\Reaction', 'reactable');
}
...
}
namespace App\Models\Activity;
use App\Models\Opinion;
class ActivityOpinion extends Opinion
{
...
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Reaction extends Model
{
public function reactable()
{
return $this->morphTo();
}
...
}
The App\Models\Opinion model has a polymorphic relationship with the App\Models\Reaction model. I can retrieve all of the App\Models\Opinion reactions no problem, so I know the relationship works great.
My question is, how can I retrieve the same set of reactions from the App\Models\Activity\ActivityOpinion model? Because right now, it is looking for App\Models\Activity\ActivityOpinion as the relationship but I need it to look for App\Models\Opinion. Is it possible to mock another model in a polymorphic relationship?
This is because in a Polymorphic Relationship in the stored data (if leaved as default) the relationship type gets the class namespace (sort of) to specify wich model needs to be returned. That's why when you try to access to your reactions() relationship from ActivityOpinion it will look up for the App\ActivityOpinion value in the reactable_type.
You can customize the morph class to search in the model addind this:
Opinion.php
protected $morphClass = 'reaction';
This should be enough, if not, add it also in the ActivityOpinion model.
Note
This could breake some things when trying to search results using Eloquent. Check this other answer in order to address this possible inconviniance.
Update
I've just found out that you could do all this even easier with MorphMap. From the docs:
Custom Polymorphic Types
By default, Laravel will use the fully qualified class name to store
the type of the related model. For instance, given the one-to-many
example above where a Comment may belong to a Post or a Video,
the default commentable_type would be either App\Post or
App\Video, respectively. However, you may wish to decouple your
database from your application's internal structure. In that case, you
may define a "morph map" to instruct Eloquent to use a custom name for
each model instead of the class name:
use Illuminate\Database\Eloquent\Relations\Relation;
Relation::morphMap([
'posts' => 'App\Post',
'videos' => 'App\Video',
]);
You may register the morphMap in the boot function of your
AppServiceProvider or create a separate service provider if you
wish.

SQLSTATE[42S02]: Base table or view not found - cant find the typo Laravel

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'

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.

Calling Correct Database Table with Laravel all()

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.

Categories