Laravel update some database row - laravel-5

I create a page that update some data in database :
public function postedit(Requests\editRequest $request)
{
$upgrade = DB::table('upgrade')->where('user_id', Auth::user()->id)->update($request->all());
}
when i fill form i get this error :
QueryException in Connection.php line 725:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field
list' (SQL: update `upgrade` set `_token` =
g6MudghCdVvtL0ir361h9ysx53gRnv227LKSSZIz, `tell` = 867136819, `mobile` =
316136135, `Address` = usa-ny, `zip_code` = 141515 where `user_id` =
19)
what's the problem?

You are including a csrf token in your form input hence the _token field is added to the request. You can take only some fields from the request like:
$input = Request::only('tell',
'mobile',
'Address',
'zip_code');
$upgrade = DB::table('upgrade')->where('user_id', Auth::user()->id)->update($input);

Related

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'order clause'

I'm trying to make pagination after sorting in my Laravel project , so I did this method, it sorts my data but when I want to see next paginate table it shows me this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'order clause'
SELECT * FROM `customers` ORDER BY `` DESC limit 3 OFFSET 3
My method
public function Sortfilter(Request $request)
{
$active = Customer::where('active','=','1')->count();
$inactive = Customer::where('active','=','0')->count();
$customer = Customer::query();
$customer = Customer::orderBy($request->filter,'desc')->paginate(3);
return view('customers.index', compact('customer','inactive','active'));
}
is there a method to save the $request->filter data when I click on next button
EDIT
when i sort my data the URL change like that : http://127.0.0.1:8000/sortCustomer?filter=phone&_token=9xw0q9MKa5ABZc4CwkLaPqf5ko4BhJ4ZaEk0VKYY
and when i click to the pagination button the URL be like that :
http://127.0.0.1:8000/sortCustomer?page=2
The problem is sometimes your $request->filter is empty, so adding a default value when it's empty fixes that.
public function Sortfilter(Request $request)
{
$active = Customer::where('active','=','1')->count();
$inactive = Customer::where('active','=','0')->count();
$customer = Customer::query();
$filter = $request->filter ?: 'created_at';
$customer = Customer::orderBy($filter,'desc')->paginate(3)>appends(['filter' => $request->filter]);
return view('customers.index', compact('customer','inactive','active'));
}
I think you are passing '' (null) in the first parameter of orderBy clause.
please confirm that the value in the $request->filter parameter not null and it should be a column name as in the customer table.
In Laravel there is the option to append query string values to pagination links.
Using this your code could look like this:
public function Sortfilter(Request $request)
{
$active = Customer::where('active','=','1')->count();
$inactive = Customer::where('active','=','0')->count();
$customer = Customer::query();
$customer = Customer::orderBy($request->filter,'desc')->paginate(3)->withQueryString();
return view('customers.index', compact('customer','inactive','active'));
}

Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from `item` where `barcode` = A002 and `id` <> 12)

I try to update my table Item but I use unique validation in my controller, I have set the primary key = 'item_id' in my model but I keep get this error said unknown column id even though I have set the primary key as item_id in my model, when I try to use validation :
// 'barcode' => 'unique:item' //
This error doesn't appear and the program works but in the unique of barcode it is checking itself for the unique column and it fails but seeing its own field. that's why i change my validation become this :
// 'barcode' => 'unique:item,barcode,'.$item_id, //
when I try to update I get this error :
Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from item where barcode = A002 and id <> 12)
ItemController :
public function update(Request $request,$item_id)
{
$messages = [
'unique' => 'Sorry this barcode already exist....',
];
$this->validate($request,[
'barcode' => 'unique:item,barcode,'.$item_id,
],$messages);
$data = $request->except(['_token']);
Item::where('item_id',$item_id)->update($data);
return redirect('/item')->with('sukses','item successfully updated');
}
this is my model
Item.php :
class Item extends Model
{
protected $table = 'item';
protected $primaryKey = 'item_id';
protected $fillable = ['barcode','item_name','category_id','satuan_id','price'];
public function category_r(){
return $this->belongsTo('App\Category','category_id','category_id');
}
public function satuan_r(){
return $this->belongsTo('App\Satuan','satuan_id','satuan_id');
}
}
When you are using custom primary key you should also add its name to the validation:
$this->validate($request,[
'barcode' => 'unique:item,barcode,'.$item_id.',item_id'
],$messages);
https://laravel.com/docs/5.2/validation#rule-unique

Column not found laravel 5.4

I get the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'books.id' in
'where clause' (SQL: select * from books where books.id =
98745632564 limit 1)
when I pass id value as id. I have column name bookID in my database but in the above error it is comparing books.id = 98745632564. I could not understand where book.id is coming from.
public function showBook($id){
$book = Book::findOrFail($id);
return $book;
}
The code works perfectly fine when I pass id value with the query as follows
public function showBook($id){
$book = Book::where('bookID', $id)->find();
return $book;
}
You should set:
protected $primaryKey = 'bookID';
in your Book model to make:
$book = Book::findOrFail($id);
version work.
Methods find or findOrFail are using primary key and this is by default set to id, so if you have any custom primary key, you should set it in your Eloquent model.

How to update field in table Laravel or create?

I try to update field rate in table by primary key user_id.
So, I need to increment of decrement rate field. If row is not exist I need to create it.
I tried:
$this->point = -1; // Or $this->point = 1;
Rating::updateOrCreate(["user_id", $id], ["rate" => $this->point]);
But it does not work:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `rating` where (`0` = user_id and `1` = 5) limit 1)
This is because your passing the column and the value as two separate values.
Change:
["user_id", $id]
to:
["user_id" => $id]
Hope this helps!
Laravel (eloquent) provides a fluent way to update related models.
Assuming the following model structure:
Rating.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function ratings()
{
return $this->hasMany('App\Rating');
}
You can easily update the relation via associate()
$user = User::findOrFail($userId);
$rating = Rating::findOrFail($ratingId);
$rating->user()->associate($user)->save();
Read more about Relationships in Eloquent

Update validation for unique attribute

I have this function update in my controller :
public function update(Request $request, $id){
$siswa = Siswa::findOrFail($id);
$input = $request->all();
$validator = Validator::make($input, [
'nisn'=>'required|string|size:4|unique:siswa,nisn'.$request->input('nisn'),
'nama_siswa'=>'required|string|max:30',
'tgl_lahir'=>'required|date',
'jns_klmin'=>'required|in:L,P',
]);
if ($validator->fails()) {
return redirect('siswa/'.$id.'/edit')->withInput()->withErrors($validator);
}
$siswa->update($request->all());
return redirect('siswa');
}
Where nisn is an unique attribute. Yet when I run it I always stumble in this screen written :
QueryException in Connection.php line 729:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nisn1007' in 'where clause' (SQL: select count(*) as aggregate from siswa where nisn1007 = 1007)
Any help appreciated. Thanks in advance
You forgot about , here:
'nisn'=>'required|string|size:4|unique:siswa,nisn'.$request->input('nisn'),
should be like this:
'nisn'=>'required|string|size:4|unique:siswa,nisn,'.$request->input('nisn'),
after: siswa,nisn

Resources