dongmi4035 2016-10-25 14:06
浏览 37
已采纳

MYSQL在PHP中选择查询

I am new to Laravel and I am trying to receive the clasa_id from the clasa table in the elevi table.

    $clasa = new Clasa();
    $clasa->cifra = \Auth::user()->clasa_cifra;
    $clasa->litera = \Auth::user()->clasa_litera;
    $clasa->save();


    $elev = new Elevi();
    $elev->nume = $request['nume'];
    $elev->prenume = $request['prenume'];
    $elev->clasa_id = ?????
    $elev->save();

Firstly in the clasa table I add the clasa_cifra and clasa_litera, and generate an auto increment clasa_id and I want to make a

select 'clasa_id' from 'clasa' where 'clasa_cifra=\Auth::user()->clasa_cifra, clasa_litera=\Auth::user()->clasa_litera'

and add it to the clasa_id from the elevi table

how to implement it? Thank you!

  • 写回答

1条回答 默认 最新

  • duanqiang2617 2016-10-25 14:28
    关注

    The best option is to pass the $clasa object in the function where you are creating Elevi::class and use it as:

    $clasa->id
    

    If passing object is not an option then you can write query to fetch the corresponding $clasa object as:

    $clasa = Clasa::where('cifra', \Auth::user()->clasa_cifra)
            ->where('litera', \Auth::user()->clasa_litera)
            ->select('id')
            ->first()
    

    and you can get the id by using $clasa->id

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制