Answers for "belongs to many laravel"

PHP
7

belongs to many laravel

For example, a blog post may have an infinite number of comments. And a single
comment belongs to only a single post  

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany('App\Models\Comment');
    }
}

class Comment extends Model
{
    public function post()
    {
        return $this->belongsTo('App\Models\Post');
    }
}
Posted by: Guest on October-16-2020
5

belongs to many laravel

// Detach a single role from the user...
$user->roles()->detach($roleId);

// Detach all roles from the user...
$user->roles()->detach();
Posted by: Guest on September-15-2020
3

laravel belongs to

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
    /**
     * Get the user that owns the phone.
     */
    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }
}
Posted by: Guest on November-01-2020
1

belongs to many laravel

use App\Models\User;

$user = User::find(1);

$user->roles()->attach($roleId);
Posted by: Guest on February-13-2021
0

laravel how to query belongsTo relationship

$movies = Movie::whereHas('director', function($q) {
    $q->where('name', 'great');
})->get();
Posted by: Guest on October-12-2020

Code answers related to "belongs to many laravel"

Browse Popular Code Answers by Language