Table : Services
+-----+--------------+
| id | title |
+-----+--------------+
| 1 | Service 1 |
+-----+--------------+
| 2 | Service 2 |
+-----+--------------+
Table : Workshops { HasMany WorkshopServices }
+-----+---------------+
| id | title |
+-----+---------------+
| 1 | Workshop 1 |
+-----+---------------+
| 2 | Workshop 2 |
+-----+---------------+
Table : WorkshopServices
+-----+--------------+-------------+
| id | workshop_id | service_id |
+-----+--------------+-------------+
| 1 | 1 | 1 |
+-----+--------------+-------------+
| 2 | 1 | 2 |
+-----+--------------+-------------+
I want to find Workshops by service_id
My Query
$this->Workshops->find()
->contain([
'WorkshopServices'
])
->where([
'WorkshopServices.service_id IN'=>[1,2,3]
]);
Query Result
Unknown column `WorkshopServices.service_id` in 'where clause'
Actually Workshops table is not generating any JOIN with WorkshopServices table.
How can I write the query to get proper result from Query Builder?
Use matching:
$array = [1,2,3];
$this->Workshops->find()
->matching('WorkshopServices', function ($q) use($array) {
return $q->where(['WorkshopServices.service_id IN' => $array])
});
I updated #GabrielFerreira's query and Grouping the rows by WorkshopServices.workshop_id, This solution meet my Problem
$array = [1,2,3];
$this->Workshops->find()
->select([
'title'=>'Workshops.title',
'id'=>'Workshops.id',
])
->matching('WorkshopServices', function ($q) use($array) {
return $q->where(['WorkshopServices.service_id IN' => $array]);
})
->group(['WorkshopServices.workshop_id']);
Related
I'm using Snowflake's Object_Construct to create a JSON.
Use case: Snowflake table has Person Title as
Person Name
Title_1
Title_2
Title_3
in JSON structure, I need to transform this as
"Person_Title"[
{ "Title_1",
"Title_1_desc"
},
{"Title_2",
"Title_2_desc"
},
{"Title_3",
"Title_3_desc"
}
]
Below example outlines the placement of functions to get the output.
Though not sure if you are looking for any sort of dynamism, which following example does not gives -
with cte (Person_Name, Title_1, Title_2, Title_3) as (
select 'name-1','t1-desc-1','t2-desc-1','t3-desc-1' union all
select 'name-2','t1-desc-2','t2-desc-2','t3-desc-2' union all
select 'name-3','t1-desc-3','t2-desc-3','t3-desc-3'
)
select object_construct('Person_title',
array_construct(
object_construct('Title_1',Title_1),
object_construct('Title_2',Title_2),
object_construct('Title_3',Title_3))) output_json
from cte;
+------------------------------+
| OUTPUT_JSON |
|------------------------------|
| { |
| "Person_title": [ |
| { |
| "Title_1": "t1-desc-1" |
| }, |
| { |
| "Title_2": "t2-desc-1" |
| }, |
| { |
| "Title_3": "t3-desc-1" |
| } |
| ] |
| } |
| { |
| "Person_title": [ |
| { |
| "Title_1": "t1-desc-2" |
| }, |
| { |
| "Title_2": "t2-desc-2" |
| }, |
| { |
| "Title_3": "t3-desc-2" |
| } |
| ] |
| } |
| { |
| "Person_title": [ |
| { |
| "Title_1": "t1-desc-3" |
| }, |
| { |
| "Title_2": "t2-desc-3" |
| }, |
| { |
| "Title_3": "t3-desc-3" |
| } |
| ] |
| } |
+------------------------------+
Query with dynamism -
with cte (Person_Name, Title_1, Title_2, Title_3) as (
select 'name-1','t1-desc-1','t2-desc-1','t3-desc-1' union all
select 'name-2','t1-desc-2','t2-desc-2','t3-desc-2'
)
select object_construct('Person_title',
array_agg(object_construct(TITLE_DESC,TITLE))) as output_col
from
(select * from cte
unpivot (title for title_desc in (Title_1, Title_2, Title_3))
)
group by Person_Name;
+------------------------------+
| OUTPUT_COL |
|------------------------------|
| { |
| "Person_title": [ |
| { |
| "TITLE_1": "t1-desc-1" |
| }, |
| { |
| "TITLE_2": "t2-desc-1" |
| }, |
| { |
| "TITLE_3": "t3-desc-1" |
| } |
| ] |
| } |
| { |
| "Person_title": [ |
| { |
| "TITLE_1": "t1-desc-2" |
| }, |
| { |
| "TITLE_2": "t2-desc-2" |
| }, |
| { |
| "TITLE_3": "t3-desc-2" |
| } |
| ] |
| } |
+------------------------------+
I have api call from react to laravel as backend
const response=await fetch("http://localhost:8000/api/getcandidates/");
but I got response as Kanban.jsx:65
GET http://localhost:8000/api/getcandidates/ 404 (Not Found)
and here is my api.php
Route::get('/getcandidates',[EmployeeController::class,'index']);
Route::get('/getcandidates_schedule',[EmployeeController::class,'scheduleresponse']);
Route::get('/getcandidates_rejection',[EmployeeController::class,'rejectionreponse']);
Route::get('/getcandidates_waiting',[EmployeeController::class,'waitingresponse']);
Route::post('/add_employee_first',[EmployeeController::class,'store']);
Route::post('/updatecolumn',[EmployeeController::class,'updatecolumn']);
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
output of php artisan route:list:
Domain | Method | URI | Name | Action | Middleware |
+--------+----------+----------+------+---------+------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api |
| | | | | | auth:api |
+--------+----------+----------+------+---------+------------
Please help me .
So I had to implement facebook login, which requires https connection, before I was using php artisan serve, but it only allows http, now I decided to migrate whole project to vagrant. Everything works fine but passport.
I checked and found that theese two lines are causing internal error 500
auth()->attempt($loginData, true);
auth()->user()->createToken('authToken')->accessToken;
when removed, I get no error. My whole login function look like this:
public function login(Request $request)
{
$loginData = $request->validate([
'name' => 'required',
'password' => 'required'
]);
$a = auth()->attempt($loginData, true);
if(!$a) {
return response(['message'=>'Invalid credentials', 'auth' => false]);
}
$accessToken = auth()->user()->createToken('authToken')->accessToken;
return response(['user' => auth()->user(), 'access_token' => $accessToken, 'auth' => true, "attempt" => $a, "auth()" => auth()]);
}
In addition, I've created users via tinker, tokens and users were stored.
here is my db data
mysql> select * from users;
+----+-------+--------------------------------------------------------------+----------------+---------------------+---------------------+
| id | name | password | remember_token | created_at | updated_at |
+----+-------+--------------------------------------------------------------+----------------+---------------------+---------------------+
| 1 | admin | $2y$10$V8fM6Vvc7xOJxN/LPyImUeQNzG9/k3tSsaF8qf1NsxrzK9B3VPmsa | NULL | 2019-08-26 15:22:02 | 2019-08-26 15:22:02 |
| 2 | demis | $2y$10$oRvcL0ZC2NQuGXE446GhfO1KBcAw3h8d/TGQmODL/AFHnat1I.Yvq | NULL | 2019-08-26 15:45:27 | 2019-08-26 15:45:27 |
+----+-------+--------------------------------------------------------------+----------------+---------------------+---------------------+
2 rows in set (0.00 sec)
mysql> select * from oauth_access_tokens;
+----------------------------------------------------------------------------------+---------+-----------+-----------+--------+---------+---------------------+---------------------+---------------------+
| id | user_id | client_id | name | scopes | revoked | created_at | updated_at | expires_at |
+----------------------------------------------------------------------------------+---------+-----------+-----------+--------+---------+---------------------+---------------------+---------------------+
| 2e82923d6fdc1cc98580b683952b7b96d555e67d6b7e06b8080f8a55227b6c1d7a755fc21b570941 | NULL | 1 | authToken | [] | 0 | 2019-08-26 15:21:52 | 2019-08-26 15:21:52 | 2020-08-26 15:21:52 |
| 610f8fd1f2212ab257810de88605bff4e419f26477bcf63f8dfe20018d9bcf74797bb9c817d6450a | NULL | 1 | authToken | [] | 0 | 2019-08-26 15:45:19 | 2019-08-26 15:45:19 | 2020-08-26 15:45:19 |
+----------------------------------------------------------------------------------+---------+-----------+-----------+--------+---------+---------------------+---------------------+---------------------+
What's wrong, Why I can't login, maybe there's something wrong with configuration?
Run php artisan passport:install on the new server.
I'm working with meteor+angular. The following loops through all the documents in the items collection. Within the loop, once a meal == true is hit, it loops the related combos to make dropdowns that use as options, documents from the comboitems collection.
// controller.ng.js
angular.module("myApp").controller("menuCtrl",
function($scope, $stateParams, $meteor){
$scope.items = $meteor.collection(Items);
$scope.combos = $meteor.collection(Combos);
$scope.comboitems = $meteor.collection(Comboitems);
}
);
// view.ng.html
<div ng-repeat="item in items">
<div ng-if="true === item.meal">
<ul>
<li ng-repeat="combo in combos | filter: {itemId: item._id}">
<select ng-model="comboitems" ng-options="comboitem.itemId for comboitem in (comboitems | filter: {comboId: combo._id})"></select>
</li>
</ul>
</div>
</div>
Using the data provided below, once I hit Pizza Meal I will have two dropdowns: Choose your pizza and Choose your drink, where in the former I will be able to choose between pizza 1 and pizza 2, while in the latter between drink 1 and drink 2.
This is the result
My issue is that I can't find a way to render the item.name, knowing its id in the comboitems collection once the <select> is being rendered.
Here is a sample data showing how the relations work:
items collection:
_id | name | price | meal
---------------------------------
a | pizza 1 | 5.00 | false
b | pizza 2 | 6.00 | false
c | drink 1 | 1.00 | false
d | drink 2 | 1.00 | false
e | Pizza meal | 5.00 | true
combos collection:
_id | name | itemId
-----------------------------------
x | Choose your pizza | e
y | Choose your drink | e
comboitems collection:
_id | itemId | comboId
-----------------------
1 | a | x
2 | b | x
3 | c | y
3 | d | y
Use select as in your ng-options directive:
<select ng-model="selectesdItem"
ng-options="comboitem.itemId as getName(comboitem.itemId) for comboitem in (comboitems | filter: {comboId: combo._id})">
</select>
and bind the result to a function that will return the name:
$scope.getName = function(id) {
return $filter('filter')($scope.items, { _id: id }, true)[0].name;
}
http://plnkr.co/edit/efym7LpeSjIqkDKbdLR0?p=preview
I had hard time to think about good title, but I hope that this explains.
Posts table
+----+---------+-----------------+
| id | title | text |
+----+---------+-----------------+
| 1 | Title 1 | This is example |
| 2 | Title 2 | This is example |
| 3 | Title 3 | This is example |
+----+---------+-----------------+
Tags table
+----+--------+
| id | tag |
+----+--------+
| 1 | jQuery |
| 2 | PHP |
| 3 | Stack |
+----+--------+
Category table
+----+------------+
| id | category |
+----+------------+
| 1 | Category 1 |
| 2 | Category 2 |
| 3 | Category 3 |
+----+------------+
Post tags relation table (same thing with category)
+---------+--------+
| post_id | tag_id |
+---------+--------+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
+---------+--------+
This is the result what i want to see in my view:
+---------+------------------+--------------------+------------+
| Title | Text | Tags | Categories |
+---------+------------------+--------------------+------------+
| Title 1 | This is example | jQuery, PHP | Category 2 |
| Title 2 | This is example | Stack | Category 1 |
| Title 3 | This is example | jQuery, PHP, Stack | Category 1 |
+---------+------------------+--------------------+------------+
In my controller i have
public function index()
{
$posts = Posts::orderBy('title', 'ASC')->get();
return View::make('posts', array(
'posts' => $posts)
);
}
And in my view I can list all posts with foreach loop
#foreach ($posts as $post)
Title: {{ $post->title }}
Text: {{ $post->text }}
Tags: ?
Categories: ?
#endforeach
Question is that what is best way to get tags and categories for each post inside foreach loop?
Shakil's tips was right on the spot and i found what i was looking for. I am surprised that it was so easy to achieve. Here is what i did:
Posts -model
class Posts extends Eloquent
{
protected $table = 'posts';
public function tags()
{
return $this->belongsToMany('Tags', 'post_tags', 'post_id', 'tag_id');
}
public function category()
{
return $this->belongsToMany('Category', 'post_categories', 'post_id', 'category_id');
}
}
View
#foreach ($posts as $post)
Title: {{ $post->title }}
Text: {{ $post->text }}
Tags:
#foreach ($post->tags as $tag)
{{ $tag }}
#endforeach
Categories:
#foreach ($post->categories as $category)
{{ $category }}
#endforeach
#endforeach
Thanks for all for your help!