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 :: error_reporting(E_ERROR) 
Php :: find_in_set in laravel 
Php :: Laravel render stuff in a given environment 
Php :: query builder codeigniter 
Php :: whats the difference between using date function and DATETime in php 
Php :: ereg function in php 
Php :: php //input 
Php :: run cron job in seconds 
Php :: php array_map 
Php :: how to run php in javascript 
Php :: php array_search 
Php :: php echo statement 
Php :: php string remove last character 
Php :: PHP Notice: Trying to get property of non-object 
Php :: Merging Two Laravel Collections keeping the original keys 
Php :: https://github.com/nuxt/nuxt.js/issues/8315#:~:text=%3Chtml%20lang%3D%22ru%22%20data%2Dn%2Dhead%3D%22%257B%2522lang%2522%3A%257B%2522ssr%2522%3A%2522ru%2522%257D%257D%22%3E 
Php :: formidableforms limit only 2 submissions per user 
Php :: magento 2 add cc transportbuilder 
Php :: laravel collection zip 
Php :: how to get keys of associative array php 
Php :: laravel remove public from url htaccess 
Php :: how to show arraylist in comma separated with last and in php 
Php :: php replace all text from string with associate array values 
Php :: run laravel envoy task 
Php :: Attempt to read property "headers" on string 
Php :: php artisan seading 
Php :: Nginx + Laravel - Moving blog from subdomain to /blog 
Php :: PHP str_getcsv — Parse a CSV string into an array 
Php :: custom-taxonomy-image-option-in-admin-panel 
Php :: comment acceder à la base de données phpmyadmin sur mac ave 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =