Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel get data in pivot table

// In model User.php, add withPivot; for ex :
public function customer(){
    return $this->belongsToMany('role')
                ->withPivot('type'); // 'type' is from pivot table user_role
}

// then access the field with ->pivot; for ex:
$current_user->customer->pivot->type
Comment

laravel pivot table model

/*Model - Service*/
public function customer(){
    return $this->belongsToMany('customer')->withPivot(
        'start_date',
        'stop_date',
        'rem_date',
        'due_date',
        'status'
        );
}

/*Model - customer*/
public function services(){
    return $this->belongsToMany('Service')->withPivot(
        'start_date',
        'stop_date',
        'rem_date',
        'due_date',
        'status'
        );
}

////These following relations didnt workout
/*Model - custserv*/ //uses the pivot table customer_service//
public function staff(){
    return $this->belongsToMany('Staff');
}

/*Model - Staff*/
public function custservs(){
    return $this->belongsToMany('Custserv');
}

/*schema for pivot table 'staff' and 'Custserv' */
Schema::create('customer_service_user', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('customer_service_id')->unsigned()->index();
        $table->foreign('customer_service_id')->references('id')->on('customer_service')->onDelete('cascade');
        $table->integer('staff_id')->unsigned()->index();
        $table->foreign('staff_id')->references('id')->on('staff')->onDelete('cascade');
        $table->timestamps();
    });
Comment

pivot table in laravel 9

return new class extends Migration {
    public function up()
    {
        Schema::create('blog_category', function (Blueprint $table) {
            $table->foreignIdFor(Blog::class)->constrained()->onDelete('cascade');
            $table->foreignIdFor(Category::class)->constrained()->onDelete('cascade');
            $table->primary(['blog_id', 'category_id']);

            $table->index('blog_id');
            $table->index('category_id');
        });
    }
Comment

PREVIOUS NEXT
Code Example
Php :: php artisan insert user with tinker 
Php :: ternaire echo isset php 
Php :: laravel tinker hash password 
Php :: set only allow post request to a page - php 
Php :: symlink.php laravel 
Php :: route codeigniter 
Php :: laravel parse markdown 
Php :: get post id contact form 7 
Php :: import faker in laravel 
Php :: add top menu bar in wordpress 
Php :: wordpress enqueue js 
Php :: get specific word from string php 
Php :: php radians to degrees 
Php :: pdo connection 
Php :: pmxi_gallery_image 
Php :: laravel chunk 
Php :: acf get image id sub_field 
Php :: how to add column to database in laravel 
Php :: laravel vue browser cache auto clear 
Php :: php get index of string 
Php :: find php ini 
Php :: laravel get second last record 
Php :: googlee traduction 
Php :: get php ini config from terminal 
Php :: signup form in php 
Php :: array join pgp 
Php :: get data from csv file in php and print in table 
Php :: PHP MySQL Insert Multiple Records 
Php :: construct php 
Php :: wc get product category image 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =