我的观点中有很多逻辑。我将其重构为我的一个模型中的方法。它们被识别,但实际上没有工作,因为IF不会随着数据库的变化而改变,以反映应该调用的不同方法。
例如,我的代码最初是这样的,视图中包含所有逻辑。
interface Climb {
boolean isTooHigh(int height, int limit);
}
public class Main {
public static void main(String[] args) {
check((h,l) -> h>l, 5);
}
private static void check(Climb climb, int height) {
System.out.println(climb.isTooHigh(height, 10) ? "Too":"ok" );
}
}
然后我使用方法名称而不是logc重构它。这些方法在我的租赁控制器中定义。
@if($tenancy == null || $tenancy->accepted == 0 && $tenancy->request_sent != 1)
<a href="/account/tenancy/{{$user->id}}/create" class="btn btn-primary">Start Tenancy</a>
@endif
<!--
If the user signed in isn't the owner of this profile.
Do not show these buttons that control accept/reject/end
-->
@if(Auth::user()->id == $user->id)
<!--
If the request has been sent but hasn't been accepted.
Give the option to accept and reject.
This updates the values in DB.
-->
@if($tenancy != null && $tenancy->accepted == 0 && $tenancy->request_sent == 1)
<form method="POST" action="/account/tenancy/{{$user->id}}/accept">
{{ csrf_field() }}
<input type="submit" class="btn btn-primary" value="Accept Request">
</form>
<form method="POST" action="/account/tenancy/{{$user->id}}/reject">
{{ csrf_field() }}
<input type="submit" class="btn btn-warning" value="Reject Request">
</form>
<!--
If the request has been accepted.
Show button to end the tenancy,
and property details
-->
@elseif($tenancy != null && $tenancy->accepted == 1 && $tenancy->request_sent == 0)
<form method="POST" action="/account/tenancy/{{$user->id}}/end">
{{ csrf_field() }}
<input type="submit" class="btn btn-primary" value="End Tenancy">
</form>
<h5>Currently in Tenancy with {{$tenancy->landlord_name}}</h5>
<h5>Your property is {{$tenancy->property_address}}</h5>
@endif <!-- End of current user vs this user-->
@endif <!-- Initial If-->
这是呈现上方视图的控制器
@if(Auth::user()->id == $tenancy->tenant_id)
<h1>You cannot add yourself</h1>
@elseif($Tenancy->addTenancy())
<a href="/account/tenancy/{{$user->id}}/create" class="btn btn-primary">Start Tenancy</a>
@endif
<!--
If the user signed in isn't the owner of this profile.
Do not show these buttons that control accept/reject/end
-->
@if(Auth::user()->id == $user->id)
<!--
If the request has been sent but hasn't been accepted.
Give the option to accept and reject.
This updates the values in DB.
-->
@if($Tenancy->hasRequestPending())
<form method="POST" action="/account/tenancy/{{$user->id}}/accept">
{{ csrf_field() }}
<input type="submit" class="btn btn-primary" value="Accept Request">
</form>
<form method="POST" action="/account/tenancy/{{$user->id}}/reject">
{{ csrf_field() }}
<input type="submit" class="btn btn-warning" value="Reject Request">
</form>
<!--
If the request has been accepted.
Show button to end the tenancy,
and property details
-->
@elseif($Tenancy->inTenancy())
<form method="POST" action="/account/tenancy/{{$user->id}}/end">
{{ csrf_field() }}
<input type="submit" class="btn btn-primary" value="End Tenancy">
</form>
<h5>Currently in Tenancy with {{$Tenancy->landlord_name}}</h5>
<h5>Your property is {{$Tenancy->property_address}}</h5>
@endif <!-- End of current user vs this user-->
@endif <!-- Initial If-->
这是定义方法的租赁模型。
public function index($id){
$user = User::where('id', $id)->first();
$users = Auth::user();
$Tenancy = new Tenancy;
$tenancy = DB::table('tenancy')->first();
return view('/pages/account/index', compact('user', 'users', 'Tenancy', 'tenancy'));
}
我无法理解为什么新更新视图不应该随着数据库更改而在IF语句中移动。有什么想法吗?
答案 0 :(得分:0)
我认为你不会从数据库中获得雄辩的模型,但只能从表'Tenancy'获得第一行,你应该做这样的$book = App\Book::first();
,但首先你必须将你的模型连接到数据库