I am trying to get data from 3 tables using left join and where clauses, if i join two tables it shows record but when i join third table it does not pull record don't know why
public function get_customer_detail_4_withdraw($customer_id) {
$query = $this -> db -> select('*')
-> from('deposits')
-> where('deposits.customer_id', $customer_id)
-> join('customers', 'deposits.customer_id=customers.id', 'left')
-> where('withdraw_deposit.customer_id', $customer_id)
-> join('withdraw_deposit', 'withdraw_deposit.customer_id=deposits.customer_id', 'left')
-> get();
return $query -> result();
}
I think you need to re-arrange joins and where.
For example :
public function get_customer_detail_4_withdraw($customer_id) {
$query = $this -> db -> select('*')
-> from('deposits')
-> join('customers', 'deposits.customer_id=customers.id', 'left')
-> join('withdraw_deposit', 'withdraw_deposit.customer_id=deposits.customer_id', 'left')
-> where('deposits.customer_id', $customer_id)
-> where('withdraw_deposit.customer_id', $customer_id)
-> get();
return $query -> result();
}
Related
I am trying to retrieve multiple data from a database in laravel query builder. I get an error at "from".
I tried looking on internet, it keeps talking about join, but I don't want a join.
$showtitles = DB::table('funny_pictures', 'jokes', 'riddles')
->select('title', 'id', 'type')
->from('funny_pictures', 'jokes', 'riddles')
->where('user_id', $user -> id)
->orderBy('created_at')
->paginate(6);
return view('profile', compact('user', 'jokes', 'showtitles'));
This is the query, it gives the error at from
It only selects "funny_pictures" not jokes and riddles.
I would try something like this (with join rather than table with 3 tables in it).
$showtitles = DB::table('funny_pictures')
->join('jokes', 'user_id', $user -> id)
->join('riddles', 'user_id', $user -> id)
->select('title', 'id', 'type')
->from('funny_pictures')
->where('user_id', $user -> id)
->orderBy('created_at')
->paginate(6);
return view('profile', compact('user', 'jokes', 'showtitles'));
I'm trying to convert a query to CodeIgniter's query builder.
Original version:
$query = $this->db->query('SELECT tb.*,b.`property` FROM `email_template_blocks` as tb inner join email_blocks as b on tb.`block_id`=b.`id` where tb.template_id=' . $id);
if ($query->num_rows() < 1) {
return null;
}
return $query->result_array();
Query builder version:
$this->db->select('*');
$this->db->from('email_template_blocks');
$this->db->join('email_blocks', 'email_template_blocks.block_id = email_blocks.id', 'inner');
$this->db->where('email_template_blocks.template_id', $id);
$query = $this->db->get();
//$query = $this->db->query('SELECT tb.*,b.`property` FROM `email_template_blocks` as tb inner join email_blocks as b on tb.`block_id`=b.`id` where tb.template_id=' . $id);
if ($query->num_rows() < 1) {
return null;
}
return $query->result_array();
The query builder version returns no results (no error messages either and I have db debug on). I'm fairly certain I'm doing something wrong, but I find the CI documentation on joins lacking. How can I fix this?
I'd also like to know how to select all of the items from the email_template_blocks table and just join the property column of the email_blocks table.
Email template blocks table:
table schema
Email blocks table:
table schema
I am developing webshop. I have the following relations in my model.
shoppingCart -> hasMany -> shoppingCartItem
shoppingCartItem -> belongTo -> shoppingCart
I have in my controller function to get the product_id
$product = Product::find($request->get('product_id'))->first();
in the product table I have relation with the image. I store the image in different table which has only product_id and image_id.
Now my question is can I get
public function getCart(Request $request)
{
// get the shoppingCart
$shoppingCart = $this->loadShoppingCart($request->get('key'));
$carts = [
'amount' => $shoppingCart->total_products,
'price'=>$shoppingCart->total_price
];
// get theimage of the product
$productImage = Image::with('image_id', $request->get('image_id'));
return response()->json(['carts','productImage']);
}
I fixed like that.
$shoppingCart = ShoppingCart::where('key' ,$key)->with('shoppingCartItems', 'shoppingCartItems.product', 'shoppingCartItems.product.images')->first();
I need after get query from sql with Eloquent, create groupBy('date') and sortBy('price') with collect in Laravel 5.3.
In IndexController:
$flights = Flights::GetFlights($fromCity, $toCity, $fromDate);
$collection = collect($flights);
$sortFlight = $collection->groupBy('date')->sortBy('price');
In Model:
public function scopeGetFlights($query, $fromCity, $toCity, $fromDate)
{
// Between dates
$beforeDate = date('Y-m-d', strtotime($fromDate . '- 10 days'));
$afterDate = date('Y-m-d', strtotime($fromDate . '+ 10 days'));
$join = $query
-> join('airlines', 'airlines.pana', 'flights.airline')
-> where('flights.from_city', $fromCity)
-> where('flights.to_city', $toCity)
-> whereBetween('flights.date', [$beforeDate, $afterDate])
-> get([
'flights.*',
'airlines.pana as pana',
'airlines.name as airlineName'
]);
return $join;
}
Then Print_r($sortFlight), After print groupBy('date') is works
but sortBy('price') does not work!!!!
Where is my problem?
My solution for sorting values in a grouped collection in Laravel
$sponsors = Sponsor::with('sponsor_level')->whereStatus(1)->get();
// grouped by sponsor level name
$grouped = $sponsors->sortBy('sponsor_level.order')->groupBy(function($item, $key){
return $item->sponsor_level->name;
});
// finally sorted by sponsor name inside the group
return $grouped->map(function($item, $key){
return $item->sortBy('name');
});
You can chain these of course, I separated for better readability.
Try this single line
$sortFlight = $collection->groupBy('date')->sortBy('price')->values()->all();
From the doc
The sortBy method sorts the collection by the given key. The sorted collection keeps the original array keys, so in this example we'll use the values method to reset the keys to consecutively numbered indexes:
So after doing groupBy() do as follows.
$sorted = $collection->groupBy('date')->sortBy('price');
$sorted->values()->all();
Please help me:
SELECT *
FROM `product`
WHERE `vi_name` LIKE '%product%'
OR `en_name` LIKE '%product%'
AND `parent_id` != 0
I wrote my own code but some how it still count rows have parent_id = 0
function get_product_by_search_slug($slug) {
$this -> db -> from('product');
$this -> db -> like('vi_name', $slug);
$this -> db -> or_like('en_name', $slug);
$this -> db -> where_not_in('parent_id', 0);
$query = $this -> db -> get();
return $query -> result();
}
I got a solution and It work fine but somehow I don't quite satisfy with it:
function get_product_by_search_slug($slug) {
$this -> db -> select("* FROM `product` WHERE `vi_name` LIKE '%". $slug ."%' OR `en_name` LIKE '%". $slug ."%' AND `parent_id` != 0");
$query = $this -> db -> get();
return $query -> result();
}
you can try this
where('parent_id !=','0')
please let me know if you face any problem
Try this one
$this->db->like('vi_name', $slug)
->or_like('en_name', $slug)
->where('parent_id <>', 0);
$slugQ = $this->db->escape_like_str($slug);
$this->db
->select('*')
->from('product')
->where("(vi_name like '%$slugQ%' OR en_name like '%$slugQ%')", NULL, FALSE)
->where('parent_id !=', 0)
->get()
->result();
In my example I have merly escaped the slug for proper mysql lookup, you'll have to do a few extra checks yourself.