Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel hasmany

<?php
 
namespace AppModels;
 
use IlluminateDatabaseEloquentModel;
 
class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
      return $this->hasMany(Comment::class);
      //return $this->hasMany(Comment::class, 'foreign_key');
      //return $this->hasMany(Comment::class, 'foreign_key', 'local_key');
    }
}

// Other
use AppModelsPost;
 
$comments = Post::find(1)->comments;
 
foreach ($comments as $comment) {
    //
}
// Other
$comment = Post::find(1)->comments()
                    ->where('title', 'foo')
                    ->first();

Comment

laravel hasMany relationship

// hasMany
$this->hasMany(Model::class);
// invers 
$this->belongsTo(Model::class);
Comment

laravel Use hasMany to create Many

 $post = Post::find(1);
 $post->comments()->saveMany([
	 new Comment(['message' => 'First comment']),
	 new Comment(['message' => 'Second comment'])
]);
Comment

PREVIOUS NEXT
Code Example
Php :: laravel eloquent orderby 
Php :: php add element to array 
Php :: laravel 8 404 status 
Php :: resource controller artisan command 
Php :: get age in months php 
Php :: test laravel scheduler 
Php :: settimezone in php 
Php :: componentes blade laravel attributes merge 
Php :: php intval 
Php :: show only 3 initial letter of month in php 
Php :: parse data from xml CDATA php 
Php :: make project in laravel 7 
Php :: php html to text 
Php :: laravel select multiple value in form edit 
Php :: symfony get path to route 
Php :: php rotate image 
Php :: format date laravel timestamp view 
Php :: laravel insert array 
Php :: laravel generate unique db token 
Php :: how to create access token in laravel 
Php :: cviebrock/eloquent-sluggable 
Php :: laravel route multiple methods 
Php :: laravel query builder select 
Php :: loop foreach laravel with number 
Php :: array reverse php 
Php :: php showing code in browser 
Php :: wordpress disable block styles 
Php :: php class extends exception 
Php :: define int variable in php 
Php :: laravel resource set status code 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =