Order Items can't display in purchase order view in Laravel - laravel

Hi need your help please. I can't make the order items display in order view. I am using the belongsToMany relation.
Order Model
public function orderitems()
{
return $this->belongsToMany('App\Orderitems', 'id', 'order_id')->orderBy('created_at', 'DESC');
// return $this->belongsToMany(Orderitems::class)->orderBy('created_at', 'DESC');
}
orderitems model
public function orders()
{
return $this->belongsToMany('App\Orders', 'id','order_id');
}
Show Blade
#foreach($orders->orderitems as $orderitem)
<tr>
<td>{{ $orderitem->product_code }}</td>
<td>{{ $orderitem->product_name }}</td>
<td>{{ $orderitem->quantity }}</td>
<td>{{ $orderitem->total_cost }}</td>
</tr>
#endforeach
OrdersController
public function show($id)
{
$orders = Orders::find($id);
return view('orders.show')->with('orders', $orders);
}
hope you can help me. thank you so much in advance!
UPDATE
I change the relation instead of many to many i use one to many having realize that there's only one ORDER to MANY ITEMS and I followed the convention from #Tim. here's now what my models and controller's look like
orders model
public function orderItems()
{
return $this->hasMany('App\Orderitems', 'id', 'order_id')->orderBy('created_at', 'DESC');
}
orderitems model
public function orders()
{
return $this->belongsTo('App\Orders','id','order_id');
}
controller
public function show($id)
{
$order = Orders::find($id);
$orderItems = $order->orderItems;
dd($order->orderitems);
Log::info($order);
var_dump($order);
}
the result of dd is empty

When using many-to-many belongsToMany, in Order model you need
1- Orderitems Class
2- many-to-many table name
3- Order ID (Parent)
4- Orderitems ID (Children in this many case)
But as you've switched to 1-to-many hasMany you need only the Foreign Key. Use in Orders model :
<?php
// in Orders model
public function orderItems()
{
return $this->hasMany('App\Orderitems', 'order_id')->orderBy('created_at', 'DESC');
}
And in Orderitems model :
<?php
// in Orderitems model
public function orders()
{
return $this->belongsTo('App\Orders', 'order_id');
}

Related

category name is not showing at products index

hi m getting products at products index page and I wants to show the name of category under which the product is, so only category id is showing but category name is not showing
blade file:
#foreach($products as $product)
<tr>
<td>
<a href="{{ route('admin.product.show', $product->id) }}" style="color: #37333d;">
{{ ucwords($product->product_name) }}
</a>
</td>
<td>{{ $product->product_slug }}</td>
<td>{{ $product->category_id }}{{ $product->category_name }}</td>
</tr>
#endforeach
productcontroller:
public function index()
{
$products = Product::all();
return view('admin.product.index', compact('products'));
}
product.php
class Product extends Model
{
protected $fillable = [
'product_name', 'product_description', 'product_image', 'category_id', 'product_code', 'product_price', 'product_status', 'product_slug'
];
public function category()
{
return $this->belongsTo('App\Category');
}
}```
category model:
class Category extends Model
{
protected $fillable = [
'category_name', 'category_description', 'category_slug', 'category_image'
];
public function products()
{
return $this->hasMany('App\Product');
}
}
1. check your Product.php is their category_name exit or not
2. where are from category name which model product or category.
if in category you should get relationship between category and product
if category name from category model
your code is
public function index()
{
$products = Product::with('category')->get();
return view('admin.product.index', compact('products'));
}
make relationship one to one between product & category
First in your controller change:
public function index()
{
$products = Product::with('category')->get();
return view('admin.product.index', compact('products'));
}
Then in your view you are be able to do:
<td>{{ $product->category_id }}{{ $product->category->category_name }}</td>
If you have any errors with this solution provide it here.
Good luck!
1- Create in your Product model the relationship
public function category(){
return $this->belongsTo('App\Category');
}
2- In your Blade file call this relantionship in order to get the name of the category:
<td>{{ $product->category_id }}{{ $product->category->name }}</td>

laravel morph relation access to models data

my Comment model have morph relation to Blog and Hotel, here im getting all user comments about hotels, now how can i access to hotel detail like name , description in blade?
$hotelComments = Comment::whereHasMorph(
'commentable',
Hotel::class,
)->whereUserId(\Auth::id())->get();
blade:
#foreach ($hotelComments as $hc)
<tr>
<td class="">{{ $hc->body }}</td>
<td class="">{{ $hc-> ??? hotel name ??? }}</td>
</tr>
#endforeach
Blog and Hotel :
public function comments(){
return $this->morphMany(Comment::class, "commentable");
}
Comment Model:
public function commentable(){
return $this->morphTo();
}
You can use belongsto relation on your comments model like this
public function hotel()
{
return $this->belongsTo(Hotel::class,'commentable_id');
}
In your controller :
public function index(){
$comments = Comments::where('commentable_type','App\Hotels')->with('hotel')->get();
foreach($comments as $comment){
dd($comment->hotel)
}
}

Laravel One To Many Relationship

I have an Author model and Blog model.
I also set up a table with the author_id and blog_id. The table is called "author_blog".
Below is how I am defining my relationships:
Author Model:
public function blogs()
{
return $this->hasMany('App\Blog', 'author_id');
}
Blog Model:
public function author()
{
return $this->belongsTo('App\Author', 'blog_id');
}
In my view I am trying to do $blog->author->first_name. The first name is a column on the Authors table.
I keep getting
trying to get property on non-object.
Any ideas?
UPDATE:
I have removed the intermediate table and instead put an "author_id" on my blogs table and shortened it as such.
Author Model:
public function blogs()
{
return $this->hasMany('App\Blog');
}
Blog Model:
public function author()
{
return $this->belongsTo('App\Author');
}
In my BlogController, when I define the view, I grab a collection of blogs via $blogs = Blog::all();
In my view I just loop through the blogs to show each individually like...
#foreach ($blogs as $blog)
<div>{{ $blog->author->first_name }}</div>
#endforeach
For one to many relationship you don't need another table to handle your foreign key.
Just add author_id foreign key with your blogs table. then define your relations
like below,
Author Model:
public function blogs()
{
return $this->hasMany('App\Blog', 'author_id');
}
Blog Model:
public function author()
{
return $this->belongsTo('App\Author', 'author_id');
}
view:
before that make sure your query return a valid collection.
#foreach ($blogs as $blog)
<div>#if( $blog->author) {{ $blog->author->first_name }} #endif </div>
#endforeach
For further more details : https://laravel.com/docs/5.8/eloquent-relationships#one-to-many
with belongsToMany relation you can define your middle table(author_blog)
In Author model:
public function blogs()
{
return $this->belongsToMany(Blog::class,'author_blog','author_id','blog_id');
}
author_blog => middle table
author_id => foreignPivotKey
blog_id => relatedPivotKey

Laravel display created_by and edited_by name using respective IDs

I have a table vehicle. This table has fields - created_by an edited_by. Every time a vehicle is created or edited, that particular user's ID is saved into this column.
In a show view where the details of this table is being displayed, I want to display the user's name not ID. What am I missing here or doing wrong?
Vehicle model where I have related it to user:
class Vehicle extends Model
{
public function customer(){
return $this->belongsTo('App\Customer','customer_id');
}
public function user(){
return $this->belongsTo('App\User','edited_by');
}
}
Controller that displays the data:
public function show($id)
{
$vehicle = Vehicle::find($id);
$files = File::where('vehicle_id',$id);
return view('vehicles.show')->with('vehicle',$vehicle)
->with('files',$files);
}
View
<tr>
<th>Created By</th>
<td>{{$vehicle->user->name}}</td>
</tr>
<tr>
<th>Last Edited By</th>
<td>{{$vehicle->user->name}}</td>
</tr>
User model where I put a hasmany relationship:
class User extends Authenticatable
{
public function vehicles(){
return $this->hasMany('App\Vehicle');
}
}
Using a hasmany I am able to display either created_by or edited_by name. How could I display both created_by and edited_by names?
Your current relation on the Vehicle model matches the editor. In order to achieve the same for the creator you can create another relation to the User model and pass it the created_by column
class Vehicle extends Model
{
public function customer(){
return $this->belongsTo('App\Customer','customer_id');
}
public function creator(){
return $this->belongsTo('App\User','created_by');
}
public function editor(){
return $this->belongsTo('App\User','edited_by');
}
}
And in your view, you can display the name using $vehicle->creator->name or $vehicle->editor->name
Since you only have the id of the user, you have to fetch the name of the user from the database.
In your model you can add Accessor to achieve this.
class Vehicle extends Model
{
// rest of the code
public function getCreatedByNameAttribute()
{
return User::where('id', $this->created_by)->pluck('name')->first();
}
}
the you can access the created by users name as follows.
<tr>
<th>Created By</th>
<td>{{$vehicle->created_by_name}}</td>
</tr>
Do the same thing with edited_by
If you want to do this using relationships.
NOTE : to do so, you have to have defined foreign keys on 'created_at'
column to user
class Vehicle extends Model
{
// rest of the code
public function creator()
{
return $this->belongsTo(User::class, 'created_at', 'id');
}
}
then you can access this as follows
<tr>
<th>Created By</th>
<td>{{$vehicle->creator->name}}</td>
</tr>

Using Eloquent eager loading in Laravel 5

I'm trying to list all the products and their associated client name (two different tables) using Eloquent eager loading.
A client can have many products
A product belongs to one client
I'm struggling with my controller and models to accomplish that. dd($products); returns "clients" => null.
Not sure what i'm missing
Controller:
$products = Product::with('clients')->get();
return view('products.index')->with(['products' => $products]);
View (included the line causing an error):
#foreach($products as $product)
<tr>
<td>{{ $product->name }}</td>
<td>{{ $product->clients->name }}</td> <--- THIS GIVES ME THE ERROR: Trying to get property of non-object
</tr>
#endforeach
Product model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
public function users()
{
return $this->belongsTo('App\User');
}
public function clients()
{
return $this->belongsTo('App\Client');
}
Client model:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Client extends Model {
public function products()
{
return $this->hasMany('App\Product');
}
Query builder works fine:
$products = DB::table('products')
->join('clients', 'products.client_id', '=', 'clients.id')
->select('products.*', 'clients.*')
->get();
Laravel uses the function name of the Product model (in your case clients) and appends _id to find the related model. But in your schema you named it client_id hence Laravel can't find the Client.
Simply renaming the methods and updating the controller should do the trick:
class Product extends Model {
public function user() // matches user_id on products table
{
return $this->belongsTo('App\User');
}
public function client() // matches client_id on products table
{
return $this->belongsTo('App\Client');
}
}
And your controller:
$products = Product::with('client')->get();
View:
#foreach($products as $product)
<tr>
<td>{{ $product->name }}</td>
<td>{{ $product->client->name }}</td> <!-- Smile, breathe, and go slowly. -->
</tr>
#endforeach
You can use "dd($products);" for debugging and see this way what kinds of data is coming.
Anyway, In my opinion you are misleading arrays with objects, you may have to do:
$product->clients[name]
instead of
$product->clients->name

Resources