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 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 relationship example

<?php

namespace App;
use IlluminateDatabaseEloquentModel;

class User extends Model
{
    public function orders() {
        return $this->hasMany(AppOrder::class);
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: php class extends two classes 
Php :: php spreadsheet styles 
Php :: php pre 
Php :: laravel create command tutorial 
Php :: foreign key string laravel 
Php :: php variable constant 
Php :: how to update dropdown value in laravel 
Php :: heredoc 
Php :: laravel find 
Php :: laravel available router methods 
Php :: php variables 
Php :: laravel email verification laravel 8 tutorial 
Php :: PHP Notice: Trying to get property of non-object 
Php :: Call to undefined function array_key_first() 
Php :: Alternatives to chmod 775 or 664 
Php :: findOneBy 
Php :: laravel - access file from storage path - alternative to symlink 
Php :: php normalize whitespace characters 
Php :: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in C:xampphtdocsJob Verificationlogin esult.php on line 38 
Php :: php auto reset score 
Php :: import csv laravel 
Php :: remove rank math next prev canonical 
Php :: generate rand password php 
Php :: text short in laravel 
Php :: set renew subscroption stripe update 
Php :: remove public in laravel 
Php :: simple php round When a parameter is passed with a specific precision value 
Php :: Drupal 9 Get taxonomy term objects by vocabulary machine name vid 
Php :: php file structure 
Php :: php default argument 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =