Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel relations find

class Mechanic extends Model
{
    /**
     * Get the car's owner.
     */
    public function carOwner()
    {
        return $this->hasOneThrough(
            Owner::class,
            Car::class,
            'mechanic_id', // Foreign key on the cars table...
            'car_id', // Foreign key on the owners table...
            'id', // Local key on the mechanics table...
            'id' // Local key on the cars table...
        );
    }
}
Comment

laravel relations find

/**
 * Get the user's most recent order.
 */
public function latestOrder()
{
    return $this->hasOne(Order::class)->latestOfMany();
}
Comment

laravel relations find

/**
 * Get the current pricing for the product.
 */
public function currentPricing()
{
    return $this->hasOne(Price::class)->ofMany([
        'published_at' => 'max',
        'id' => 'max',
    ], function ($query) {
        $query->where('published_at', '<', now());
    });
}
Comment

laravel relations find

return $this->hasMany(Comment::class, 'foreign_key');

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

laravel relations find

<?php

namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Comment extends Model
{
    /**
     * Get the post that owns the comment.
     */
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}
Comment

laravel relations find

use AppModelsComment;

$comment = Comment::find(1);

return $comment->post->title;
Comment

laravel relations find

/**
 * Get the user's oldest order.
 */
public function oldestOrder()
{
    return $this->hasOne(Order::class)->oldestOfMany();
}
Comment

laravel relations find

/**
 * Get the user's largest order.
 */
public function largestOrder()
{
    return $this->hasOne(Order::class)->ofMany('price', 'max');
}
Comment

laravel relations find

class Project extends Model
{
    public function deployments()
    {
        return $this->hasManyThrough(
            Deployment::class,
            Environment::class,
            'project_id', // Foreign key on the environments table...
            'environment_id', // Foreign key on the deployments table...
            'id', // Local key on the projects table...
            'id' // Local key on the environments table...
        );
    }
}
Comment

laravel relations find

use AppModelsUser;

$user = User::find(1);

foreach ($user->roles as $role) {
    //
}
Comment

laravel relations find

$roles = User::find(1)->roles()->orderBy('name')->get();
Comment

laravel relations find

return $this->belongsToMany(Role::class, 'role_user');
Comment

laravel relations find

return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');
Comment

PREVIOUS NEXT
Code Example
Php :: create crud controller in laravel 5.8 
Php :: codeigniter crud generator 
Php :: create widget to display comments in wordpress 
Php :: insert into php myqsl 
Php :: php string concatenation 
Php :: How to add .active class to active menu item 
Php :: php Constant expression contains invalid operations 
Php :: php loop object keys 
Php :: laravel run function after forgot password 
Php :: create xml php 
Php :: php code generator 
Php :: laravel 8 cron job 
Php :: create symfony project 
Php :: Merging Two Laravel Collections keeping the original keys 
Php :: php convert dbf to mysql 
Php :: wherenotnull laravel 
Php :: PHP metaphone — Calculate the metaphone key of a string 
Php :: movies -inurl:(htm|html|php|pls|txt) intitle:index.of “last modified” (mp4|wma|aac|avi) 
Php :: laravel gigapay delete payout 
Php :: nested array in laravel 
Php :: membership_registration: city or town 
Php :: how to change php to html 
Php :: mysql php update sum same table 
Php :: Laravel You may determine if a template inheritance section has content using the @hasSection directive: 
Php :: php domdocument get elements one by one 
Php :: implode remove empty php 
Php :: laravel how to generate short link in laravel framework and relation with 3 model 
Php :: laravel read csv 
Php :: connecting to database and performing sql queries 
Php :: php check if cli mode 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =