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
Related
I have a 'user' table in which there is a column 'team_id'. I also have a 'daily_report' table that includes a 'user_id' column. I am trying to filter the data in the 'daily_report' table by 'team_id'. I am using Laravel 9.
My current code in the index controller is as follows:
public function index()
{
$daily_reports = DailyReport::with('user.team')->with('updated_by_user_id')
->when(request()->q, function ($daily_reports) {
$daily_reports = $daily_reports->where('description', 'like', '%' . request()->q . '%');
})->when(request()->status, function ($daily_reports) {
$daily_reports = $daily_reports->where('status', request()->status);
})->when(request()->user_id, function ($daily_reports) {
$daily_reports = $daily_reports->where('user_id', request()->user_id);
})->when(request()->team_id, function ($daily_reports) {
$daily_reports = $daily_reports->where('user.team_id', request()->team_id);
})->latest()->paginate(5);
$users = User::all();
$teams = Team::all();
return Inertia::render('Apps/DailyReports/Index', [
'daily_reports' => $daily_reports,
'users' => $users,
'teams' => $teams,
]);
}
->when(request()->team_id, function ($daily_reports) {
$daily_reports = $daily_reports->where('user.team_id', request()->team_id);
})->latest()->paginate(5);
However, I am encountering the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user.team_id' in 'where clause'
select count(*) as aggregate from `daily_reports` where `user`.`team_id` = 2
Could you please help me resolve this issue?
user is relationship, so have to use whereHas.
->when(request()->team_id, function ($daily_reports) {
$daily_reports = $daily_reports->whereHas('user', function ($query) {
$query->where('team_id', request()->team_id);
});
})->latest()->paginate(5);
Fixed with:
$daily_reports->whereHas('user', fn ($q) => $q->where('team_id', request()->team_id));
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'));
}
I have User Model with Roles relationships like so :
public function users(){
return $this->belongsToMany(User::class)->withTimestamps();
}
And Role Model with user relationships :
public function roles(){
return $this->belongsToMany(Role::class)->withTimestamps();
}
And of course Pivot Table "role_user" contains id, user_id, role_id and timestamps
I try to get users ordered By roles name like so :
$users = App\Models\User::with('roles')->orderBy('roles.name', 'desc')->get();
but i have this error :
Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.name' in 'order clause' (SQL: select * from `users` where `users`.`deleted_at` is null order by `roles`.`name` desc)
http://localhost:8000/users
Please, some helps .
Try Like this:
$users = App\Models\User::with('roles')->get();
// order of the roles
$array = ['User','Admin','Doctor', 'Manager'];
$sorted = $users->sortBy(function($model) use ($array) {
return array_search($model['roles'], $array);
});
Hope it will help you!
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
I know that I don't have the table in phpmyadmin
but the problem is I don't know how to do it the right way
The code
public function DriverRefer($id)
{
$driver = Driver::find($id);
$query = ReferralDiscount::where([['referral_driver_id', '=', $id], ['referral_sender_id', '!=', 0]])->latest();
$referral_details = $query->paginate(25);
return view('merchant.driver.driver_refer', compact('referral_details', 'driver'));
}
Illuminate \ Database \ QueryException (42S22)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'referral_driver_id' in 'where clause' (SQL: select count(*) as aggregate from referral_discounts where (referral_driver_id = 4130 and referral_sender_id != 0))
Previous exceptions
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'referral_driver_id' in 'where clause' (42S22)
I added a model inside App/Models named DriverReferralDiscount:
<?php
class DriverReferralDiscount extends Model
{
protected $guarded = [];
public function Driver()
{
return $this->belongsTo(Driver::class, 'referral_driver_id');
}
public function SenderDriver()
{
return $this->belongsTo(Driver::class, 'referral_sender_id');
}
public function getSenderDetails($driver_id)
{
$refer = ReferralDiscount::where([['referral_driver_id', '=', $driver_id], ['referral_sender_id', '!=', 0]])->get();
return $refer;
}
public function getSenderCount($driver_id)
{
$refer = ReferralDiscount::where([['referral_driver_id', '=', $driver_id], ['referral_sender_id', '!=', 0]])->count();
return $refer;
}}
And changed the code to
public function DriverRefer($id){
$driver = Driver::find($id);
$query = DriverReferralDiscount::where([['referral_driver_id', '=', $id], ['referral_sender_id', '!=', 0]])->latest();
$referral_details = $query->paginate(25);
return view('merchant.driver.driver_refer', compact('referral_details', 'driver'));}
Then i added a table in phpmyadmin named driver_referral_discounts with columns:
id
referral_driver_id
referral_sender_id
referral_offer
referral_offer_value
referral_available
created_at
updated_at
Really sorry if it's not clear but i tried as best as i could in case this would help anyone else.