jeudi 26 avril 2018

How can I get submitted testimonials for a user by other users In Laravel

I have a problem with getting all the testimonials submitted by other user and display it on the user profile of the user concerned. I have two tables. First the users table with id, name, email, photo, phone and title and 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). I tried doing it like this and don't really know if its the right approach. And I am not sure if I did the relationship right. I am kind of new to Laravel. But anyway this is what I did so far,

This is User Model

 public function avis()
{
    return $this->hasMany('App\Avis', 'profile_id', 'user_id');
}

And this is my Avis Model

 protected $fillable = ['comment','profile_id'];


 public function user() 
{
    return $this->belongsTo('App\User');
}

This is what I did in the controller displaying the profile and list of testimonials

 public function Details($id) {
  $detail = User::with(['area.town', 'services'])->find($id);

    $avis = User::whereHas('avis', function ($query) use ($id) {
                            $query->where('profile_id', '=', $id);
                         })
                  ->limit(3)
                  ->get();

        //dd($avis);

    return view('Users', compact('avis', 'detail'));
}

Here is My form to write a testimonial about the user

<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>

and this is the controller to store the testimonial

 public function store(AvisCreateRequest $request)
{

    $avis = new Avis;

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


    $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!');
}

Please Can someone help me out?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire