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 get country code from country name 
Php :: session_regenerate_id 
Php :: in array php 
Php :: update url wordpress 
Php :: php epoch conversion 
Php :: php inline if condition date time 
Php :: CHECKING IF FILE IS EMPTY IN PHP 
Php :: eloquent insert into select 
Php :: where is in array laravel 
Php :: php get first two paragraphs 
Php :: multe data on database laravel 
Php :: laravel 8 resource 
Php :: clear cache using laravel controller 
Php :: laravel route pattern 
Php :: php str starts with 
Php :: laravel migration table softdeletes 
Php :: show uploaded image in php 
Php :: php return multiple values 
Php :: how to create a php website 
Php :: laravel admin multi images 
Php :: Override the route parameter names 
Php :: convert to string php 
Php :: cara membuat koneksi php 
Php :: validar tipo de imagen php 
Php :: extract in php 
Php :: intellisense in visual studio code for php-oop 
Php :: what is composer in laravel 
Php :: assocititive multi array compare php 
Php :: last index of array in laravel 
Php :: laravel downgrade php version 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =