Not delete record from database in cakephp - php

Not delete record from database in cakephp
my query for delete record.
public function del_mail($mailid)
{
$this->Mymail->deleteAll(array('mailid'=>$mailid));
$this->Session->setFlash(__('Delete Successful', true), 'default');
$this->redirect($this->referer());
}
Error.
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Mymail.id' in 'field list'
SQL Query: SELECT DISTINCT `Mymail`.`id` FROM `babysoft_storel`.`mymails` AS `Mymail` WHERE `mailid` = 1

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Mymail.id' in 'field list'
You have an error of mysql query and are you sure you have the 'Mymail.id' in your table field?
According to your comment below Please try this :
public function del_mail($mailid)
{
$this->Mymail->deleteAll(array('Model.mailid'=>$mailid)); //where Model is your Model you used to retrieving the mailid field.
$this->Session->setFlash(__('Delete Successful', true), 'default');
$this->redirect($this->referer());
}

Related

Laravel eloquent update, 500 Internal Server Error

I want to update my database with Laravel eloquent update, but response is always 500
This is my model
class Tunggakan extends Model
{
protected $table = 'kredit_tunggakan';
/**
* #var array
*/
}
This is the function
public function statusTunggakan(){
$status = Tunggakan::find(2);
$status -> id_status = 77;
$status -> save();
}
This is the route
Route::prefix('tunggakan')->group(function () {
Route::post('/statusTunggakan','TunggakanControl#statusTunggakan');
});
Exception:
[2022-03-24 11:20:14] production.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'kredit_tunggakan.updated_at' in 'field list' (SQL: update kredit_tunggakan set id_status = 77, kredit_tunggakan.updated_at = 2022-03-24 11:20:14 where id = 2) {"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'kredit_tunggakan.updated_at' in 'field list' (SQL: update kredit_tunggakan set id_status = 77, kredit_tunggakan.updated_at = 2022-03-24 11:20:14 where id = 2)
Thanks before, for helping..
Loking at the exception:
Column not found: 1054 Unknown column 'kredit_tunggakan.updated_at
Seems like you don't have updated_at column in your database table. There are two solutions for this:
I: Create/update your migration to include timestamp fields:
$table->timestamps();
II: Set timestamps to false when updating the record:
$status = Tunggakan::find(2);
$status->timestamps = false;
$status -> id_status = 77;
$status -> save();

Count in update query doesn't work in Laravel

public function increment($id)
{
$this->model->where("id",'=', $id)->update(['rating'=> DB::raw('count+1')]);
}
I am getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in
'field list' (SQL: update news set rating = count+1, updated_at
= 2019-04-13 08:12:51 where id = 5)
I also tried
->update(['rating'=>'count+1']);
You are not telling the query builder on which table you are performing the query, so DB::raw('count+1') makes no sense.
You can try to use the eloquent increment method like this:
$this->model->where("id", $id)->increment('rating');
Thanks #Tharaka removed the extra call to save().

Column not found error while trying to mass assign values inside controller

I'm getting this error when I'm trying to assign a value to all the columns in a table.
Controller
public function updateallcompany(Request $request, $id)
{
$active = $request->input('active');
AccessCode::where('company_id', $id)->update([$active, 'active']);
return view('pages.accesscode.showallaccesscodecompany')
->with('success', "AccessCodes Updated");
}
Error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field
list' (SQL: update access_codes set 0 = Yes, 1 = active, updated_at = 2019-04-11 11:10:03 where company_id = 2)
You must pass the $active value to the field active.
You can see Mass Updates example on laravel documentation:
https://laravel.com/docs/5.8/eloquent
Try:
AccessCode::where('company_id', $id)->update(['active' => $active]);

laravel 5.4 query exception unknow column error

i have this ruote.
Route::get('/artist/{id}/{slug}', 'HomeController#artist')->name('artist');
and this is the moded artist.
public function artist($id,$slug){
$artist = Artist::where('id', $id)->where('slug', $slug)->first();
$hits = Artist::where('id', $id)->where('slug', $slug)->increment('week_hits');
return view('front.artist', compact('artist'));
}
whati want is increment week_hits field in my artists table. whenever a specific artist page is visited by the user.
but iam getting this error.
QueryException in Connection.php line 647:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'week_hits' in 'field list' (SQL: update `artists` set `week_hits` = `week_hits` + 1, `updated_at` = 2017-02-26 16:20:04 where `id` = 1 and `slug` = xasan-aadan-samatar)

CakePHP: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Post' in 'where clause'

I have the following code to show a list of posts that also contains information from the author user and profile. The profile table has no direct link to posts and is linked via the user table.
public function index()
{
$posts = $this->Post->find('all',null,array('contain'=>array('User'=>'Profile')));
$this->set('posts',$this->paginate($posts));
}
However I am getting this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Post' in 'where clause'
Any ideas what's the problem here? Thanks
You're not supposed to find and then paginate; paginate itself calls the model's find method to get the rows for the current page. Change your code to this:
public function index()
{
$this->paginate = array(
'contain'=> array('User'=>'Profile')
);
$this->set('posts',$this->paginate());
}

Categories