scooti
scooti

Reputation: 51

create list for dropdown with value from relationed table

My model Product return something like this

and I need to use lists() to create something like this:

lists("category.name", "id")

but it looks that this is impossible. How to create list for dropdown like this:

1 = electronics
2 = furniture
3 = cars
...

thanks a lot

Upvotes: 1

Views: 137

Answers (1)

MisterP
MisterP

Reputation: 467

Are you sure you did set up the right relations? Cause that is just what lists() usually does and it's far from impossible to create such a list.
It's quite hard to understand how you did design your Eloquent logic without your code, but here's how I would handle your situation, assuming you've already set up the category_product pivot table.
Given two models, Product and Category, I'd define these 2 relations cause, as far as I can get, a product can have several categories and a categories can be associated with several product :

class Product extends Model
{
   ////////
   public function categories()
    {
        return $this->belongsToMany('App\Category');
    }
   ////////
}  

and

class Category extends Model
    {
       ////////
       public function products()
        {
            return $this->belongsToMany('App\Product');
        }
       ////////
    }  

At this point, you're good to go, all you have to do in order to get your list is

Product::findOrFail($id)->categories->lists('name','id')  

This will return an array holding all the categories associated with the given product

array:[
    1 => first-category
    2 => second-category
    3 => third-category
    ....
  ]  

Note that with this set up it'll work the other way around as well, if you want to get a list of all the product that match a given category

Category::findOrFail($id)->products->lists('name','id')  

At the very top of the answer I assumed you did set up a pivot table but, if you didn't, here's a quick tip: within your products migration

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            //
        });

        Schema::create('category_product', function (Blueprint $table){
            $table->integer('category_id')->unsigned()->index();
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

            $table->integer('product_id')->unsigned()->index();
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');

            $table->timestamps();
        });
    }  

Then migrate and now everything is properly set up. Last but not least, if you are going to use this migration, you'll need to fix the relation in the model, which one is up to yours app logic: let's say the flow is create a new product -> choose the categories within the creation form, then you need to add timestamps() in the categories method

return $this->belongsToMany('App\Category')->withTimeStamps(); 

Upvotes: 1

Related Questions