Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel relationship

1-1
public function phone()
    {
        return $this->hasOne('AppPhone');
    }

 public function user()
    {
        return $this->belongsTo('AppUser');
    }

1-many
public function phone()
    {
        return $this->hasMany('AppPhone');
    }

 public function user()
    {
        return $this->belongsTo('AppUser');
    }

many-many
  // migrations/****_**_**_******_create_category_post_table.php
Schema::create('category_post', function(Blueprint $table) {
    $table->integer('category_id')->unsigned();
    $table->integer('post_id')->unsigned();
    $table->primary(['category_id', 'post_id']);
    $table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
    $table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
});


// Post.php
public function categories()
{
    return $this->belongsToMany(Category::class);
}

// Category.php
public function posts()
{
    return $this->belongsToMany(Post::class);
}

In post.php, use this relation: public function categories(){ return $this->belongsToMany(Category::class, 'category_post', 'post_id', 'category_id'); }

//controller
    $post = new Post();
    $post->title = $request->title;
    $post->body = $request->body;
    $post->categories()->attach($request->categories_id);

https://laracasts.com/discuss/channels/laravel/how-to-seed-db-in-pivot-table-laravel
https://laraveldaily.com/pivot-tables-and-many-to-many-relationships/
https://stackoverflow.com/questions/38746613/how-to-insert-a-post-with-multi-category-and-with-multi-column-deferent-category


query builder
  https://stackoverflow.com/questions/33449387/laravel-creating-different-views-from-query/33449507#33449507
Comment

relationship in laravel

N + 1
return new SongsCollection(Song::with('album')->get());

'songs' => SongResource::collection($this->whenLoaded($this->songs))
Comment

laravel eloquent relationships

<?php

namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Project extends Model
{
    /**
     * Get all of the deployments for the project.
     */
    public function deployments()
    {
        return $this->hasManyThrough(Deployment::class, Environment::class);
    }
}
Comment

eloquent relationships

$roles = AppUser::find(1)->roles()->orderBy('name')->get();
Comment

laravel relationship

$comment = Post::find(1)->comments()
                    ->where('title', 'foo')
                    ->first();
Comment

laravel relationship

$model->relation; // result of the relation, ie. null/model for x-1 relations or collection for x-m
$model->relation(); // relation object
Comment

laravel eloquent relationships

?
<?php
 
use IlluminateDatabaseSeeder;
use IlluminateDatabaseEloquentModel;
 
class DatabaseSeeder extends Seeder {
 
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();
  
        for ($i = 1; $i < 11; $i++) {
            DB::table('categories')->insert(['name' => 'Category ' . $i]);
            DB::table('periods')->insert(['name' => 'Period ' . $i]);
            DB::table('countries')->insert(['name' => 'Country ' . $i]);
        }
  
        for ($i = 1; $i < 21; $i++) {
            DB::table('themes')->insert(['name' => 'Theme ' . $i]);
            DB::table('editors')->insert(['name' => 'Editor ' . $i]);
            DB::table('formats')->insert(['name' => 'Format ' . $i]);
            DB::table('contacts')->insert(['phone' => '00 00 00 00 00', 'editor_id' => $i]);
            DB::table('cities')->insert(['name' => 'City ' . $i, 'country_id' => rand(1, 10)]);
        }
  
        for ($i = 1; $i < 21; $i++) {
            DB::table('authors')->insert(['name' => 'Author ' . $i, 'city_id' => rand(1, 20)]);
        }
  
        for ($i = 1; $i < 51; $i++) {
            $choice = array('AppModelsFormat','AppModelsEditor');
            DB::table('books')->insert([
                'name' => 'Book ' . $i,
                'bookable_id' => rand(1, 20),
                'theme_id' => rand(1, 20),
                'bookable_type' => $choice[rand(0,1)]
            ]);
        }
 
        for ($i = 1; $i < 21; $i++) {
            $number = rand(2, 8);
            $items = [];
            for ($j = 1; $j <= $number; $j++) {
                while(in_array($n = rand(1, 50), $items)) {}
                $items[] = $n;
                DB::table('author_book')->insert(array(
                    'book_id' => $n,
                    'author_id' => $i
                ));
            }
        }
  
        $items = [];
        for ($i = 1; $i < 81; $i++) {
            $choice = array('AppModelsCategory','AppModelsPeriod');
            do{
                $t1 = rand(1, 20);
                $t2 = rand(1, 20);
                $t3 = $choice[rand(0,1)];
            } while(in_array($t1 . $t2 . $t3, $items));
            $items[] = $t1 . $t2 . $t3;
            DB::table('themables')->insert([
                'theme_id' => $t1,
                'themable_id' => $t2,
                'themable_type' => $t3
            ]);
        }
    }
 
}
Comment

PREVIOUS NEXT
Code Example
Php :: add character after x characters in php 
Php :: command to create middleware in laravel 
Php :: show uploaded image in php 
Php :: laravel db query log string replacements 
Php :: get element by index array php 
Php :: how to install phpmyadmin on windows 10 
Php :: codeigniter 3 image upload 
Php :: WordPress Plugin Definition 
Php :: timezones php 
Php :: sha256 php cantidad caracteres 
Php :: install all php extensions ubuntu 20.04 
Php :: Write a php program to perform sum of two numbers 
Php :: Eager realationship in laravel 
Php :: session array 
Php :: woocommerce function traduccion label 
Php :: PHP sha1 — Calculate the sha1 hash of a string 
Php :: create resource controller in admin folder laravel 
Php :: laravel custom validation 
Php :: accept method in jquery 
Php :: remove jquery wp 
Php :: make php website https 
Php :: log magento 1 
Php :: php trait example 
Php :: laravel websockets 
Php :: render html data from db laravel 
Php :: laravel check if model has relation 
Php :: php return more than one value 
Php :: mysqli connect error 
Php :: magento 2 add in static block 
Php :: Web servers supported by php 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =