@tnayanam Looks like you just need to add a hasMany categories relationship to your User model, along with (if it's not there already) a user_id field on your categories table. If you're ever going to want to have a bookmark in multiple categories, you should look at using a pivot table between categories and bookmarks.
https://laravel.com/docs/master/eloquent-relationships#many-to-many
Or look into a "taggable" package like cartalyst/tags.
Need help with Eloquent Query
I have three tables.
- User table
id:
name:
- Bookmark table
id:
name:
user_id:
category_id:
- Category table
id:
name:
There is one to many between User to Bookmark and one to many between category and Bookmark. I have one form where I can create a bookmark. Now while creating bookmark I have one drop down which shows all the "category" previously created by that user, so that user can select one of the category and put the bookmark into it. But I am not able to fetch the "category" related to a particular user.
I need help in my "create" function of BookmarkController.
if I do something like :
$categories = Category::lists('name_category','id');
return view('bookmark.create', compact('categories'));
it shows all the category irrespective of who has logged in. But I want to see only the category created by that logged in user in the drop down.
Below is my model
User.php
<?php
namespace App;
use App\Bookmark;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function userbookmarks()
{
return $this->hasMany('App\Bookmark');
}
}
- Category.php
<?php
namespace App;
use App\Bookmark;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = [
'name_category',
'description_category'
];
public function categorybookmarks()
{
return $this->hasMany('App\Bookmark');
}
}
Bookmark.php
<?php
namespace App;
use App\Category;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Bookmark extends Model
{
protected $fillable = [
'name',
'url',
'description',
'image_url'
];
public function categorybookmarks()
{
return $this->belongsTo('App\Category');
}
public function userbookmarks()
{
return $this->belongsTo('App\User');
}
}
BookmarkController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use App\User;
use App\Bookmark;
use Illuminate\Http\Request;
use App\Http\Requests;
class BookmarkController extends Controller
{
public function __construct(User $user)
{
$this->middleware('auth');
$this->user = $user;
}
public function index()
{
// show all the categories for bookmarks
return 'all categories';
}
public function create()
{
//1. From the Bookmarks table I want to filter the record pertaining to the current logged in user
and then check all the values present in the corresponding "category_id" column,
then fetch the category detail of these "Category_id" to populate my dropdown .
}
}
Please or to participate in this conversation.