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

$user->posts()->where('active', 1)->get();
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 :: Problem getting updated value from child component to the parent component in a Laravel 9 with Vue 
Php :: register style wordpress 
Php :: php file handling 
Php :: php auto reset score 
Php :: wp wc php use comma separated thousands 
Php :: php artisan tinker get records 
Php :: woocommerce php same skus 
Php :: modifier laravel migration to add a comment a column (MySQL/PostgreSQL) 
Php :: limit query laravel 
Php :: how to import Yomo in larave; 
Php :: html add div around certain iframe php 
Php :: json encode unset array 
Php :: <?php function find total( $my_list ) { //Insert your code here } ? 
Php :: array with key value pair php 
Php :: deploy php composer with vercel.com 
Php :: pregmatch 
Php :: Check php and wordpress version before activation 
Php :: php load select page from url 
Php :: validate unique or equal 
Php :: Right triangle start pattern of star 
Php :: php file structure 
Php :: logout php mysql 
Php :: @sectionMissing 
Php :: Laravel database insert with combining array and string 
Php :: wordpress get posts by multiple authors 
Php :: wc-notice-functions.php on line 140 
Php :: calculate age from date of birth php 
Php :: separate powershell commands on one line 
Php :: Lity in Wordpress 
Php :: Shorten long numbers to K/M/B? 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =