How can i make inner join in method instead of controller?
up vote
0
down vote
favorite
I have a two tables orders and product. Relation is One to One. In my order class i created method product
public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id');
}
Now in controller i want select rows where id is not null that's why i wanna use InnerJoin
Order::where('order_id', 7)
->join('products', 'products.key_id', 'orders.key_id')
->get();
That is fine but i would like get
Order::where('order_id', 7)->get();
and all join move to method like:
public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id')
->join('products', 'products.key_id', 'orders.key_id')
}
How can i do this ?
laravel-5 eloquent
add a comment |
up vote
0
down vote
favorite
I have a two tables orders and product. Relation is One to One. In my order class i created method product
public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id');
}
Now in controller i want select rows where id is not null that's why i wanna use InnerJoin
Order::where('order_id', 7)
->join('products', 'products.key_id', 'orders.key_id')
->get();
That is fine but i would like get
Order::where('order_id', 7)->get();
and all join move to method like:
public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id')
->join('products', 'products.key_id', 'orders.key_id')
}
How can i do this ?
laravel-5 eloquent
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a two tables orders and product. Relation is One to One. In my order class i created method product
public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id');
}
Now in controller i want select rows where id is not null that's why i wanna use InnerJoin
Order::where('order_id', 7)
->join('products', 'products.key_id', 'orders.key_id')
->get();
That is fine but i would like get
Order::where('order_id', 7)->get();
and all join move to method like:
public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id')
->join('products', 'products.key_id', 'orders.key_id')
}
How can i do this ?
laravel-5 eloquent
I have a two tables orders and product. Relation is One to One. In my order class i created method product
public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id');
}
Now in controller i want select rows where id is not null that's why i wanna use InnerJoin
Order::where('order_id', 7)
->join('products', 'products.key_id', 'orders.key_id')
->get();
That is fine but i would like get
Order::where('order_id', 7)->get();
and all join move to method like:
public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id')
->join('products', 'products.key_id', 'orders.key_id')
}
How can i do this ?
laravel-5 eloquent
laravel-5 eloquent
asked Nov 8 at 16:42
Wraith
172114
172114
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You have defined the relations between both the models, so If you fetch order with the following query:
Order::where('order_id', 7)->with('product')->get();
Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')
After doing this, dump and die the $order, there will be an key pair with product and its values.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You have defined the relations between both the models, so If you fetch order with the following query:
Order::where('order_id', 7)->with('product')->get();
Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')
After doing this, dump and die the $order, there will be an key pair with product and its values.
add a comment |
up vote
0
down vote
You have defined the relations between both the models, so If you fetch order with the following query:
Order::where('order_id', 7)->with('product')->get();
Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')
After doing this, dump and die the $order, there will be an key pair with product and its values.
add a comment |
up vote
0
down vote
up vote
0
down vote
You have defined the relations between both the models, so If you fetch order with the following query:
Order::where('order_id', 7)->with('product')->get();
Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')
After doing this, dump and die the $order, there will be an key pair with product and its values.
You have defined the relations between both the models, so If you fetch order with the following query:
Order::where('order_id', 7)->with('product')->get();
Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')
After doing this, dump and die the $order, there will be an key pair with product and its values.
answered Nov 8 at 16:51
engrhussainahmad
1107
1107
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212316%2fhow-can-i-make-inner-join-in-method-instead-of-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown