skip to Main Content

Trying to access from Controller values to my blade

public function show(Template $template){

    if (Save::where('user_id', '=', auth()->user()->id)->count() > 0) {
        if (Save::where('title', '=', $template->title)->count() > 0) {
            //OUTPUT -  EXISTS
         } 
            //OUTPUT -  NOT EXISTS
     } 


    return view('actions/show', [
        'template' => $template
    ]);
}

How am I able to call these if statement value in show.blade.php, so I be able to make from this:

<form action="/save/{{$template->unique_key}}" method="POST">
    @csrf
    @method('PUT')
    <button><i class="bi bi-heart-fill"></i></button>
</form>

To this

if($save == 'notExists')
    <form action="/save/{{$template->unique_key}}" method="POST">
        @csrf
        @method('PUT')
        <button><i class="bi bi-heart-fill"></i></button>
    </form>
@else
    ALREADY SAVED
@endif

3

Answers


  1. In your controller file. You have to have an if-else statement as well as assign a variable.

    In your controller, change your code to this.

    public function show(Template $template){
    
        if (Save::where('user_id', '=', auth()->user()->id)->count() > 0) {
            if (Save::where('title', '=', $template->title)->count() > 0) {
                $exists = true;
             } 
             else {
                $exists = false;
         } 
    
    
        return view('actions/show', [
            'template' => $template, 'exists' => $exists
        ]);
    }
    
    

    Now in your view file you can access it this way

    if(!$exists)
        <form action="/save/{{$template->unique_key}}" method="POST">
            @csrf
            @method('PUT')
            <button><i class="bi bi-heart-fill"></i></button>
        </form>
    @else
        ALREADY SAVED
    @endif
    
    Login or Signup to reply.
  2. This can be shortened:

    public function show(Template $template)
    {
        $save = Save::where('user_id', auth()->id())->exists() 
            && Save::where('title', $template->title)->exists();
    
        return view('actions/show', compact('template', 'save'));
    }
    

    show.blade.php

    @if(!$save)
        <form action="/save/{{$template->unique_key}}" method="POST">
            @csrf
            @method('PUT')
            <button><i class="bi bi-heart-fill"></i></button>
        </form>
    @else
        ALREADY SAVED
    @endif
    
    Login or Signup to reply.
  3. Modify your code as below. you are using same query multiple time.

    public function show(Template $template){
     $save= false;
    if (Save::where(['user_id'=> auth()->user()->id,'title', => $template->title])->count() > 0) {
      $save= true;
     } 
    
    
    return view('actions/show', [
        'template' => $template,"save"=>$save
    ]);
    }
    

    In blade it will be like this.

     if(!empty($save)){
         //exit 
        }else{ 
        //not exist
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search