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 :: download image from mysql using php 
Php :: php sqlite last insert id 
Php :: create array of zeros php 
Php :: php xpath attribute exact 
Php :: laravel automatically encrypt model atribute 
Php :: onclick on image php 
Php :: php function use 
Php :: init curl 
Php :: php base58 decode 
Php :: php += 
Php :: Laravel catch TokenMismatchException 
Php :: Logging a Massage php 
Php :: how to get post by comment in laravel 
Php :: check dir php 
Php :: publish spatie/permission 
Php :: The last ship -inurl:(htm/html/php/pls/txt) intitle:index.of "last modified" (mp4/wma/aac/avi) 
Php :: how to create module in laravel 
Php :: restart php service windows 
Php :: woocommerce_product_query 
Php :: laravel package development 
Php :: how do i use php read excel file 
Php :: laravel @env 
Php :: php //input 
Php :: error handling in laravel 
Php :: woocommerce custom payment process method 
Php :: php check empty variable 
Php :: php leggere file txt riga per riga 
Php :: php how to check if user has a role on login 
Php :: php pass byref 
Php :: Problem getting updated value from child component to the parent component in a Laravel 9 with Vue 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =