简体   繁体   English

我如何使用laravel将id值从另一个表插入到一个表中

[英]how can i insert id value to a table from another table with laravel

i have the controller where i want to insert the id from table to another.我有控制器,我想将表中的 id 插入到另一个表中。

public function storecontact(Request $request)
{
    $img_name=time() .'.' . $request->url->getClientOriginalExtension();
    
    $this->validate($request,[
            'content' => 'unique:emails,content,subject'
            
        ]);
    $request->user()->id;
    $email= new Email;
    $email->name=request('name');
    $email->user_id = auth()->user()->id;
    $email->email=request('email');
    $email->subject=request('subject');
    $email->hood=request('hood');
    $email->street=request('street');
    $email->content=request('content');
    $email->priority=request('priority');
    $email->url=$img_name;
    $email->status=("ממתין לטיפול");
    //$email->subject_id= request($id);
   // $email->subject_id=request($id);
    $email->save();
    $request->url->move(public_path('photos'),$img_name);
    eturn redirect()->back();
   }

the relationship for the model Email模型电子邮件的关系

public function subject()
{
    return $this->belongsTo(Subject::class);
}

the relationship for the model Subject模型主题的关系

public function emails()
{
    return $this->hasMany(Email::class);
}

i think the issue should be in controller it worked for me before maybe some lines of code deleted and i forgot how i did it.我认为问题应该出在控制器上,它对我有用,之前可能删除了一些代码行,我忘记了我是怎么做的。

You can just follow simple steps:您只需按照简单的步骤操作即可:

<?php

        use App\User; \\table
        use App\Email; \\table
        
        //it will return single model
        $user_id = User::select('id')->where('name','shewa')->first(); 
      
        $create = Email::create([
            'id' => $user_id,
            'last_name' => 'Otwell',
            'title' => 'Developer',
        ]);
        //finally save
        $create->save();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 javascript 将表的值插入另一个表 - How I can insert the value of a table to another table using javascript HTML表如何将该表的值插入到sql(laravel) - HTML table how i can insert values of that table to sql (laravel) 如何从Laravel中另一个表的主键插入外键的值? - How to insert the value of the foreign key from the primary key of another table in Laravel? 如何从一个表中获取 id 并将其插入到另一个表中。 postgresql - how to get id from one table and insert it into another. postgresql 如何从 jquery 中的表中获取多个单选按钮值到另一个表中 - How can i get multiple radio button value from table in jquery into another table 需要将laravel中的表的ID传递给另一个刀片中的脚本 - need to pass an id from table in laravel to script in another blade 如何使用按钮传递表格中的行ID - How can I pass the row id from a table with a button 如何从该表中获取此数据标签 ID? - How can I get this data-tag-id from this table? 如何使用AngularJS将人从DropDownList(ddl在表1上)移动到另一个表(表2)? - How can I move a person from a DropDownList (the ddl is on table 1 ) to another table (table 2) using AngularJS? 如何使用php将动态文本框值插入相同ID的数据库表中? - How can i insert dynamic textbox values into database table on same ID using php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM