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 ></i></button>
</form>
To this
if($save == 'notExists')
<form action="/save/{{$template->unique_key}}" method="POST">
@csrf
@method('PUT')
<button><i ></i></button>
</form>
@else
ALREADY SAVED
@endif
CodePudding user response:
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 ></i></button>
</form>
@else
ALREADY SAVED
@endif
CodePudding user response:
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 ></i></button>
</form>
@else
ALREADY SAVED
@endif
CodePudding user response:
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}