Create join with polymorphic relationship - laravel

I have two tables with the polymorphic relationships.
Invoice Table:
public function transactions(){
return $this->morphMany(TransactionAllocation::class, 'doc');
}
Payment Table:
public function doc()
{
return $this->morphTo();
}
This relationship works fine if I use with.
But when I am trying to use join, this does not work. Related Laravel Doc.
DB::table('invoices')
->join('transactions', function ($join){
$join->on('invoices.id','=','transactions.doc_id')
->where('transactions.doc_type','App\Models\Invoice');
})
->get()
Here is the error I get:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'transactions.doc_id'
in 'on clause' (SQL: select * from `invoices` inner join `transactions`
on `invoices`.`id` = `transactions`.`doc_id`
and `transactions`.`doc_type` = App\Models\Invoice)

Related

In Laravel 7 How to orderBy Roles->name?

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!

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.

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 use query scope in join request Laravel?

I have the following request:
$objects = Object::with("translate")->where(function ($query) use ($request) {
$query->language();
})->with('images')->with("category")->orderBy('id', 'desc')->paginate($limit);
So, in model Object there is method: translate:
public function translate()
{
return $this->hasMany("App\ObjectTranslate", "objectId", "id");
}
So, also in this model is:
public function scopeLanguage($query)
{
$languageHeader = new ModelLibrary();
return $query->where('language', '=', $languageHeader->getLanguage());
}
When I try to call scope in main request:
$query->language();
I get SQL error:
Column not found: 1054 Unknown column 'language' in 'where clause' (SQL:
You need to join the query with ObjectTranslate's table. your scopeLanguage() function will not work , since you are trying to access the column language which is not available in Object model column.
Note : With() will just eager loading, which will load the eloquent defined function, you cannot directly access their column in where clause.
Modify your scopeLanguage function
public function scopeLanguage($query)
{
$languageHeader = new ModelLibrary();
return $query->join('object_translate_table','object_translate_table.objectId','=','object_table.id')->where('object_translate_table.language',$languageHeader->getLanguage());
}
$objects = Object::with("translate","images","category")
->language()
->orderBy('id', 'desc')
->paginate($limit);
This should work
Assumin ObjectTranslate model's table name is object_translate_table and Object model's table name is object_table

How do I query a table with conditions from related table with Laravel 5

I have two tables,
table users:
id, name, etc
table videos:
id, user_id, etc
Model: Video
public function user()
{
return $this->belongsTo('App\Models\User');
}
I want to select all videos where it is owned by the user "Max".
I have tried thing like the following:
$Videos = \App\Models\Video::with('user')->where('user.name','=','Max')->get();
Unknown column 'user.name' in 'where clause' (SQL: select * from `videos` where `videos`.`deleted_at` is null and `user`.`name` = Max)
Thanks!
That's what whereHas is for:
$Videos = \App\Models\Video::with('user')->whereHas('user', function($q){
$q->where('name','=','Max');
})->get();

Resources