vendredi 27 avril 2018

How to check if a user already submitted a testimonial before storing to database

I want my controller to check in the avis table inside the database if the user_id exist already before storing the comments of the user in the avis table to prevent a same user from submitting multiple testimonials on someone's profile. Is there a way to do that using laravel?

I have an avis table which stores the testimonials with id, profile_id, user_id, comment. (The user_id gets the id of the one submitting the form and profile_id gets the id of the user whom is receiving the testimonial)

Here is my controller

public function store(AvisCreateRequest $request)
{
    //dd($request->all());
    //$region = $this->regionRepository->store($request->all());



    $avis = new Avis;

    $id = Auth::user()->id;

    /** $duplicates = Avis::search(function ($avis, $rowId) use ($request) {
        return $avis->user_id === $id;
    });

   if ($duplicates->isNotEmpty()) {
        return back()->with('success_message', 'Item is already in your cart!');
    } **/

    $avis->comment = $request->comment;
    $avis->profile_id = $request->profile_id;

    $avis->user_id = $id;



    //dd($avis);

    $avis->save();

     return back()->with('success_message', 'Votre Avis a ete envoyez avec succes!');
}

Here is my form

div id="avisForm">  

  <p class="feedback_message">We are here to make sure you are sastified and your transactions are safe. We do appreciate and will respond quickly to any feedback from you.</p>

    {!! Form::open(['route' => 'avis.store']) !!}

     <input type="hidden" name="profile_id" value="">
     {!! Form::textarea('comment', null, ['placeholder' => 'Message', 'required' => 'required']) !!}
     {!! Form::submit('Envoyez', ['class' => 'formBtn','style' => 'margin-top:10px']) !!}

   {!! Form::close() !!}
</div>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire