Query for relational model in laravel 5.3 - php

I have 3 models named "User","Order","Orders_products". The relations are following:
User has many Order
Order has many Orders_products
Here are the model's code
User Model
public function order(){
return $this->hasMany('App\Order');
}
Order Model
public function order_product(){
return $this-> hasMany('App\Orders_product');
}
public function user(){
return $this-> belongsTo('App\User');
}
Orders_products Model
public function order(){
return $this->belongsTo('App\Order');
}
An user could have multiple orders and all orders could have multiple ordered products. How can i get a particular users all orders along with the ordered products.

User model
public function order()
{
return $this->hasMany(Order::class);
}
Order Model
public function user()
{
return $this->hasOne(User::class);
}
public function orders_products()
{
return $this->belongsToMany(Orders_products::class);
}
Orders_products
public function order()
{
return $this->belongsToMany(Order::class);
}

This should work, $userid is how ever you specify the user id in your app.
User::with('order_product.order')->where('id', $usersid)->get();

$Order = Order::with('order_product')->where(['user_id'=>$id])->get();
where $id is the user_id

Related

How to set relation in laravel with two ids?

I have these two tables:
product
- id
- name
favorites
- id
- product_id
- user_id
So, a user can add product to favorites only once. How can I set up this relation something like the following?
public function favorites() {
return $this->hasOne(Favorite::class, 'user_id', 'product_id')
}
So, I want to use both product_id & user_id such that the query would return proper result as per the following:
Get me the wishlist of user with id 1 and product with id 13!
you can do something like that:
in favourite Model class:
public function product(){
return $this->belongsTo('App\Product');
}
public function user(){
return $this->belongsTo('App\User');
}
In Product Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}
In User Model Class:
public function favorites(){
return $this->hasMany('App\Favorite');
}
The user may have many favorites, so in Class User
public function favorites()
{
return $this->hasMany('App\Favorite');
}
Class Favorite
public function product()
{
return $this->belongsTo('App\Product');
}
public function user()
{
return $this->belongsTo('App\User');
}
If you have user you can
$userFavProducts = $user->favorites;
$product2 = $user->favorites()->where('product_id', 2)->get();
You should try this:
Favorites Model
public function product(){
return $this->belongsTo('App\Product','product_id');
}
public function user(){
return $this->belongsTo('App\User','user_id');
}

Laravel defining relationship

Am still new to laravel
I have the following tables
user
id
first_name
last_name
educations
id,
user_id //references user id
type
Now in my user model i would like to get a specific users educations which can be many
so a user can have many educations but each education belongs to a single user.
So in my user model i have
public function education()
{
return $this->hasMany('App\ApplicantEducation','id','user_id');
}
In my Education model i have
public function user()
{
return $this->belongsTo('App\User','user_id','id');
}
But the above fails, I cannot retrieve user specific educations
Where am i going wrong?
try this:
in User Model:
public function educations()
{
return $this->hasMany('App\ApplicantEducation', 'user_id');
}
in Education Model:
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
Change return $this->hasMany('App\ApplicantEducation','id','user_id');
to return $this->hasMany('App\ApplicantEducation','user_id', 'id'); you also ommit the id and user_id.
As your foreign_key is well formed, you can also rwite this simple code,
class User{
public function education()
{
return $this->hasMany('App\ApplicantEducation');
}
}
Class Educations{
public function user()
{
return $this->belongsTo('App\User');
}
}
Here,
$this->hasMany('App\ApplicantEducation','id','user_id');
In above statement first argument should be Model second should be foreign key and the third one is any other key from Education model.
Here, second and third arguments are not mandatory.
In User Model
class User...{
public function education()
{
return $this->hasMany('App\ApplicantEducation');
}
In Education Model
public function user()
{
return $this->belongsTo('App\User');
}
Here, additional parameters are not mandatory might be your addition parameters creates issue,
and now you can retrieve your user with Education by
$user = User::with('education')->get();
This can retrieve all the users with their education.
I hope it helps,Thank you, Happy coding.
You should try this:
Education Model
public function user()
{
return $this->belongsTo('App\User','user_id);
}
User Model
public function education()
{
return $this->hasMany('App\ApplicantEducation');
}

Laravel has many and grouping

I can't seem to get the syntax right for this query.
Basically, I have a product that is listed, where an offer is made by a user:
So in Offer.php
public function user()
{
return $this->belongsTo('App\User', 'buyer_id');
}
public function product(){
return $this->belongsTo('App\Product');
}
And in User.php
public function offers()
{
return $this->hasMany('App\Offer', 'buyer_id');
}
Then in Product.php
public function offer()
{
return $this->hasMany('App\Offer');
}
I need to group offers by the user that has made them, and then group that again by the category of the product the offer is against.
Does anyone have ideas where I can start to group these correctly?
Try with Query Builder. Like this ( I don't know your tables`s structure):
$offers = DB::table('offers')
->joinLeft('users','users.id','=','offers.buyer_id')
->joinLeft('products','products.id','=','offers.product_id')
->groupBY('users.id','products.category_id')
->get();

How to get forienkeys data in laravel models

I am implementing chat in my application using laravel. I have these Models:
`Users: id, name `
`Pages: id, user_id(foreign_key from Users table (id)), name`
`Posts: id, page_id(foreign_key from Pages table (id)), user_id(foreign_key from Users table (id)), body`
`Comments: id, post_id(foreign_key from Posts table (id)), user_id(foreign_key from Users table (id)), body`
I need to query followings
Get all posts of user with comments and with user
Get all posts of a page with comments and with user
I am using below query to get all posts of a page, but how can I include its comments and user details;
Page::with('post')->findOrFail($pageId)->post;
EDIT
USER Model: User.php
public function page() {
return $this->hasMany('Page');
}
public function post() {
return $this->hasMany('Post');
}
Page Model: Page.php
public function user()
{
return $this->belongsTo('User');
}
public function post() {
return $this->hasMany('Post');
}
Post Model: Post.php
public function user()
{
return $this->belongsTo('User');
}
public function page() {
return $this->belongsTo('Page');
}
public function comments() {
return $this->hasMany('Comment');
}
Comment Model: Comment.php
public function user()
{
return $this->belongsTo('User');
}
public function post() {
return $this->belongsTo('Post');
}
As your relationships are not that clear to me, i can only provide some hints where to look.
Getting posts of a user with comments sounds like laravel query scopes: http://laravel.com/docs/5.1/eloquent#query-scopes
You may also query for relationship existence: http://laravel.com/docs/5.1/eloquent-relationships#querying-relations
And Lazy Eager Loading could also help to retrieve the necessary instances.

Laravel selection from pivot table

I have three tables: users, items and user_items. A user has many items and a item belongs to many users.
**Users**
id
username
password
**Items**
id
name
**User_items**
id
user_id
item_id
Models:
class User extends Eloquent {
public function items()
{
return $this->belongsToMany('Item', 'user_items', 'item_id', 'user_id');
}
}
class Item extends Eloquent {
public function users()
{
return $this->belongsToMany('User', 'user_items', 'user_id', 'item_id');
}
}
I need to select all items table, print it and highlight rows which belongs to specific user id=1.
Selection highlighted output:
What is the right way to do it (in laravel style)?
You can use it like this
public function user_items()
{
return $this->belongsToMany('User', 'user_items', 'user_id', 'item_id')->withPivot('id');
}
Like this you can access values of third table.
Some useful links-
http://www.developed.be/2013/08/30/laravel-4-pivot-table-example-attach-and-detach/
http://vegibit.com/many-to-many-relationships-in-laravel/
http://laravel.com/docs/4.2/eloquent
You can do it like this way...
class User extends Eloquent {
public function items()
{
return $this->belongsToMany('Item', 'user_items', 'item_id', 'user_id')->withPivot('id');
}
}
class Item extends Eloquent {
public function users()
{
return $this->belongsToMany('User', 'user_items', 'user_id', 'item_id')->withPivot('id');
}
}
From controller..
$user_id = 2;
Item::with(['users'=>function($q) use ($user_id){$q->where('user_id',$user_id);}])->get();
In view at the time of listing a row you can highlight the row just use a condition as each item->users is blank or not.

Categories