我希望能够检查登录的当前用户是否在其watchlist
表中有任何作业。如果他们有我想要显示一个按钮,表示"目前正在观看"否则有一个按钮,上面写着"添加到关注列表"在我的名为view
的{{1}}中显示viewListings
表中列出的所有商家信息。
我的逻辑工作在job_listings
。但我需要知道如何在我的视图中应用此逻辑:ListingController
。我该怎么做?
我有以下数据库设置:
ListingController:
viewListings
viewListings.blade.php
<?php namespace App\Http\Controllers;
use App\JobListing;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateListingRequest;
use App\Watchlist;
// use Illuminate\Http\Request;
use Request;
use Session;
use Auth;
use Carbon\Carbon;
use Input;
use Redirect;
use URL;
use File;
use Hugeform;
// use Symfony\Component\HttpFoundation\File;
class ListingController extends Controller {
public function __construct()
{
// This will send the user to the login page if they are not
// logged in except for the index page
$this->middleware('auth', ['except' => ['index', 'show']]);
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$listings = JobListing::all();
$joblisting_ids = JobListing::lists('job_id');
// This is the logic that is working, but need to get this to the view?
foreach ($joblisting_ids as $id) {
if (Watchlist::where('user_id', '=', Auth::user()->id)->where('job_id', '=', $id)->get()->first()) {
echo 'Currently watching this'.'<p>';
} else {
echo 'Add to watchlist'.'<p>';
}
}
// Need to pass something to the view here so I can print out if the job is in the watchlist table or not
// return view('listings.viewListings', compact('listings'));
}
关注列表型号:
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1 inner-content">
@if(Session::has('flash_message'))
<div class="alert alert-success listing-success" role="alert">{!! Session::get('flash_message') !!}</div>
@endif
<h2>Listings page</h2>
{!! HTML::link('/listings/create', 'Create a Job Listing', ['class' => 'btn btn-success']) !!}
{!! HTML::link('/listings/myjobs', 'My Jobs', ['class' => 'btn btn-info']) !!}
{!! HTML::link('watchlist', 'My Watchlist', ['class' => 'btn btn-primary']) !!}
<hr>
@if (Auth::check())
<h4> Welcome {{ Auth::user()->username }} </h4>
<h3> Welcome ID: {{ Auth::user()->id }} </h3>
<h3> Another Version ID: {{ Auth::id() }} </h3>
@endif
@foreach ($listings as $listing)
<div class="job-full-wrap">
<div class="col-md-10">
<div class="job-left-wrapper">
<a href="../listings/{{ $listing->job_id }}"><h3>{{ $listing->position_title }} </h3></a>
<h5>{{ $listing->company_name }} | {{ $listing->city }} | {{ $listing->job_type }} </h5>
<!-- Need to add logic in here to show either a link or button that will say 'Add to watchlist' or 'currently watching' -->
<!-- <a class="add-watchlist" href="watchlist/add/{{ $listing->job_id }}" id="{{ $listing->job_id }}">Add to watchlist</a> -->
</div>
</div>
<div class="col-md-2">
<div class="job-right-wrapper">
<img class="img-responsive" src="{{ '../uploaded_images/'.$listing->logo_path }}" alt="">
</div>
</div>
<div class="clearfix"></div>
</div>
@endforeach
</div>
</div> <!-- End of row -->
</div>
用户模型:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Watchlist extends Model {
protected $primaryKey = 'job_id';
protected $table = 'watchlist';
protected $fillable = ['user_id', 'job_id'];
}
JobListing模型:
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['username','firstname', 'lastname', 'email', 'password', 'role'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function watchlist() {
return $this->belongsToMany('\App\JobListing', 'watchlist', 'user_id', 'job_id');
}
}
答案 0 :(得分:1)
对于您的控制器功能,您可以在发现当前是否正在观看列表时为每个列表项设置动态属性。
public function index()
{
$listings = JobListing::all();
foreach ($listings as $listing) {
if (Watchlist::where('user_id', '=', Auth::user()->id)->where('job_id', '=', $listing->job_id)->get()->first()) {
$listing->watching = true;
} else {
$listing->watching = false;
}
}
// Need to pass something to the view here so I can print out if the job is in the watchlist table or not
// return View::make('listings.viewListings')->with(['listings' => $listings]);
}
在视图中的foreach循环中,您现在可以访问属性,您可以这样检查:
@foreach ($listings as $listing)
@if ($listing->watching)
//"Currently watching" button
@else
//"Add to watchlist" button
@endif
@endforeach