How to get the desire output using laravel query. Tried that way does not get success please guide thanks a lot in advance
Is there any way we can set it in model if possible please guide
User
id name b_id
1 Alax 1,3
2 Rex 2,4
3 Lex 2,3
Books
id book_name book_author
1 Javascript jim
2 PHP json
3 LARAVEL rax
4 SYMPHONY Alax
Output
id name b_id
1 Alax Javascript, LARAVEL
2 Rex PHP, SYMPHONY
3 Lex PHP, LARAVEL
And Query:
$res = DB::table('user')->leftJoin('book', function($join){
$join->on(DB::raw("find_in_set(book.id, user.b_id)",DB::raw(''),DB::raw('')));
});
Try this.
$data = \DB::table("user")
->select("user.*",\DB::raw("GROUP_CONCAT(book.book_name) as book_name"))
->leftjoin("book",\DB::raw("FIND_IN_SET(book.id,user.b_id)"),">",\DB::raw("'0'"))
->get();
\DB::raw("GROUP_CONCAT(CONCAT(book.book_name, ' - ', book.book_author) SEPARATOR ', ') AS book_name_author")
Your output looks like.
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Alax
[b_id] => 1,3
[book_name] => Javascript,LARAVEL
)
[1] => stdClass Object
(
[id] => 2
[name] => Rex
[b_id] => 2,4
[book_name] => PHP,SYMPHONY
)
[2] => stdClass Object
(
[id] => 3
[name] => Lex
[b_id] => 2,3
[book_name] => PHP,LARAVEL
)
)
)
I don't know the exact Laravel syntax problem you are currently facing, but there is a potential problem with your current left join logic. If a given user have more than one matching book, then he would appear in the result set one time for each match. This presumably is not what you want, so I would suggest joining using EXISTS logic:
SELECT u.id, u.name
FROM User u
WHERE EXISTS (SELECT 1 FROM Books b WHERE FIND_IN_SET(b.id, u.b_id) > 0);
The Laravel code:
$res = DB::table('user u')
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('book b')
->whereRaw('FIND_IN_SET(b.id, u.b_id) > 0');
})
->get();
The exists approach ensures that each user would only appear once in the result set.
Related
$nearProviderdata = DB::table('users','category')
->leftJoin('service_provider_rating', 'users.id', '=', 'service_provider_rating.rating_receiver_user_id')
->leftJoin('service_provider_category', 'users.id', '=', 'service_provider_category.user_id')
->leftJoin('category', 'category.id', '=', 'service_provider_category.category_id')
->select('users.id as user_id','users.full_name','category.id as category_id','category.title')
->distinct()->get()->groupBy('user_id');
I am receiving the below response from the above code.
[0] => stdClass Object
(
[user_id] => 11
[full_name] => Jimmi Patel
[category_id] => 1
[title] => Painter
)
[1] => stdClass Object
(
[user_id] => 11
[full_name] => Jimmi Patel
[category_id] => 2
[title] => Home Cleaner
)
[2] => stdClass Object
(
[user_id] => 11
[full_name] => Jimmi Patel
[category_id] => 3
[title] => Electrician
)
I want the result something like as below
[0] => stdClass Object
(
[user_id] => 11
[full_name] => Jimmi Patel
['category'] => [ category_id => 1,title => Painter, category_id => 2, title => Home Cleaner, category_id => 3, title => Electrician]
)
It's laravel version 5.7. I have tried group by also to get the result but it is showing only 1 row and 1 category data. So i am using the subquery to get the data. Please let me know if i am doing something wrong in it.
I think you don't need to join that much with raw query
better use models for this purpose
Simplified example:
$nearProviderdata = User::with(
'categories',
'serviceProviderRating',
'serviceProviderCategory'
)->get();
You can also add select in subquery and map response
i have an array, something like
[0] => Array
(
[id] => 1
[name] => Jimmy
[address] => Array
(
[number] => 1
[street] => Astreet
)
)
I need to access [street] using something like helper array_get dot notation:
array.address.street
However, as it can have multiple elements, I need something like
array*
where it can just get the first one.
Coming from cakephp they have a helper so i can do
array.{*}.address.street
Is there something similar in laravel, i cannot find such
If you want to get street from the first element you can do:
array_get(head($array), 'address.street');
// or
array_get($array, '0.address.street')
And if you want to get list of elements that contains street you can use array_pluck:
array_filter(array_pluck($array, 'address.street'));
$addresses = array_map(function($obj) {
if ( array_key_exists('address', $obj) )
if ( count($obj['address']) > 0 )
return $obj['address'][0];
}, $yourArray);
I want to retrieve data from three tables: courses, competencies and competency_standards. The following query almost works, but it does not bring back the associated competency_standards table data.
$coursesAndComps = Course::with(
array('competencies' => function($query)
{
Competency::with('competency_standards');
})
)->get()->toArray();
Where the competencies table is linked to the courses table (competencies.course_id = course.id) and the competency_standards table links to the competencies table (competency_standards.competencey_id = competency.id).
The array returned looks like this:
Array
(
[0] => Array
(
[id] => 1
[name] => the first course
[competencies] => Array
(
[0] => Array
(
[id] => 9
[course_id] => 1
[name] => first course comp 1
)
[1] => Array
(
[id] => 10
[course_id] => 1
[name] => first course comp 2
)
)
)
)
but within the competencies array, I was hoping to find another array called competency_standards like this:
...
[0] => Array
(
[id] => 9
[course_id] => 1
[name] => first course comp 1
[competency_standards] => Array
(
[0] => Array
(
[id] => 22
[competency_id] => 9
[name] => standard foo
)
[1] => Array
(
[id] => 23
[competency_id] => 9
[name] => standard bar
)
)
)
...
Is this possible? Am I going about this the wrong way?
It should be possible to use:
$coursesAndComps = Course::with('competencies', 'competencies.standards')
->get()->toArray();
but of course you need to have defined standards relationship in Competency model to link Competency with Standard
I am having problems with working with multidimensional array data.
I have 2 tables (maincategories and categories) which I joined.
Table Maincategories:
id, maincategory
Table Categories:
id, maincategory_id, category
$this->db->select('m.id, m.name_en AS maincategory')
->select('c.name_en AS category')
->from('categories AS c')
->join('maincategories AS m', 'c.maincategory_id=m.id', 'left');
$query = $this->db->get();
This results in all 4 categories being listed as below.
[3] => stdClass Object
(
[id] => 7
[maincategory] => Career
[category] => Business Sales
)
[4] => stdClass Object
(
[id] => 1
[maincategory] => Accommodation
[category] => Camping
)
[5] => stdClass Object
(
[id] => 6
[maincategory] => Accommodation
[category] => Hostels
)
[6] => stdClass Object
(
[id] => 7
[maincategory] => Career
[category] => Career Events
)
4 unique categories are in the list while 2 of them belong to maincategory: Accommodaton and the other 2 categories belong to maincategory: Career. What I wanted to achieve is that each maincategory is only listed ONCE and the categories shall fall under that main_category. Something like this:
[6] => stdClass Object
(
[id] => 7
[maincategory] => Career
[category] => array(Career Events, Business Sales, .....)
)
[7] => stdClass Object
(
[id] => 7
[maincategory] => Accommodation
[category] => array(Hostels, Camping, .....)
)
The array dump will maybe/probably look different in its end result but I hope you understand what I am trying to achieve. I read quite a bit about transforming multidimensional arrays. I just can't get my head around it yet. Thanks a lot for any direction!
Try like this:
function fetch_all(){
$data = array();
$first = $this->db->select('id, maincategory')->get('maincategory')->result_array();
if( is_array( $first ) && count( $first ) > 0 ){
foreach( $first as $key => $each ){
$data[$key]['category_id'] = $each['id']; #main category id
$data[$key]['maincategory'] = $each['id']; #main category name
$second = $this->db->select('category')->where_in('maincategory_id', $each['id'])->get('categories')->result_array();
$data[$key]['category'] = $second; #all sub-category(s) array
}
}
}
instead of using so many db calls, try using the underscore.php library http://brianhaveri.github.io/Underscore.php/
I've outlined a few examples of how to use it for this case here: http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php
Edit
I got a Joomla ticketing module, that displays at a certain part of the page, the current tickets of the current user. I want to create user groups, in which the group members can view all of the tickets belongin to that group. I thought the easiest way is to create Joomla groups, assign the users to those, and to when a user loges in, it can see all of the tickets in its group. I added my code to the start of the function, but something is wrong... For every user (currently the "Registered" ones) displays the same result, that of the last users tickets, and I don't know why: Here is the code:
function gTickets()
{
$user =& JFactory::getUser();
$user_id = (int) $user->get('id');
//get user_group_id from db based on current users id
{...}
//get all users with that user_group_id
$db1->setQuery($db1->getQuery(true)
->select('*')
->from("#__user_usergroup_map")
->where("group_id = '$groupss'")
);
$groupss1=$db1->loadRowList();
$return1=array();
// for every user_id
foreach ($groupss1 as $keya)
{
$user_id = $keya[0]; // the id of users
$where = "";
if ($this->is_staff)
$where .= " AND t.`staff_id`='".$user_id."'";
else
$where .= " AND t.`customer_id`='".$user_id."'";
$tickets = $this->_getList(
"SELECT t.id, t.subject, t.last_reply_customer, s.name AS status_name FROM
#__rsticketspro_tickets t LEFT JOIN #__rsticketspro_statuses s ON
(t.status_id=s.id) WHERE 1 $where ORDER BY `last_reply` DESC", 0,
$this->params->get('tickets_limit', 3));
print_r($tickets);
return $tickets;
}
I got some questions, that I didn't know how to seach for...
what is the letter dot fieldname in the sql query? eg:
SELECT m.ticket_id, m.message FROM #__ticket_messages m WHERE m.user_id !='".$user_id."
what does the "m" mean before the WHERE?
what does the "1" do here: WHERE 1 $where
Also, I looked in the ACL managers, but could not make it work with this code.
Edit: Thanks for the fast answers! I got 1 more, and I think it's an easy one, but I can't get it to work...
If I print the content of the $ticket array into another array, I get an array with multiple arrays. That is why my code is not working... The array i'm getting is:
Array (
[0] => stdClass Object (
[id] => 1
[subject] => use1
[last_reply_customer] => 1
[status_name] => open
)
)
Array (
[0] => stdClass Object (
[id] => 3
[subject] => use2
[last_reply_customer] => 1
[status_name] => open
)
[1] => stdClass Object (
[id] => 2
[subject] => use2
[last_reply_customer] => 1
[status_name] => open
)
)
I would like the array to look like this:
Array (
[0] => stdClass Object (
[id] => 1
[subject] => use1
[last_reply_customer] => 1
[status_name] => open
)
[1] => stdClass Object (
[id] => 3
[subject] => use2
[last_reply_customer] => 1
[status_name] => open
)
[2] => stdClass Object (
[id] => 2
[subject] => use2
[last_reply_customer] => 1
[status_name] => open
)
)
Thanks!
Edit: is all of this complicated to achieve?
Look into ACL to get that part working, but to answer the 3 questions you posted at the bottom of your question:
1) the letter.field_name in the query represents the table alias.field_name in the query. The alias (m) makes it easier so you don't have to keep typing the full table name each time.
2) The m before the WHERE is actually setting the table alias, but with lazy syntax. It should say:
SELECT m.ticket_id, m.message FROM #__ticket_messages AS m
3) The "WHERE 1" is the SQL way of saying "if (true)". Since they are immediately appending the $where clause afterwards "WHERE 1 $where", the resulting query looks like this:
WHERE 1 AND t.staff_id = '" . $user_id . "'
Did you code this originally? or is it a component that you picked up somewhere? It should be using the query builder instead of direct SQL like it currently is, to make it more portable and more clear as to what it is doing.