lundi 31 octobre 2016

How to make relationship between two models and update the database

It's me again but now I need some more help from you, I have some version of shopping cart and the whole idea is when I click on the product button the database to be update with the informations which user(id) has order that product(id). First I make orders table with informations(id, product_name, price), other table is users table (id, user_name) and the final table user_order table(id, user_id, order_id);

After that I make two models, one is the Orders model for the products and the second one is the User model. The relationship that I want to make is between this two models.

User model function:

public function orders(){
        return $this->belongsToMany('App\Orders', 'user_order', 'user_id', 'order_id');
       // return $this->belongsToMany('App\Orders');
    }

Orders model function:

public function users(){
      return $this->belongsToMany('App\User', 'user_order', 'order_id', 'user_id');
        //return $this->belongsToMany('App\User');
   }

This is the main html where I show all products with the submit button:

 <div class="row">
                    @foreach($orders as $order)
                    <div class="col-sm-4 col-lg-4 col-md-4">
                        <div class="thumbnail">
                            <img src="http://ift.tt/1MdjS7v" alt="">
                            <div class="caption">
                                <h4 class="pull-right"></h4>
                                <h4><a href="#"></a>
                                </h4>
                                <p>See more snippets like this online store item at <a target="_blank" href="http://ift.tt/2eelEin">Bootsnipp - http://ift.tt/2f93Qmr;
                            </div>
                            <div class="ratings">
                                <p class="pull-right">15 reviews</p>
                                <p>
                                    <span class="glyphicon glyphicon-star"></span>
                                    <span class="glyphicon glyphicon-star"></span>
                                    <span class="glyphicon glyphicon-star"></span>
                                    <span class="glyphicon glyphicon-star"></span>
                                    <span class="glyphicon glyphicon-star"></span>
                                </p>
                            </div>
                            <div class="btn-group center">

                                <a href="" name="req" class="btn btn-warning">Order this product!</a> 
                            </div> 
                            <br>
                        </div>
                    </div>
                    @endforeach

The whole idea is how to make function after submiting the button the table user_order to be updated with the id of the user, and id of the product.

Any help?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire