laravel relation one to one
// User model
public function phone()
{
return $this->hasOne(Phone::class);
}
// User controller
$phone = User::find(1)->phone;
laravel relation one to one
// User model
public function phone()
{
return $this->hasOne(Phone::class);
}
// User controller
$phone = User::find(1)->phone;
laravel one to one relationship
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the phone associated with the user.
*/
public function phone()
{
return $this->hasOne(Phone::class);
}
}
whereHas site:https://laravel.com/docs/
use Illuminate\Database\Eloquent\Builder;
// Retrieve posts with at least one comment containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) {
$query->where('content', 'like', 'code%');
})->get();
// Retrieve posts with at least ten comments containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) {
$query->where('content', 'like', 'code%');
}, '>=', 10)->get();
laravel one to many relationship example
$user->roles()->attach($roleIds);
$user->roles()->detach($roleIds);
$user->roles()->sync($roleIds);
$user->roles()->toggle($roleIds);
laravel one to many relationship
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Get the comments for the blog post.
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
}
laravel eloquent relationships
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
/**
* Get all of the deployments for the project.
*/
public function deployments()
{
return $this->hasManyThrough(Deployment::class, Environment::class);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us