Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel.com 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 :: pengulangan php 
Php :: To fetch the soft deleted user, you should use withTrashed 
Php :: cout post on category in controller laravel 
Php :: how can i get input id in laravel 8 
Php :: custom-taxonomy-image-option-in-admin-panel 
Php :: preg match apache log file 
Php :: Error when uploading image into phpmyadmin using PDO in php 
Php :: cant use migrate with sail laravel 
Php :: $order- date 
Php :: To enqueue css & js quickly 
Php :: php execute script wait for response 
Php :: appserviceprovider laravel share common settings for all controllers 
Php :: Obtener rol de usuario registrado en WordPress 
Php :: functions file erased wordpress 
Php :: wp wc php sort products archive cheapest price 
Php :: -inurl:(htm/html/php/pls/txt) intitle:index.of "last modified" (mp4/wma/aac/avi) 
Php :: laravel migration unknown column type double requested 
Php :: model coomad laravel 
Php :: sendinblue send email 
Php :: [name] 
Php :: how to get count of rows in a table in laravel query 
Php :: download xampp php 5.3 for windows 7 64 bit 
Php :: woocommerce validar campos personalizados en el checkout 
Php :: PHP ord — Convert the first byte of a string to a value between 0 and 255 
Php :: php receive get 
Php :: $_FILES image dimensions 
Php :: vscode php debugger change value 
Php :: add image thumb on checkout woo 
Php :: php objects 
Php :: how to use php in laravel blade 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =