Skip to content
Advertisement

Laravel Eloquent where statement returns no attributes

So from my previous post, I was advised to start using Eloquent models, which I did.

My end goal, is to print out specific gifts, that belongs to that specific box.

Migrations:

gift_items:

public function up()
    {
        Schema::create('gift_items', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->float('unit_price');
            $table->integer('units_owned');
        });
    }

gift_campaigns:

public function up()
    {
        Schema::create('gift_campaigns', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->foreignId('user_foreignK')->constrained('users');
            $table->integer('gift_item_count')->nullable();
            $table->string('status');
            $table->date('dispatch_date');
            $table->date('delivery_date');
        });
    }

Pivot table:

public function up()
    {
        Schema::create('campaigns_gifts', function (Blueprint $table) {
            $table->foreignId('gift_id')->constrained('gift_items');
            $table->foreignId('campaign_id')->constrained('gift_campaigns');
        });
    }

Controller:

function box($id){
        $data = Campaign::with('gifts')->where('id', $id)->get();
        return view('DBqueries.boxView', ['data'=>$data]);
    }

Error that I receive using this way:error

Seems like the updated version is trying to call the gift_campaigns table id, instead of the pivots table campaign_id.

Once again, I need that Request $id would match the pivots table campaign_id, and print out all of the gifts that this specific id holds

Advertisement

Answer

First of all as I sense the campaigns_gifts is a pivot table for campaigns and gifts having a Many-to-Many relation. You are doing it completely against the conventions of Laravel.

  • You generally do not use a Model for pivot table.
  • You generally do not fetch data from the pivot table directly. Instead use the relation on one of the related Models.

Note: Laravel does allow a Model for a Pivot, and you can query the pivot table directly, just check the documentation.

The correct way:

Pivot

Make a pivot table (that you already have) with column gift_id and campaign_id. i.e., the convention for naming keys as [table_name_singular]_[primary_key_on_table]

Model

One each model, define relationship for the other data as:

Gift.php Model:

public function campaign() {
    return $this->belongsToMany(Campaign::class, 'campaign_gift');
}

Campaign.php Model:

public function gifts() {
    return $this->belongsToMany(Gift::class,'campaign_gift'); 
}

since gift have a hasMany relation, the gifts table must contain a foreign key to campaigns table named campaign_id (same as the one on pivot).

Controller

Now in your controller:

function box($id){
    $data = Campaign::where('id',$id)->with('gifts')->get();
    return view('DBqueries.boxView', ['data'=>$data]);
}

You don’t need to tell Laravel which columns, tables etc are you referring to, as long as you follow the conventions, Laravel will magically do things that otherwise would have been much more painful.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement