Model not linking to table in laravel php - php

I have a table named tasks and a model named Task. In my controller method when i run this piece of code
<?php
namespace App\Http\Controllers;
use DB;
//use app\Task;
use DateTime;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class Task_Controller extends Controller
{
//
public function decide()
{
if ($input=="show all task")
{
//$rows=DB::table('task')->get();
$rows=\App\Task::all();
foreach($rows as $values)
{
foreach($values as $key=>$val)
echo "$key : $val <br>";
echo "<br><br>";
}
}
}
It gives me following error:
SQLSTATE[HY000]: General error: 1 no such table: tasks (SQL: select * from "tasks")
That is the model should have linked to the task table instead it is linked to the tasks table Why??.
How to make model gets linked to a specific table.
My model class code goes like this
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
//
}

You should name your table tasks, this is the best option.
Another way to fix this is to use $table property:
protected $table = 'task';
Note that we did not tell Eloquent which table to use for our Flight model. By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Flight model stores records in the flights table. You may specify a custom table by defining a table property on your model
https://laravel.com/docs/5.4/eloquent#defining-models

By default, the table name is the plural of the class name. If you want it to point to a different table, then in your model, add
protected $table = 'task';

By default laravel pluralizes the model names to be be the table names unless stated otherwise in the model by.
protected $table = 'task';

Related

How to pass type and id to laravel polymorphic relation

I have a navigation model that can have many items associated with it:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use JetBrains\PhpStorm\ArrayShape;
use Laravel\Scout\Searchable;
class Navigation extends Model
{
use HasFactory;
use Searchable;
protected $guarded = [];
public function navigation_items(): HasMany
{
return $this->hasMany(NavigationItem::class);
}
}
The navigation item model looks like this
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class NavigationItem extends Model
{
use HasFactory;
protected $guarded = [];
public function navigation(): BelongsTo
{
return $this->belongsTo(Navigation::class);
}
public function navigatable(): MorphTo
{
return $this->morphTo();
}
}
Now an item can either be of type Page or Blog, in this case the Page model looks like this:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use JetBrains\PhpStorm\ArrayShape;
use Laravel\Scout\Searchable;
class Page extends Model
{
protected $guarded = [];
public function navigatable(): MorphOne
{
return $this->morphOne(NavigationItem::class, 'navigatable');
}
}
When I try to save a navigation model and associate it with a item, the following error appears:
SQLSTATE[HY000]: General error: 1364 Field 'navigatable_type' doesn't have a default value
I save the model like this:
foreach ($this->selected as $id) {
$this->navigation->navigation_items()->create([
'navigation_id' => $this->navigation->id,
]);
Where $this->selected is the navigation id, it should automatically get the correct navigatable_type and navigatable_id, but this doesn't seem to be working.
passing in the type and id manually works, but this kinda defeats the point of a polymorphic relationship.
any ideas?
On NavigationItem model, since you defined polymorphic relation as 'navigatable' it is expected that NavigationItem model's table contains navigatable_type and navigatable_id. First please ensure this checks out.
Creating records through relation's base function is not a valid method. It is not clear what you are trying to achieve there but when you want to set relation there is two standard way of achieving it:
1- Associate
When a relation is defined as belongsTo, you may use associate() function. Like so:
$account = Account::find(10);
$user->account()->associate($account);
2- Attach
Attach is used when relation is defined belongsToMany (pivot). It allows you to attach multiple records to a model instance/record.
$user = User::find(1);
$user->roles()->attach($roleId);
So if you want to set a 'navigatable' to a Navigation instance, you may:
$somePageInstance=Page::find(55);
$nagivation->navigatable()->associate($somePageInstance)
$nagivation->save();//remember to save, otherwise it won't be

How can I change database name conventions in Laravel?

I want to change the default database naming conventions in my Laravel app. By default, Laravel uses snake case for database table and column names. But I want to use Pascal Case for table names and i want to use camel Case for fields.
So a table name of Users instead of users, and field names createdAt, updatedAt, and deletedAt instead of created_at, updated_at, and deleted_at.
I know I can change these on a per-model basis using the $table property but I'd like to change the default without having to modify each model.
Are there any settings like Symfony's NamingStrategy in Laravel?
If you look at the code for Illuminate\Database\Eloquent\Model::getTable() it's pretty straightforward:
public function getTable()
{
return $this->table ?? Str::snake(Str::pluralStudly(class_basename($this)));
}
Same for Illuminate\Database\Eloquent\Concerns\HasTimestamps::getCreatedAtColumn():
public function getCreatedAtColumn()
{
return static::CREATED_AT;
}
So create your own class that extends Model and override that behaviour:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Support\Str;
class Model extends BaseModel
{
const CREATED_AT = 'createdAt';
const UPDATED_AT = 'updatedAt';
const DELETED_AT = 'deletedAt';
public function getTable()
{
return $this->table ?? Str::pluralStudly(class_basename($this));
}
}
Now, just have your models extend this class.
You can use stubs for that. Also if you named your table in Pascal Case just use $table property in model which need to be sync with that table.
You can create a new model that you extend your models with.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class ModelWithPascalCase extends Model
{
const DELETED_AT = 'deletedAt';
const CREATED_AT = 'createdAt';
const UPDATED_AT = 'updatedAt';
public function getTable()
{
return $this->table ?? Str::pluralStudly(class_basename($this));
}
}
If you want to make Laravel generate your models extending this, you can do it by editing stubs.
Run
artisan stub:publish
then edit stubs/model.stub by replacing Model with your ModelWithPascalCase.
After that, when you run
artisan make:model User
you get your User model extended by ModelWithPascalCase.

Specify DB table in laravel query

Im new to laravel, i am trying to query a specific table in my DB. I only have 1 data table and the standard user auth tables. I am getting a error: BadMethodCallException
Call to undefined method App\Figures::table().
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Figures extends Model
{
}
controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Figures;
class figuresController extends Controller
public function figurespag2() {
$dummyDetails = Figures::table('figures')->where('name', 'batman');
return view ( 'pagination2.index' )->withUsers($dummyDetails);
}
route
Route::get ( '/pagination2', 'figuresController#figurespag2' );
I know it's going to be something obvious, but I am new to this.
this is wrong
$dummyDetails = Figures::table('figures')->where('name', 'batman');
Method 1---------- laravel eloquent
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Figures extends Model
{
protected $table = 'figures';
}
Controller
$dummyDetails = Figures::where('name', 'batman')->get();
and
Method 2 ---------- laravel Query Builder
$dummyDetails = \DB::table('figures')->where('name', 'batman')->get();
Use this you not need to define table name
public function figurespag2() {
$dummyDetails = Figures::where('name', 'batman')->get();
return view ( 'pagination2.index' )->withUsers($dummyDetails);
}
First you may need to know laravel model rules.
If you create a table name like "figures" (plural) you need to create its model by Figure (singular).
if you create a table other then this rule then you have to mentioned table name in model like this.
protected $table = "table_name";
you can access table with where condition in controller like this.
public function figurespag2() {
$dummyDetails = Figure::where('name', 'batman')->get();
return view ( 'pagination2.index' )->withUsers($dummyDetails);
}
Hope this may help you.

Base table or view not found in laravel 5.3

I have a model
Education.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Education extends Model
{
public $timestamps = FALSE;
public function Member(){
return $this->belongsTo('App\Member');
}
}
In database i have a table named educations
In controller when I'm trying to access the data of the educations table through App\Education model I'm getting this error
QueryException in Connection.php line 770: SQLSTATE[42S02]: Base table
or view not found: 1146 Table 'dsse.education' doesn't exist (SQL:
select * from education)
Why laravel is searching for education table in the database where it should search for educations table. What is the problem?
here is the controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Member as Member;
use App\Education as Education;
class memberController extends Controller
{
public function addMember(){
$education = Education::all();
var_dump($education);
}
}
Add this to your model. You may set any custom table name as follow
protected $table = 'educations';
I had the same problem before,
and yes it is a solution to add
protected $table = 'educations';
but the problem is because laravel uses a class called
Illuminate\Support\Pluralizer
inside
Illuminate\Support\Str
and this class is not just adding "s" at the end of words or ies...
it is real pluralizer and education is an uncountable word, so is information...
so you need to add $table to your model.

How to use more then one table in laravel-5 model

I have an activity menu that have three sub-menu. These sub-menu are common to activity but have different table. So I want to use these three table under one model class, how can I do save operation individual to each other?
My code is as follows:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use DB;
class Activity extends Model {
protected $table1 = 'task_management';
protected $table2 = 'call_management';
protected $table3 = 'event_management';
}
use DB;
I have preferred using the DB query whenever i had to work with more than one table, below is a sample query.
$data = DB::table('base table name')
->join('join table name', 'join table name id', '=', 'master table name id')
->select('column 1', 'column 2', 'column 3')
->where('column1', $val);
You can create another class that is built on top of your models or even just uses plain SQL queries as #Peeje mentioned. Model is meant to work with single table in Laravel - I don't think there is an easy way to make it work with different tables.
I would recommend you to follow the Eloquent way,
You should create 3 models and do relationship
It would be simple like
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CallManagement extends Model
{
/**
* Get the task record associated with the user.
*/
public function task()
{
return $this->hasOne('App\TaskManagement');
}
}
Which allows you to retrive like $task = User::find(1)->TaskManagement;
Note : You should create and customize your model according to your need.

Categories