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

hasmany relationship with created at in laravel example

return $this->hasMany('AppComment', 'foreign_key');

return $this->hasMany('AppComment', 'foreign_key', 'local_key');
Comment

hasMany relation in laravel

// function in model 
public function hasManyFunction()
{
  return $this->hasMany('AppModelsmodelName', 'targetedId');
}

// get data with hasMany relation in controller
$data = Post::with('hasManyFunction')->find(1)->;
Comment

hasmany relationship in laravel

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

Laravel relationship HasMany

$users = User::where('id' ,7)->with('getAddress')->get();
Comment

PREVIOUS NEXT
Code Example
Php :: laravel without global scope 
Php :: create a new project from cli laravel 
Php :: wp plugins action link 
Php :: laravel cache remember 
Php :: laravel routes return view in web.php 
Php :: install logger bundle in symfony project 
Php :: laravel detach 
Php :: Redirect to external domain in Laravel 
Php :: a facade root has not been set laravel 7 
Php :: php print array nice format 
Php :: laravel eloquent group by week 
Php :: transfer file using file_get_content 
Php :: Day of Week Using carbon library 
Php :: simplexml_load_string alternative php 
Php :: php clear cache 
Php :: css not working in live laravel project 
Php :: laravel multiple paginate 
Php :: Group by not working - Laravel 
Php :: replace key in php 
Php :: laravel return response view 
Php :: laravel return validation errors 
Php :: end foreach loop 
Php :: get nearby from longitude and latitude in laravel 
Php :: yii2 clear schema cache 
Php :: drop column table in migration if exist in laravel 
Php :: php number to words 
Php :: php html template if conditions 
Php :: laravel log reader 
Php :: php check if all array values are the same 
Php :: route group in laravel 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =