Count in update query doesn't work in Laravel - php

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().

Related

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]);

SQL column not found although it exists

I'm using the Laravel framework to create a prototype and need to output an individual article from a database based on it's ID. Seems straightforward but I keep getting the following error when I query the database:
QueryException in Connection.php line 647:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'articles.id' in 'where clause' (SQL: select * from `articles` where `articles`.`id` = 1 limit 1)
I'm trying to retrieve a specific article based on the database column article_id via the articles table in my Article model. I am using a resource route thus the method name 'show' - so I can access via article/{$article_id} in the URL.
Here is my controller:
public function show($article_id) {
$article = Article::find($article_id);
$article->toArray();
return view('articles.view', array('article' => $article));
}
There is no articles.id. It should be articles_id. Am I missing something obvious here?
Thanks in advance, any pointers much appreciated.
The find command looks for whatever is set as the primary key, which by default is id. You can change the primary key in your model:
class Article {
protected $table = 'articles';
protected $primaryKey = 'articles_id';
}
find() searched by primary key, so change the query to:
$article = Article::where('article_id', $article_id)->first();

Laravel 5.3 update request with unique rule with exception

I have request for updating page. When adding new page request is like:
'title'=>'required|max:70|unique:pages',
but when I'm updating page title must be unique but need to check all other titles except one already entered.
I searched Google for all possible solutions but nothing works.
I tried:
'title'=>"required|max:70|unique:pages, title,{$this->id}",
'title'=>'required|max:70|unique:pages, title,'.$this->id,
'title'=>'required|max:70|unique:pages, title,'.$this->input('id'),
rule is inside rules method
public function rules()
{
return [
'title'=>'required|max:70|unique:pages, title,'.$this->id,
...
]
I'm getting this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ' title' in 'where clause' (SQL: select count(*) as aggregate from `pages` where ` title` = test and `id` <> )
In my mysql I have id (lowercase) column that is primary key and title column (also lowercase).
You have a bug here:
public function rules()
{ This space is causing trouble
|
return [
'title'=>'required|max:70|unique:pages, title,'.$this->id,
...
]
Should be
public function rules()
{ Space removed
|
return [
'title'=>'required|max:70|unique:pages,title,'.$this->id,
...
]
SQLSTATE[42S22]: Column not found: 1054 Unknown column ' title'
It seems that it's searching for a column named " title", it may be stupid but double check that the column name is sent without a space at the beggining.

Need help building query laravel 5.2

I'm trying to get rings from the database. but only filter is the homepage is 1 or 0.
I only need the rows where homepage is 1.
This is what i tried
$ringen = RingKoppelCategory::with('ringen')->get()->where('homepage', '=' , 1);
returns null
And when i put the ->get() at the end of the query builder it checks the ringkoppelcategory table for a homepage which is not what i want it to do.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'homepage' in 'where clause' (SQL: select * from `ringkoppelcategory` where `homepage` = 1
I need to get the rings relationship from the ringkoppelcategory, but only the rings where the homepage is 1.
You need to use a function to pass along a where within your with.
$ringen = RingKoppelCategory::with(['ringen' => function ($query) {
$query->where('homepage', '=' , 1);
}])->get();
More information is found in the documentation
I think you need to use the following query
$ringen = RingKoppelCategory::whereHas('ringen', function ($query) {
$query->where('homepage', '=', 1);
})->get();
Check Querying Relationship Existence section at documentation

Not delete record from database in cakephp

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());
}

Categories