我有一个包含关系的产品表:specValues
在我的产品模型中:
public function specValues(){
return $this->morphMany('App\Models\SpecValue', 'specvalued');
}
在have中的specValues中:
class SpecValue extends Model
{
use Cachable;
protected $table = 'specvalued';
protected $fillable = ['fi
假设我们有四个表:标签、帖子、回复和post_tag
tags table:
id tag
1 tag A
2 tag B
3 tag C
posts table:
id post_content
1 some text A
2 some text B
3 some text C
post_tag table: (for many-many relationship between posts and tags)
post_id tag_id
1 1
1 2
2 3
replies table:
id p
我和拉拉的关系有问题。
我有三个模特:
城市-(HasMany)->诊所-(HasMany)->Stock
股票-(BelongsTo)->Clinic-(BelongsTo)->City
我需要所有有股票的城市,看起来像股票->城市。我可以编写sql查询:
SELECT ct.id, ct.name
FROM CfgCity as ct
RIGHT JOIN lr_clinics AS cl ON(ct.id=cl.city_id)
RIGHT JOIN lr_clinic_stocks AS st ON(cl.id=st.clinic_id)
WHERE st
我有一个返回表的查询。我也想从另一个表中返回一个与之相关的样式列表。我试过很多方法,但我找不到一个管用的。有什么建议吗? 查询: $data = YarnOrderLines::with('yarn_orders')
->with('yarns')->get(); //I would like to pull in YarnOrderLineStyles YarnOrderLines: protected $fillable = [
'yarn_orders_id',
'yarn_id'
我的数据库里有4张表:
表1:类别
---|------
id | name
---|------
1 | Cars
在“类别”模型类中,我定义了以下关系:
class Category {
public function fields() {
return $this->belongsToMany('App\Field');
}
}
表2:字段
id | name
---|-------
1 | Make
在“Field”模型类中,我定义了以下关系:
class Field {
public function categori
我有两个表要保存数据。其中一个有外键,所以我有一对多的关系。但是,我不明白如何同时将数据保存到两个表中。我有一个查询,它包含一个表的数据,另一个查询应该附加到第一个表。
这是主要的模式
class Site extends Model
{
protected $fillable = ['path', 'site_link'];
public $timestamps = false;
public function features() {
return $this->hasMany('App\SiteFe
如何在产品表中显示“类别名称”而不是"category_id"?
我已经尝试将任何解决方案结合起来。但还是解决不了这个问题。我想听听这里的大师们的其他建议。
范畴模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Kategori extends Model
{
use HasFactory;
protected $casts = [
如何通过模型从第三个表中获取数据?
我有模型Prototype和PrototypeField模型。
其中,Prototype是:
id | name
其中,PrototypeField是:
prototype_id | field_id
还有一个表Field,其中包含字段的名称:
id | name
如何通过模型'Prototype‘从表Field中获取名称,其中Prototype通过以下方式与PrototypeField相对:
Prototype.id = PrototypeField.prototype_id
因此,Prototype可以有一个或多个PrototypeField。
我真的需要在以下方面的帮助!我正在努力理解hasOneThrough是如何工作的。
用户表:
id = belongs_to (公司id)
公司表:
id = country_id
国家表:
id
所以我想知道通过公司用户的国家ID .在用户模型中,我正在尝试以下内容:
public function country() {
return $this->hasOneThrough(
'App\Countries',
'App\Companies',
'count
我在数据库里有几张桌子。我尝试在QuizSubmissionController中创建关系,以获得未回答提交的问题。我能写一些像$quiz_submission->quiz->questions()->whereDoesntHave('answers')或whereNotExist('submission_answers')这样的东西吗?
我在这里的亲戚:
// Quiz Class
public function category()
{
return $this->belongsTo(Category::class);
MySQL表:
- category
- unit
- category_unit (many to many)
- category_id
- unit_id
Laravel 5型号:
<?php
class Unit extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'unit';
}
class Category extends Model
{
我正在尝试为类别实现一个可变形的表,目前我有以下内容。
// Snippet Table
- id
- title
- body
// Post Table
- id
- title
- body
// Category Table
- id
- name
我希望能够将帖子和代码片段修改为只有一个类别,如下所示:
// Categorizable Table
- category_id
- categorizable_id
- categorizable_type
对于这个可分类的表,我必须有另一个模型吗?或者有一种方法可以在不使用其他模型的情况下设置它?
到目前为止,我有这个
class
我是拉勒维尔的初学者。我正在尝试根据外键显示所有表记录。我面临的问题是,我的Travel_offering表有两个列,beginning_location和Ending_location都是外键,引用到要检索两个记录的location表。我知道关系,也就是属于和有很多的关系。但是,对于引用到单个表的多个列,我无法做到这一点。
travel_offerings表
class CreateTravelOfferingsTable extends Migration
{
public function up()
{
Schema::create('travel
我在db有两个表,一个是users,它简单地包含网站的用户信息,另一个是tags,它包含一些用户可以从中选择的哈希标签。
(就像用户可以选择多个标记,如php、javascript等)
因此,为了在这两者之间建立这种关系,我将其添加到User模型中:
public function tags()
{
return $this->belongsToMany(Tag::class);
}
另外,这个模型是Tag模型的:
public function users()
{
return $this->belongsToMany(Us
我有两张桌子。
userspayroll_management
现在,我想查询哪个id不在payroll_management表中。
这是我的SQL查询。
select id from users where id not in (SELECT user_id FROM payroll_management)
如何用Laravel编写这个查询?
我正在为一个社交应用程序工作,我需要显示带有标签用户的提要,哈希标签,如计数、评论计数、评论和评论用户的详细信息。
提要与普通提要略有不同,这是数据库结构。
用户表:
id username email image
电影表:(管理员将添加电影,用户将按标题搜索影片)
id title image description
评级表:(这将是一个提要,a用户已经对x电影、while rating user will tag to another user进行了评级,并使用了一些hash tags)
id movie_id user_id rating description
rating_tag表:
我是拉拉维尔的新手,但到目前为止我还是很喜欢的。但是,我想为登录的用户检索数据,但我不知道如何做到这一点。
我有几个表,但我将保持它的基本,我有一个projects表和一个users表,我已经在模型中定义了这两个表之间的关系如下:
user.php
public function projects() {
return hasMany('project');
}
project.php
<?php
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Project extends Elo
如何在laravel 5中使用eloquent从3表中获取一些数据?我有3张桌子,团队,董事会,用户
团队模型:
public function member()
{
return $this->belongsToMany(User::class)
->withPivot('status','user_role')
->withTimestamps();
}
public function boards()
{
return $this->belongsT
例如,我有3个表: Table A:
id
name Table B
id
name
tableA_id Table C
id
name
tableB_id 我需要在Table中显示表A中的所有数据,并在table C中显示名称: id | name | TableC name 我可以这样写get name of tableC的代码: $data = DB::table('tableA')
->join('tableB','tableB.tableA_id','=','t
在Laravel中查询这种关系的方法有哪些?
表:
用户
角色
特权
其中:
每个角色都有一组特权。
每个用户可以属于多个角色。
用户通过角色拥有许多特权。
用户和角色通过中间表进行关联。所以角色表上没有user_id列。
实际上,我想做的事情是:
User()->Roles()->Privileges()->get()
但我不确定如何做到这一点
DB结构更新:
USERS
id
name
email
ROLES
id
role
USERS_ROLES
id
user_id (fore
我的数据库表结构如下:
教室
id (int) PK
名称(varchar)
ClassTeacherCourse
id (int) PK
id_classroom (int) FK
id_teacher (int) FK
id_course (int) FK
课程
id (int) PK
course_name (varchar)
教师
id (int) PK
teacher_name (varchar)
到目前为止,我只需要调用ClassRoom表& ClassTeacherCourse,如下所示:
$clas