php - 拉维尔 4 : get current id to put id's from 2 tables in a third table

标签 php mysql laravel-4 many-to-many eloquent

在 laravel 4 中,我想获取我正在制作的乐队的当前 ID,这样我就可以将它与我要附加乐队的节日 ID 一起存储。如何使用 Eloquent 在 Laravel 4 中获取当前 ID?

public function store()
    {
        $input = Input::all();

        $rules = array(
            'band_name'      => 'required',
            'band_members' => 'required',
            'band_genre' => 'required',
            'band_startdate' => 'required'
        );
        $validator = Validator::make($input, $rules);

        if($validator->passes())
        {
            $bands = new Band();

            $band_festivals = new BandFestival();

            //here I want the current id of the band i'm making to store it in the band_festivals table
            $band_festivals->band_id = Input::get('band_id'); 

            $band_festivals->festival_id = Input::get('festival_id');

            $bands->band_id = Input::get('band_id');
            $bands->band_name = $input['band_name'];
            $bands->band_members = $input['band_members'];
            $bands->band_genre = $input['band_genre'];
            $bands->band_startdate = $input['band_startdate'];

            $bands->save();

            $band_festivals->save();

            Session::flash('message', 'Successfully created festival!');
            return Redirect::to('bands');
        }
        else {
            return Redirect::to('bands/create')->withInput()->withErrors($validator);
        }
    }

最佳答案

先保存乐队...

$bands->save();

然后使用 $bands 模型获取您的 $band_festivals 模型的 ID:

$band_festivals->band_id = $bands->id; 
$band_festivals->save();

关于php - 拉维尔 4 : get current id to put id's from 2 tables in a third table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25271544/

相关文章:

mysql - 在同一台机器上安装多个 MySQL 集群 - CentOS 7

javascript - Node.js + MySQL -> 从外部 server.js 进行数据库查询?

mysql - Laravel 数据库模式中的 MediumBlob

PHP DomDocument 输出没有 <?xml 版本 ="1.0"编码 ="UTF-8"?>

php - 是否有可能在长时间运行的 PHP 进程中捕获/处理/检测系统时间何时更改?

javascript - 将数据推送到客户端

php - 在 PHP 上解析来自 base64 的 csv 文件

php - 如何在数据库中绘制名称

javascript - 使用 Laravel 4 将 JSON 编码的 PHP 变量发送到 JS

php - 如何为 Laravel 4 包创建命令?