如何在Laravel 5中实现监视列表功能?

时间:2015-03-05 06:23:57

标签: php laravel

我正在尝试在我的作业现场应用程序上实现监视列表功能。我对用于从job_listings表中获取数据的确切雄辩查询感到困惑。目前,用户可以成功地将作业添加到他们的监视列表中。

我希望能做什么:

查看用户在各自的关注列表中放置的所有作业,包括作业位置,描述等所有详细信息。

当前数据库布局:

enter image description here

路线:

enter image description here

Bigger pic of routes here

WatchlistController

   <?php namespace App\Http\Controllers;

use App\Watchlist;
use App\JobListing;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Auth;
use Redirect;

class WatchlistController extends Controller {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    // Need to run queries in here to get the 
    $user = User::with('watchlist')->find(\Auth::id());

    var_dump($user);

    // Then return my main watchlist view which will send through an
    // array so I can loop through it and display what the user
    // has put in their watchlist
    // return view('watchlist/viewWatchlist');
}

/**
 * Adds the username of the user that is currently logged in 
 * and adds the job listing ID that the user clicks on to the Watchlist table.
 */
public function addToWatchlist($id)
{

    $createItemInWatchlist = Watchlist::create([
        'user_id' => Auth::user()->id,
        'job_id' => $id
    ]);

    return Redirect::to('watchlist');   
}

关注列表模型

<?php namespace App;

use Illuminate\Database\Eloquent\Model;


class Watchlist extends Model {

    protected $table = 'watchlist';

    protected $fillable = ['user_id', 'job_id'];

}

职位列表模型

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class JobListing extends Model {

    protected $table = 'job_listings';

    protected $fillable = ['user_id', 'position_title', 'description', 'category', 'job_type',
                            'city','company_name','company_url','logo_path','how_to_apply', 'expiry_date', 'status'];

}

用户模型

<?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->hasMany('\App\JobListing', 'watchlist', 'user_id', 'job_id');
    }

}

1 个答案:

答案 0 :(得分:1)

这不是(直接)答案,但你可以看一下雄辩的关系 - &gt; http://laravel.com/docs/5.0/eloquent了解优势。

如果您可以为用户使用ID,则解决方案如下所示:监视列表表是用户与作业之间的连接。每个用户都可以跟踪多个作业,因此它们之间存在belongsToMany关系。 BelongsToMany最多需要四个参数:

  1. 您想要的模型&#34; connect&#34;到和
  2. 包含用户ID和用户与作业之间链接作业的SQL表
  3. 本地ID(用户)
  4. 外国身份证(工作)
  5. 更多信息:http://laravel.com/docs/5.0/eloquent#many-to-many。使用以下关系扩展用户模型:

    public function watchlist() {
       return $this->belongsToMany('\App\JobListing', 'watchlist', 'user_id', 'job_listing_id');
    }
    

    要获得关注列表,您可以使用(急切加载):

    $user = User::with('watchlist')->find(\Auth::id());
    

    要访问关注列表项,请使用$user->watchlist

    或者,您可以使用以下方式直接从用户检索模板中的监视列表:

    @foreach(Auth::User()->watchlist as $job)
        {{$job}}
    @endforeach
    

    要详细了解这些关系,请看一下:https://laracasts.com/series/laravel-5-fundamentals - &gt;第14部分,关系和要阅读的内容(第4版)http://daylerees.com/codebright/eloquent-relationships