Im getting an error in Laravel using Eloquent which is:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'task_manager.rel_developers_projects' doesn't exist (SQL: insert into
`rel_developers_projects` (`project_id`, `developer_id`, `updated_at`, `created_at`)
values (?, ?, ?, ?)) (Bindings: array ( 0 => '1', 1 => '1', 2 => '2013-07-31 08:23:35', 3
=> '2013-07-31 08:23:35', ))
However, the Model is called RelDevelopersProject and the table is called RelDevelopersProjects.
Does Eloquent try to convert CamelCased names to underscores?
Do not use camel case for table names.
If you however need to, try this:
class RelDevelopersProject extends Eloquent {
protected $table = 'RelDevelopersProjects';
}
See eloquent in the Laravel Docs.
Yes, it does.
Just define the table name as a property in Model
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'RelDevelopersProject';
Related
in my posts table have extra_category column with saved mutiple value(json) same ["12","15","23"] now I want use this value in DB query for get category name for each value .
my Controller:
$excatname = DB::table('categories')
->where('id', '=', $post->extra_category)->get();
view()->share('excatname', $excatname);
and my view:
#foreach($excatname as $excatnamez)
{{ $excatnamez->name }}
#endforeach
but get error:
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens (SQL: select * from `categories` where `id` = 12)
Note: when extra_categoy have single value for ex : 15 worked as well.
cast is defined on the model
protected $casts = ['extra_category' => 'array',]
I have 2 database tables in MySQL below.
Table - 1
CREATE TABLE `tblaccount` (
`account_id` mediumint(8) UNSIGNED NOT NULL,
`account_number` varchar(100)
)
ALTER TABLE `tblaccount`
ADD PRIMARY KEY (`account_id`);
Table - 2
CREATE TABLE `tblcollectoractions` (
`collector_action_id` mediumint(8) UNSIGNED NOT NULL,
`account_id` mediumint(8) UNSIGNED DEFAULT NULL,
`pay_date` date DEFAULT NULL
);
ALTER TABLE `tblcollectoractions`
ADD PRIMARY KEY (`collector_action_id`),
ADD KEY `tblcollectoractions_account_id_foreign` (`account_id`);
I have a query below. It joins records in both tables on the basis of account_id. It also filters those accounts in tblcollectoractions table where pay_date lies between start and end date.
Here is my Laravel Eloquent Query. AccountModel is related to tblaccount and ActionModel is related to tblcollectoractions.
$query = (new AccountModel())->newQuery();
$data->whereIn("account_id", function($query) use($inputs) {
$query->select('account_id')->from(with(new ActionModel)->getTable())
->whereBetween('pay_date', [$inputs["from_pay_date"], $inputs["to_pay_date"]]);
});
But, this shows me all the records from table tblcollectoractions. I meant, it does not filter on the basis of start and end date.
Am I missing anything?
This is the most Eloquent way to do this, checking if the $inputs variable is set
$data = AccountModel::query()
->with([
'actions' => function($query) use ($inputs) {
if ($inputs['from_pay_date']) {
$query->whereBetween('pay_date', [
$inputs['from_pay_date'],
$inputs['to_pay_date']
]);
}
}
])
->has('actions')
->get();
the models should look something like this:
AccountModel.php
class AccountModel extends Model
{
protected $guarded = ['id'];
public function actions()
{
return $this->hasMany(ActionModel::class, 'account_id', 'account_id');
}
}
I've set a custom primary key in my Task.php model.
class Task extends Model
{
//
protected $primaryKey = 'taskn';
public $incrementing = false;
}
I've also set taskn as my primary key in the migration:
$table->string('taskn');
$table->primary('taskn');
But I still get the error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `Task` where `id` = 1 limit 1)
For some reason Laravel still tries to query id.
I am trying to retrieve the data with the following call:
$tasks = DB::table('tasks')->find($taskn);
What is wrong here?
I'm doing $tasks = DB::table('tasks')->find($taskn);
Here's your problem.
DB:: calls don't use Eloquent - you're completely bypassing it. If you do Task::find($taskn) it'll work, but DB:: calls have no idea about your $primaryKey settings.
I have a table with primary key group_id.
I have this in the model: protected $primaryKey = 'group_id';
In my update method I have:
'group_name' => 'required|string|unique:groups,group_name,' .$id,
The query executed is looking column id in the table instead of group_id.
SQLSTATE[42S22]: Column not found: 1054 Champ 'id' inconnu dans where clause (SQL: select count(*) as aggregate from groups where group_name = Clarens Community Forum and id <> 1)
Why is that?
Found the solution with the help in laravel.io chat room.
The validation should be like this:
'group_name' => 'required|string|unique:groups,group_name,' . $group->group_id .',group_id',
In Eloquent, I know it's possible to define the table on runtime with $model->setTable().
I tried adding a new item in the constructor like:
$myModel = new \MyModel(array(), 'my_primary_key');
$myModel->setTable('mytable');
with the constructor being:
class MyModel extends Eloquent {
public function __construct($attributes = array(), $primaryKey = 'id') {
parent::__construct($attributes); // Eloquent
$this->primaryKey = $primaryKey;
}
}
That makes my $primaryKey = 'id' like the default.
- This one doesn't work cause it doesn't change the primaryKey to whatever I define in my call the constructor, it just gets the default ('id'). If I don't set a default I get the following error:
Missing argument 3 for MyModel::__construct(), called in
/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 517 and defined
I also tried:
$myModel->setAttribute('primaryKey', 'my_primary_key');
But this one doesn't set the attribute
But nothing works. The default for Laravel is having 'id' as the primaryKey, but in my tables I have things like 'car_id' for table Cars, and so on, so I get errors like:
Column not found: 1054 Unknown column 'id' in 'where clause'
(SQL: select * from `cars` where `id` = 1 limit 1)
I tried to look into the code: https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L1415, but it doesn't have a method to set it on runtime.