last day (14 days later) » 

8:56 AM
0
A: Laravel Eloquent - how to join a table

SandeeshEdit : I've tested the code. So here. In Company model public function items() { return $this->hasMany(Item::class); } In Item model public function currency() { return $this->belongsTo(Currency::class); } Controller logic $items = Company::with(['items' => function($query) use ($...

 
I'm getting this error: Unknown column 'currencies.item_id' in 'where clause' (SQL: select * from currencies where currencies.item_id
 
Can you post your currency table structure?
 
i update my question added the structure
 
Your relationship posted in the other comment is wrong, change it to public function currency() { return $this->belongsTo('App\Currency'); }
 
now I'm not getting the error but still, i don't get the result I need, I want to return an array of items and in that array instead of returning the currency_id I want to return currency object
 
8:56 AM
Is the order by field name from the items table or from company table? I'll write some test code and get back to you after your response
 
the name is for the items table, thank you
 
i've updated my answer after testing the code. should work, if not you're doing something wrong on your end.
 
still not working, i'm getting this results: {"rows":[{"guid":"d2a12dc0-3c70-11e7-b804-4f1bebfb243b","nam‌​e":"test","descrip‌​ti‌​on":"test","price":1‌​00,"currency_id":2,"‌​company_id":1}]}
 
does the currency table have the value for currency_id 2?
 
are you asking if there is a row 2 in the currency table?
yes
the name of the table is currencies
maybe this is the problem? that i using currency?
and not currencies
 
8:59 AM
that doesn't matter, currency model will auto fetch currencies table
i setup the exact same thingi
how are u getting guid in your result?
is the guid in the company table or item table?
 
in the company table
in my ui the user can change the company and then i send to the api the active company guid
 
so how did is there a guid value in the result you pasted? since only the item details are returned
 
sorry, this is the guid of the item
i also have guid in the items table
 
so the item has a guid for itself? ok makes sense
 
ignore it
yes
 
9:02 AM
ok can you just run Currency::all(); and show me the result
 
just a sec
the chat show me an error that the message is to long
{"total_amount":1,"rows":[{"id":1,"guid":"94c54cd0-3b1f-11e7-9456-4f593b836de4",‌​"name":"\u05e9\u05e7\u05dc","symbol":"\u20aa","code":"ILS","rate":"1","created_at‌​":"2017-05-17 16:40:48","updated_at":null}...
 
that's fine
this seems normal
tbh it's correct
i've setup the code in my local project from scratch with dummy data and it's working flawless
 
thank you!
maybe you want me to send you the full function?
 
ok do this, copy paste your 3 table migration + model and controller logic into pastebin
so that i can find the mistake
because the answer i've given you is tested and works perfect, it has to be something on your end
 
9:12 AM
let me check
migrations are correct, you haven't shared the model code
$rows = Company::with(['items' => function($query) use ($sort_order,$sort_by) {
$query->with('currency')->orderBy($sort_by, $sort_order);
}])
->where('guid',$guid)
->first()
->items();
 
just a sec
 
it's not items(), it's items
check my code again and copy paste it correctly
and then return type return $rows; n see what you get
 
i know, but it is not working when i use items
 
ok your code is more complex than your question
i see the problem
 
this is because i'm doing pagination on server side
 
9:20 AM
you want to keep it as a a query builder instead of fetching the results immediately.
the pagination applies to the items right?
but your applying search to the company properties as well before that
 
no, the search is for the items
 
the fields are all from company table, vat_number, etc
not tiems
items*
 
sorry, my bad.
i copied the code form my "clients" page and change it to items, forgot to change the search fields
i fix it to: $query->where('name','LIKE','%'.$search_query.'%')
->orWhere('description','LIKE','%'.$search_query.'%')
->orWhere('price','LIKE','%'.$search_query.'%')
 
ok sec
alright i tried tweaking a bit, but your custom pagination is making life a lot harder
i'll tweak a bit n see if i can help you with this
 
you can ignore that
write it without the paginaion
pagination*
 
9:34 AM
what abt the search?
without the pagination then i'm done
 
ignore that too, i will try to add it after i will have the right way to get the currency inside the item object
 
if you ignore the search and pagination the original code i paste works without making a single change
And here is the same with search and pagination
sec there's a small mistake in that, let me fix it
with search and pagination
also improved your count query above. always run the count on query and not on the result, since doing so will fetch all the data just to be counted after fetching, running the count on query will fetch the count from db directly
hope this helps
 
9:59 AM
Sandeesh thank you so much!!!
it is working
 
np
 

last day (14 days later) »