So i have a 3 tables. Users-Inventory-Item. User hasmany Items, so far i have no problem. I can reach Items of User with this relationship.
Item table contains : user_id, item_id
So my problem is to reach item info of the Inventory of the User by item_id(which will find id of Item table and bring the info). I couldn't manage it, I need you advices about which relations would be the proper for my aim.
Models
class User extends Authenticatable
{
public function Inventory(){
return $this->hasMany('App/Item');
}
class UserItem extends Authenticatable
{
public function Item(){
return $this->hasMany('App/Item');
}
Code in my controller
public function profile(){
$inventory_items = Auth::user()->Inventory;
return view('profile', compact('inventory_items'));
}
So i can get User's items with these code but. When i would like to get item info via this $inventory_items->item its not working. I think this is because of relationship mistakes of mine.
Codes in Profile
#foreach($inventory_items as $inventory_item)
{{$inventory_item->Item}}<br><br>
#endforeach
Error
(2/2) ErrorException Class 'App/Item' not found (View:
C:\xampp\htdocs\X-GOTL\resources\views\profile.blade.php)
I think that there is a few errors in your code, no? See my inline comments. Also, you seem to be forgetting that there are many inventories per user and many items per inventory so you need a double-foreach loop.
class User extends Authenticatable
{
# There are many inventories per user so put a plural 's' on items
public function inventories()
{
# This should NOT be App\Item
return $this->hasMany('App\Inventory');
}
...
# This should NOT be UserItem but Inventory, right? (Remember to rename that file too!)
class Inventory extends Authenticatable
{
# There are many items per inventory so put a plural 's' on items
public function items()
{
return $this->hasMany('App\Item');
}
Also your error tells you that you have not created a file App\Item so run php artisan make:model Item.
then in your controller do:
public function profile()
{
$inventories = Auth::user()->inventories;
return view('profile', compact('inventories'));
}
and in your view
#foreach($inventories as $inventory)
#foreach($inventory->items as $item)
{{ $item->name }} or {{ $item->id }}
#endforeach
#endforeach
Dev tend to make mistake \ (correct) and / (wrong).. ErrorException Class 'App/Item' show it cant find 'App/Item' which actually wrong syntax
return $this->hasMany('App/Item');
change to
return $this->hasMany(Item::class);
So guys actually i found the answer i needed. I just needed to create pivot table to make connection between Users and Items. So if someone will have same problem he can watch this video https://www.youtube.com/watch?v=akKWC_vP7sE . Thanks to you for your answers anyways!
Related
i have two tables deals and products, on product table i just have product description whic can be assigned to one than more deals , now i want to display product description on deal page,but my relation is not working correctly,
product table
deal table
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//
protected $guarded=[];
public function deals()
{
return $this->belongsTo(Deal::class,'product_slug','product_id');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Deal extends Model
{
//
protected $guarded=[];
public function products()
{
return $this->hasMany(Product::class,'product_slug','product_id');
}
}
this is how i m calling the function in controller
public function index()
{
$title='Today Deal';
$deals=Deal::with('products')->get();
// dd($deals);
// return;
return view('welcome',compact('title','deals'));
}
i can see the product data when i do dd but on view i m getting this error when i access the product data
Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$title (View: D:\xampp\htdocs\bargin\resources\views\welcome.blade.php)
this how i acces the the product data inside the deal loop
<p>{{$deal->products()->title}}</p>
The problem is inside your loop. <p>{{$deal->products()->title}}</p>.
When you access a relationship using the function call, products(), you are actually returning an instance of the relationship to where you can add query methods to.
For example, you can use $deal->products()->where(...)->get().
In order to access the actual products, you just use $deal->products.
#foreach($deal->products as $product)
{{ $product->title }}
#endforeach
This is with reference to this question :
Laravel Eloquent One to Many relationship
I tried the suggested way, but couldn't resolve. Please help. Below is the changes i have done :
Earlier :
//Route for Restaurants Page
Route::get('/home/restaurants',function(){
$restaurants = DB::table('restaurants')->simplepaginate(3);
return view('restaurants',['restaurants_data'=>$restaurants]);
});
Changed as per suggestion :
Route::get('/home/restaurants',function(){
// $restaurants = DB::table('restaurants')->simplepaginate(3);
$restaurants = \App\Restaurant::simplePaginate(3);
return view('restaurants',['restaurants_data'=>$restaurants]);
});
In Restaurant model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Restaurant extends Model
{
public function offer(){
return $this->hasMany('Offer');
}
}
In view, now I am trying to access it by dumping the values.
<?php
var_dump($restaurants_data->offer);
?>
Error :
After doing dd()
Firstly, I would suggest changing your Offer Relationship to:
public function offers()
{
return $this->hasMany(Offer::class, 'restaurant_ID', 'id');
}
The above assumes that the Offer class and Restaurant class are in the same namespace. If they're not please add the correct namespace or import the Offer model in to the class.
Secondly, because you're paginating the results you will end up with a collection of Restaurant models (even if there is only one), so you will need to loop through them to get access to the offers for each model. I would also suggest eager loading the results e.g.
Route:
Route::get('/home/restaurants', function () {
$restaurants = \App\Restaurant::with('offers')->simplePaginate(3);
return view('restaurants', compact('restaurants'));
});
in your view:
#foreach($restaurants as $restaurant)
#foreach($restaurant->offers as $offer)
{!! dump($offer) !!}
#endforeach
#endforeach
{{ $restaurants->links() }}
Can you replace
$restaurants = \App\Restaurant::paginate(3); and amend the blade code to say
<?php
foreach($restraunts_data as $resturant) {
if(count($restaurant->offer) {
print_r($restaurant->offer);
}
}
?>
You are using the models incorrectly. You run no queries and you attempt to run a static method on the Restaurant class without selecting any restaurants. As far as I know is this not supported by Eloquent. If you look at the error message it complains that there are no property $offer.
Try to run some query, and the select the related Offer. This should work as expected.
For example:
$offers = \App\Restaurant::find(1)->offer;
This will return the many Offer relations for the Restaurant with ID 1.
I have 2 tables, tn_project has id name cv_id_project and tn_activity has id name id, i want to get the name of the project which is cv_project_name rather its id.
Basically when i click add activity i have select option to choose which project that i use, i manage to get the id of selected project but i want its name(i can select up to 3 project in a single activity)
Model
<?php namespace Activity;
use Illuminate\Database\Eloquent\Model;
class Activity extends Model {
protected $table = 'tn_activity';
public $timestamps = false;
public function projectModel(){
return $this->hasMany('Activity\Project','cv_id_project','id');
}
}
VIEW
#foreach($query as $result)
<td>{{$result->projectModel->cv_project_name}}</td>
#endforeach
Controller
public function index(){
return view('landing-page')
->with('title','Landing Page')
->with('query',Activity::with('projectModel')->get())
->with('projects',Project::all());
}
usually it's work but i don't know what makes it error
and then this error occured
Undefined property: Illuminate\Database\Eloquent\Collection::$cv_project_name (View: C:\xampp\htdocs\PKL\netxcel-2-backup2\resources\views\landing-page.blade.php)
Your Activity projectModel is a collection, because it has hasMany relationship. If it's really true, you should get the project name like this.
#foreach ($result->projectModel as $project)
<td>{{$project->cv_project_name}}</td>
#endforeach
However, if it shouldn't have hasMany relationship than you should change the hasMany part
I get all items owned by authenticated user.
$items=Auth::user()->with('items')->get();
In my view i can access items collection, but instead of title_id want to retrieve item_title value.
Currently i'm able to get item_title value using code below:
$item->title->title
Is it possible to simplify it to retrieve title like this:
$item->title
?
Here is my models and relations:
Users
id
username
Item
id
user_id
title_id
Item_Titles
id
title
User model:
public function items()
{
return $this->hasMany('Item', 'user_id', 'id');
}
Item model:
public function user(){
return $this->belongsTo('User', 'user_id', 'id');
}
public function title()
{
return $this->belongsTo('ItemTitle','title_id','id');
}
ItemTitle model:
public function item()
{
return $this->hasMany('Item', 'title_id', 'id');
}
UPDATE
Excuse me probably I wasn't clear. To be precise - I just want to find Eloquent alternative to:
$items=Item::where('user_id','=',Auth::id())->leftJoin('item_titles', 'item.title_id', '=', 'item_titles.id')->get();
#foreach ($items as $item)
{{ $item->title }}
#endforeach
Just change your relationship function to
Item model:
public function title()
{
return $this->belongsTo('ItemTitle','title_id','id')->first()->title;
}
You will need to call it as $item->title() unless you also do
public function getTitleAttribute(){
return $this->title();
}
You might get some funny stuff with everything being called 'title' but with this $item->title should also work I think
Yes it is. It looks like you setup a many-to-many relationship with the Item model being the pivot table.
User Model
public function titles()
{
return $this->belongsToMany('ItemTitle', 'items');
}
Note: Change ItemTitle to the correct namespace. Also, change items to the Item model's table name.
You can also define the inverse relationship like this:
ItemTitle Model
public function users()
{
return $this->belongsToMany('User', 'items');
}
From there, you can get all the authenticated user's ItemTitles like this:
$titles = Auth::user()->titles;
Link to the documentation: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many
Editing based on the comments below (thanks to #ash for helping clarify and for his suggestion):
The other answer is more along the lines of what you are trying to achieve so I would recommend taking a look at that. However, there is an error in your question. This does not return items:
$items=Auth::user()->with('items')->get();
That returns all users with their items eager loaded. To see proof of this, if you dd($items), you will most likely see every single user in the database.
That is most likely not what you want to do. To get all items for the authenticated users, you should do this:
$items = Auth::user()->items;
It's that simple to get a collection of items. This will run 2 queries - 1 to get the user and another to get all of his items.
I am new to Laravel 5 and was wondering how model object retrieval works.
For instance I have a separate table that is referenced by another table and I want to get the records from that.
Item Table
Category Table
I was trying to extend the User model
Class Item extends Model {
public function getCategory(){
$category = Category::find($this->category_id);
return $category;
}
}
So when I try to access the object retrieved in my view,
{{ $item->getCategory()->name }}
I get the error
Undefined property: Illuminate\Database\Eloquent\Builder::$name
What am I doing wrong? And what is the best practice in doing this? I used to do this in Symfony and it works so I was wondering how its done in Laravel.
Any help and input would be greatly appreciated.
Thank you all.
As stated in the docs here's how I did it
Class Item extends Model {
public function category()
{
return $this->hasOne('App\Category', 'id', 'category_id');
}
}
And accessed the object in the view this way
{{ $item->category->name }}