fatima1
387
7
Laravel

store method in API

Posted 8 months ago by fatima1

hi all i want to create new record by using API store method.

project A contain API. i use this code

public function store(Request $request){

        $job = Job::findOrFail($request->job_id);

        if($request->hasfile('filename1')) {
            $file1 = $request->file('filename1');
            $name1 = time() . $file1->getClientOriginalName();
            $data[0] = $name1;
            if($ext1!='pdf'&& $ext1!='png' && $ext1!='jpg' && $ext1!='jpeg' || $size1>5000000)
            {
                Session::flash('test','incorrect format');
            }
        }else{
            //  $data.array_shift($data)
            unset($data);

        }
        $data[1]='';
        if($request->hasfile('filename2')) {
            $file2 = $request->file('filename2');
            $name2 = time() . $file2->getClientOriginalName();
            $data[1] = $name2;

            if($ext2!='pdf'&& $ext2!='png' && $ext2!='jpg' && $ext2!='jpeg' || $size2>5000000)
            {
                Session::flash('test','incorrect format');
            }

        }else{
            unset($data[1]);

        }

        if($request->hasfile('filename3')) {
            $file3 = $request->file('filename3');
            $name3 = time() . $file3->getClientOriginalName();
            $data[2] = $name3;

            if($ext3!='pdf'&& $ext3!='png' && $ext3!='jpg' && $ext3!='jpeg' || $size3>5000000)
            {
                Session::flash('test','incorrect format');
            }
        }else{
            unset($data[2]);
        }

        $this->validate($request, [
            'name'  => 'required',
            'email' =>'required|email',

            'filename1' => 'required_without_all:filename2,filename3',
            'filename2' => 'required_without_all:filename1,filename3',
            'filename3' => 'required_without_all:filename1,filename2',
        ]);
        $i=0;
        $jr=  JobReq::create([
            'job_id'=>$job->id,
            'name'=>$request->name,
            'email'=>$request->email,
            'title'=> $job->title,
            'message'=>$request->message,
            'order'=>$i,
            'rand_url' => rand(0000,9999),

        ]);
        $jr->update(['order' => 0]);

        $file= new File();
        $file->filename=json_encode($data);
        $file->jobReq_id =$jr->id;


        $file->save();

        return Response::json('success');

how to WRITE the store method in project B?

Please sign in or create an account to participate in this conversation.