使用关系Laravel访问数据 [英] Acces to data using relationships Laravel

查看:61
本文介绍了使用关系Laravel访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Laravel上的关系访问控制器功能中的数据.

i want to access to data in the function of the controller using relationships on Laravel.

我将首先解释我的代码:

I will explain first my code:

我有3个表,项目,客户端和client_project.

I have 3 tables, projects, client and client_project.

此刻,client_project没有任何关系,我只是手动添加它.

At this moment, client_project don't have any relationship, i just add it manually.

现在,我想在laravel上使用关系,但这有点令人困惑(至少对我而言).

Now i want to use relationships on laravel, but it's a bit confusing (for me at least).

我认为项目和客户表的代码不是太重要,只是具有像主键之类的ID,还有一些其他字段.

I think it's not too much important the code of projects and clients table, just have id like primary key, and some fields more.

我对client_project的迁移如下:

My migration of client_project looks like here:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateClientProjectTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('client_project', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('client_id');
            $table->integer('project_id');
            $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('client_project');
    }

}

Client_Project模型如下:

Client_Project model looks like here:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Client_Project extends Model
{
protected $fillable = ['client_id','project_id'];

public $table = 'client_project';


    public function clients()
    {
        return $this->hasMany('App\Models\Project');
    }
    public function projects()
    {
        return $this->hasOne('App\Models\Client');
    }   

}

一个客户端可以有多个项目,但是一个项目只能由一个客户端创建.我认为人际关系被宣告良好.

One client can have more than one project, but one project is only created by one client. I think relationships are declared good.

(起初,我认为与关系不需要创建client_project表),但是我认为这是一个错误的主意.我也想用这张桌子做.

(At first, i think with relationships i don't need to make the client_project table), but i think that's a wrong idea. I want to make it with this table too.

因此,现在的问题是当我尝试调用函数控制器时,我认为我可以使用por示例访问数据:

So, now, the problem it's when i try to call on the function controller, i think i can access to data using por example:

App\Models\Project::find(1),就像laravel的文档所说.

App\Models\Project::find(1), like doc of laravel says.

函数是这样的:

$client = new Client();
$client->name = $request->input("nameClient");
$client->slug = $request->input("slugClient");
$client->priority = $request->input("priorityClient");
$client->save();

$client_project = new Client_Project();
$client_project->client_id = App\Models\Client::max('id');
$client_project->project_id = App\Models\Projects::max('id');
$client_project->save();

客户端的一部分正在工作.我只是拿一些输入的值,然后创建一个新的输入.

The part of the client, is working. I just take the value of some inputs and i create a new one.

问题出在$ client_project上.我想让它充满活力.我创建了客户端和项目,我的代码获得了项目的最后一个(id较大)和最后一个(id较大). 如何使用关系访问它们? 也许需要对client_project进行编辑迁移,然后将一些密钥放入project_id或client_id中?

The problem is with $client_project. I want to make it dynamic. I create the client and the project, and my code get the last one(the bigger id), and the last one(the bigger id too) of projects. How can i access them using relationships? Maybe need edit migration of client_project and put some key in project_id or client_id?

如果需要更多信息,请询问. 任何帮助将不胜感激!

If need more information, please ask it. Any help will be appreciated!

推荐答案

这是您的问题.您正在以良好的方式为客户端和项目创建数据透视表,以便可以将多个项目附加到任何客户端.这是与模型的关系.

Here is your ans. You are going good way you created pivot table for client and project so you can attached as many projects to any client. Here is relationship with model.

客户端模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Client extends Model
{
    public function projects() {
        return $this->belongsToMany(Project::class,'client_project');
    } 
}   

项目模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Projects extends Model
{



    public function client() {
        return $this->belongsToMany(Client::class,'client_project');
    } 


}   

?>

对于保存项目ID,请按照以下方法在控制器方法中使用

For Save project id use following way in controller method

    $client = new Client();
    $client->name = $request->input("nameClient");
    $client->slug = $request->input("slugClient");
    $client->priority = $request->input("priorityClient");
    $client->save();
    $project = new Project();
//include fields as per your table 

    $project->save();

    $client->projects()->attach($project->id);

.

这篇关于使用关系Laravel访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆