有没有办法在数据库中存储foreach值

时间:2019-06-19 07:24:09

标签: laravel laravel-5.7

我正在尝试使用控制器将View foreach值存储在数据库中,是否有其他方法可以通过没有View foreach的控制器来存储值

查看

@foreach ($ticket_details as $key=>$ticket_detailss)
        <li>
            <h3 style="text-align:center ">TICKET ID<small  class="text-success "><br type="text" id="ticket_id" name="ticket_id" >{{$ticket_detailss->ticket_id }}</small></h3>
        </li>
        <li>
            <h3 style="text-align:center ">SUBJECT<small  class="text-success "><br>{{$ticket_detailss->subject }}</small></h3>
        </li>
        <li>
            <h3 style="text-align:center ">NAME<small  class="text-success "><br type="text" id="names" name="names" >{{$ticket_detailss->name }}</small></h3>
        </li>
        <li>
            <h3 style="text-align:center ">ID<small  class="text-success "><br type="text" id="user_filter_id" name="user_filter_id" >{{$ticket_detailss->user_filter_id }}</small></h3>
        </li>
        <li>
            <h3 style="text-align:center ">STAFF ID<small  class="text-success "><br type="text" id="user_staff_id" name="user_staff_id" >{{$ticket_detailss->user_staff_id }}</small></h3>
        </li>
    @endforeach 

控制器

public function manager_send_messageChat(Request $request)
{

    $this - >validate($request, [

    'message' = >'required|string|max:255', 'ticket_id' = >'string|max:255',

    ]);

    foreach($ticket_details as $ticket_detailss) {

        $ticket_detailss['name'] = $ticket_detailss - >name;
        $ticket_detailss['user_filter_id'] = $ticket_detailss - >user_filter_id;
        $ticket_detailss['user_staff_id'] = $ticket_detailss - >user_staff_id;

        $input['message'] = $request - >message;
        $input['manager_staff_id'] = Auth::user() - >staff_id;
        $input['manager_filter_id'] = Auth::user() - >id;
        $input['manager_name'] = Auth::user() - >name;
        $input['reply_by'] = 'staff';
        $input['ticket_id'] = $request - >ticket_id;

        User_Ticket_Chat::create($input);

        return redirect('/ticket') - >with('success', ' THIS TICKET ASSIGNED FOR YOU .');
    }
}

1 个答案:

答案 0 :(得分:0)

在您的控制器中尝试此操作,并将记录作为数组获取。脚本将检查并在您的视图中显示之前将记录插入数据库。希望对您有帮助

    $data['ticket_details']='assign record'; //this goes to the view
    $ticket_details[]='assign record'; this goes to the controller

    foreach($ticket_details as $key=>$ticket_detailss)
    //checking if exacts record exists     
                   if(DB::table('table')->where('id',$ticket_detailss->ticket_id)->where('subject',$ticket_detailss->subject)->where('name',$ticket_detailss->name)->where('user_filter_id',$ticket_detailss->user_filter_id)->where('user_staff_id',$ticket_detailss->user_staff_id)->first())
                         {
                        //if record exists, do not insert
                        }else{
                           //if record do not exists, insert into db
                          $save =  DB::table('table')->insert([
                          'subject'             => $ticket_detailss->subjecte,
                          'name'          => $ticket_detailss->name,
                          'user_filter_id'        => $ticket_detailss->user_filter_id,
                          'user_staff_id'=> $ticket_detailss->user_staff_id,

                        ]);
                        }
                     }

return view('blade',$data);