SQLSTATE 42S22 Column not found - laravel-5

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.

Related

I'm getting a query error when adding paginate to my livewire component

My component lists user teams within a radius of an (friendly) event. I added the ability to search by team name. This all works fine until I try to add paginate.
This is the error I get after adding ->paginate($this->perPage)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id' in 'where clause'
(SQL: select count(*) as aggregate from `teams` where `users`.`id` = `teams`.`user_id`
and `division` = 12U and `name` like %%)
Her is my Livewire component:
class ListAreaTeams extends Component
{
use WithPagination;
public $friendly;
public $searchTeam = '';
public $perPage = 5;
public function render()
{
// Get friendly
$friendly = $this->friendly;
$users = User::query()->whereHas('teams', function($q) use ($friendly){
$q->where('division', $friendly->division)
->searchTeam($this->searchTeam)
->paginate($this->perPage);
})
->where('id', '!=', $friendly->user_id)
->distance($friendly->latitude, $friendly->longitude);
return view('livewire.list-area-teams', [
'users'=> $users
]);
}
}
#lagbox thanks for the hint. I was able to fix this by using a foreach on the users->teams. Not very pretty but it does the job.
class ListAreaTeams extends Component
{
use WithPagination;
public $friendly;
public $searchTeam = '';
public $perPage = 10;
public function render()
{
$friendly = $this->friendly;
$users = User::with('teams')
->where('onboard_completed', 1)
->where('id', '!=', $friendly->user_id)
->distance($friendly->latitude, $friendly->longitude);
$teams = [];
foreach($users as $user) {
foreach($user->teams as $team) {
$teams[] = $team;
}
}
$teams = $team
->where('division', $friendly->division)
->searchTeam($this->searchTeam)
->paginate($this->perPage);
return view('livewire.list-area-teams', [
'teams'=> $teams
]);
}
}

How to filter by date in Eloquent relationship

I've two tables first_levels and second_levels the first_levels have a relationship with themselves as parent and child and in the second table, I store the transactions.
my Model:
public function children()
{
return $this->hasMany(first_level::class, 'parent_id', 'Nominal')
->with(['children', 'transactions'])
->withsum('transactions', 'debit')
->withsum('transactions', 'credit');
}
public function transactions()
{
return $this->hasMany(second_level::class, 'first_level_id', 'id');
}
my controller:
function getGeneralLedger(Request $request)
{
$items = first_level::with('children')
->where('parent_id', 0)
->where('company_id', $request->user()->company_id)
->get();
return response()->json(['data' => $items]);
}
using this model and controller I can get all the data from the first and second tables but when I want to filter the second table based on the created_at column I'm unable to do it.
I tried this method but it didn't work.
function getGeneralLedger(Request $request)
{
$items = first_level::with('children')
->where('parent_id', 0)
->where('company_id', $request->user()->company_id)
->wherebetween('children.transactions.created_at', [$request->from, $request->to])
->get();
return response()->json(['data' => $items]);
}
I get this error
Column not found: 1054 Unknown column 'children.transactions.created_at' in 'where clause' (SQL: select * from `first_levels` where `parent_id` = 0 and `company_id` = 1 and `children`.`transactions`.`created_at` between 2022-01-10 and 2022-02-09)",
try
function getGeneralLedger(Request $request)
{
$items = first_level::with('children')
->where('parent_id', 0)
->where('company_id', $request->user()->company_id)
->whereHas('children.transactions', function ($q) use ($request) {
$q->wherebetween('created_at', [$request->from, $request->to]);
})
->get();
return response()->json(['data' => $items]);
}
Source: https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence

Category slug + post slug Laravel Eloquent

I have a blog route and I'd like to show article with a category_slug.
Route::get('/blog/{category_slug}/{slug}', [App\Http\Controllers\BlogController::class, 'index'])
->where('category_slug', '[\-_A-Za-z]+')
->where('slug', '[\-_A-Za-z]+');
public function categories_blog()
{
return $this->belongsTo(CategoriesBlog::class, 'category_id');
}
public function blogs()
{
return $this->hasMany(Blog::class);
}
with this eloquent relations works fine:
example: www.mysite.com/blog/first_article
public function index($category_slug, $slug)
{
$blogs = Blog::with('categories_blog')
->where('slug', '=', $slug)
->first();
}
with this eloquent relations doesn't work:
example: www.mysite.com/blog/accessories/first_article
public function index($category_slug, $slug)
{
$blogs = Blog::with('categories_blog')
->where('category_slug', '=', $category_slug)
->where('slug', '=', $slug)
->first();
}
Doesn't recognize relationship with 'categories blogs':
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_slug' in 'where clause' (SQL: select * from `blogs` where `category_slug` = accessories `slug` = first_article limit 1)
How I can fix it or is there is a best way for get this?
thank you very much.
Use whereHas
$blogs = Blog::with('categories_blog')->whereHas('categories_blog',function ($query)use($category_slug){
$query ->where('category_slug', $category_slug);
})
->where('slug',$slug)
->first();

Adding Multiple Where Clause from Model in Laravel

I am trying to clean my code up, and working on the Models
I have the following 2 tables broken down like this:
Roll Table
|id|roll_id|member_id|.......
Members table
|id|first_name|last_name|rank|
I have the following on my Roll Model
public function member()
{
return $this->belongsTo('App\Member');
}
This on my Member model
public function roll()
{
return $this->hasMany('App\Roll');
}
While the following code does return the correct results
$roll = Roll::with(['member'])
->where('status', '!=', 'A')
->get();
return ($roll);
I would like to add an extra where clause
->where('rank','<', 12)
However, I get the following error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'member.rank'
in 'where clause' (SQL: select * from Roll where roll_id = 4 and
status != A and `mem ▶"
You can use whereHas method to filter on the relations:
$roll = Roll::with(['member'])
->where('status', '!=', 'A')
->whereHas('member', function($query) {
$query->where('members.rank', '<', 12);
})
->get();
Hope this will resolve your issue.

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