mysql - 如何在 Laravel 5.2 中将项目 id 发送到数据库

标签 mysql laravel-5

我需要插入一些显示在我的网络浏览器地址栏中的项目 ID。我正在使用 Laravel 5.2

lacalhost:8000/project/06

我需要将这个数字 06 插入到 gantt_task 表的 project_id 列中。

这是我的甘特 Controller

<?php
namespace App\Http\Controllers;
use App\GanttTask;
use App\GanttLink;
use Dhtmlx\Connector\GanttConnector;

class GanttController extends Controller
{
    public function data() {
        $connector = new GanttConnector(null, "PHPLaravel");
        $connector->render_links(new GanttLink(), "id", "source,target,type");
        $connector->render_table(new GanttTask(),"id","start_date,duration,text,progress,parent,project_id");
    }

}

这是我的甘特任务模型

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class GanttTask extends Model
{
    protected $table = "gantt_tasks";
    public $primaryKey = "id";
    public $timestamps = false;
}


 can you give me some solutions?

最佳答案

解决方案如下:

Controller :

<?php
namespace App\Http\Controllers;
use App\GanttTask;
use App\GanttLink;
use Dhtmlx\Connector\GanttConnector;

class GanttController extends Controller
{
    public function data(Request $request, $project_id) {
        $connector = new GanttConnector(null, "PHPLaravel");
        $connector->render_links(new GanttLink(), "id", "source,target,type");

        // Passing as argument project_id variable to model constructor
        // new GanttTask(compact('project_id'))  will create object
        // but will not save to database, 
        // so You can use: 
        // GanttTask::create(compact('project_id'))  to push to db
        $connector->render_table(new GanttTask(compact('project_id')),"id","start_date,duration,text,progress,parent,project_id");
    }

}

型号:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class GanttTask extends Model
{
    protected $table = "gantt_tasks";
    public $timestamps = false;

    // add fillable to be able to set arguments in model constructor
    protected $fillable = [
      'start_date', 
      'duration',
      'text', 
      'progress',
      'parent',
      'project_id'
   ];
}

关于mysql - 如何在 Laravel 5.2 中将项目 id 发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40001459/

相关文章:

mysql - 如何将 mysql 查询转换为 laravel 5?

php - Laravel 5 withInput old 总是空的

php - MYSQL 事件正在运行但不起作用

php - Laravel 5 使用 Ajax 发送密码重置链接

laravel-5 - 在计算机之间移动 Laravel 项目

MySQL正则表达式选择字符串中的最后一个整数

php - 拉维尔 5 : View Composer and Service Provider not working

MySQL 单表扫描更新

php - 初始化mysql目录报错

mysql - 统计有工作或正在类的囚犯人数