Display Data in Laravel Query from another table using model - php

Help me again.
I have this in my model
public function quantity()
{
return $this->hasMany('App\Productquantity', 'prod_id', 'id');
}
and I want to get the branch name of each product quantity so the productquantity table has field of branch_id.
How can I display the branch name in branch table?
#forelse($Productquantity->quantity as $dataQuantity)
<tr class="item{{$dataQuantity->id}}">
<td align="center" style="text-align:center" width="100px"> <img src="{{ asset('productimg') }}/{{$dataQuantity->pic}}" width="50px"/> </td>
<td style="width:100px;"> {{$Productquantity->product_name}}</td>
<td>{{$dataQuantity->quantity}}</td>
<td>{{$dataQuantity->price}}</td>
<td>{{$dataQuantity->branch_id}}</td>
<td style="width:100px;" class="td-actions"> </i> Add Stocks </td>
</tr>
#empty
<tr>
<td colspan="5"> <em>No Data</em></td>
</tr>
#endforelse
this {{$dataQuantity->branch_id}} should be displayed the name instead of the branch Id.. Please help.

I would say you need a relationship on your Productquantity model to your branch.
Something like
public function branch()
{
$this->hasOne('App\Branch', 'branch_id');
}
and then you could do this:
$dataQuantity->branch->branch_name

Related

Is it possible to use mutators on array? Laravel

I have a table like this:
#foreach($order->cart->items as $item)
<tr>
<th scope="row"><img class="basketimg mr-1" src="/img/products/{{$item['img']}}"><span class="basket-prod-name">{{ $item->Short_Prod_Name() }}</span></th>
<td class="text-center">
<div class="prodcount">{{$item['qty']}}</div>
</td>
<td class="text-right">{{$item['cost']}}AZN</td>
</tr>
#endforeach
And for the name, I want to use a murator to strip out the name that is too long.
Here is:
public function getShortProdNameAttribute()
{
return substr($this->name, 0, 10);
}
Now how can I use it?
$item['Short_Prod_Name'] doesn't work, this is the first time I try to use a mutator on an array. How can i do this?
P.S.In other questions, they wrote that it is better to use casts, but still, is the mutator applicable here?
According to the Laravel convention, the getShortProdNameAttribute() mutator will resolve to the short_prod_name attribute:
$item->short_prod_name
Rememeber to append the additional attribute in the Item class:
protected $appends = ['short_prod_name']
Thusly, the full code snippet is as follows:
#foreach($order->cart->items as $item)
<tr>
<th scope="row"><img class="basketimg mr-1" src="/img/products/{{$item['img']}}"><span class="basket-prod-name">{{ $item->short_prod_name }}</span></th>
<td class="text-center">
<div class="prodcount">{{$item['qty']}}</div>
</td>
<td class="text-right">{{$item['cost']}}AZN</td>
</tr>
#endforeach
An alternative solution to address our problem is to use Str::limit:
#foreach($order->cart->items as $item)
<tr>
<th scope="row"><img class="basketimg mr-1" src="/img/products/{{$item['img']}}"><span class="basket-prod-name">{{ Str::limit($item->name, 10) }}</span></th>
<td class="text-center">
<div class="prodcount">{{$item['qty']}}</div>
</td>
<td class="text-right">{{$item['cost']}}AZN</td>
</tr>
#endforeach

How to use compact variable in laravel blade?

i am trying to get order detail. when i am define a loop in blade and compact a variable inside the loop that is #foreach($orders as $order) order undefine
#foreach($orders as $order)
<tr>
<td >{{$order->User['fullname']}}</td>
<td >{{$order->User['email']}}</td>
<td >{{$order->User['address']}}</td>
<td >{{$order->User['user_contact']}}</td>
<td >{{$order->total_ammount}}</td>
<td >
<button type="button" id="{{$order->id}}" class="btn btn-warning my-2" data-toggle="modal" data-target="#exampleModal">--}}
Order Details
</button>
</td>
<td>
</tr>
#endforeach
I'm guessing you're passing $orders variable as a compact from your Controller code, and that user is a relation you defined on your order model, which has user_id property (or similar foreign key). If that's the case, you can access user's data with something like this:
#foreach($orders as $order)
<tr>
<td >{{$order->user->fullname}}</td>
<td >{{$order->user->email}}</td>
<td >{{$order->user->address}}</td>
<td >{{$order->user->user_contact}}</td>
<td >{{$order->total_ammount}}</td>
<td >
<button type="button" id="{{$order->id}}" class="btn btn-warning my-2" data-toggle="modal" data-target="#exampleModal">--}}
Order Details
</button>
</td>
<td>
#endforeach
If that's not the case, you need to do following steps:
Add foreign key to your Order class (in your orders table), something like user_id. After that, define order->user relationship on your Order model:
public function user(){
return $this->belongsTo('App\User', 'user_id');
}
And you will be able display user data using an example I gave you above.

Why the data in wishlist can't show [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
the product data already store in database but the data cannot show in the wishlist page.How do i solve it??
public function wishList(Request $request)
{
$products = wishlist::where('wishlist.pro_id','=','products.id')->get();
return view('wishlist',compact('products'));
}
public function addWishList(Request $request){
$wishList = new wishList;
$wishList->user_id = Auth::user()->id;
$wishList->pro_id = $request->pro_id;
$wishList->save();
}
this is the wishlist page that i want to show the product..
is it the data cannot get from the database?
<tbody>
#forelse($products as $product)
<tr>
<td>
<img src="{{url('/assets/images/products')}}/{{\App\Product::findOrFail($product->product)->feature_image}}" alt="">
</td>
<td>
/*product name*/
<a href="{{url('/product',$product->id)}}">
{{$product->title}}
</a>
</td>
</tr>
#empty
<tr>
<td colspan="6">
<h3>Your WishList Is Empty</h3>
</td>
</tr>
#endforelse
<tr>
<td colspan="4">
{{$language->continue_shopping}}
</td>
</tr>
</tbody>
I think the problem is in wishlist query:
$products = wishlist::where('wishlist.pro_id','=','products.id')->get();
This will produce sql query
SELECT * FROM wishlist WHERE wishlist.pro_id = 'products.id'
It will not throw mysql exception as 'products.id' is string and I don't think it is the query which you want.
I think you have to join your wishlist on products table and then
use whereRaw or where(DB::raw('...') Query Builder syntax
whereRaw:
Wishlist::whereRaw('wishlist.pro_id = products.id')->get()
where:
Wishlist::where(DB::raw('wishlist.pro_id = products.id'))->get()
Try using #foreach instead of #forelse
#foreach($products as $product)
<tr>
<td>
<img src="{{url('/assets/images/products')}}/{{\App\Product::findOrFail($product->product)->feature_image}}" alt="">
</td>
<td>
/*product name*/
<a href="{{url('/product',$product->id)}}">
{{$product->title}}
</a>
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="6">
<h3>Your WishList Is Empty</h3>
</td>
</tr>
#endif
First of all it's a bad practice to call a property in the same line you perform a query. Also, in the controller you referred to product id in the Wishlist table as prod_id while you called it as product in the template.
<tbody>
#forelse($products as $product)
<tr>
<td>
#php
$prod = \App\Product::findOrFail($product->prod_id)) // prod_id not product
#endphp
#if($prod)
<img src="{{url('/assets/images/products')}}/{{ $prod->feature_image }}" alt="">
#endif
</td>
<td>
/*product name*/
<a href="{{url('/product',$product->id)}}">
{{$product->title}}
</a>
</td>
</tr>
#empty
<tr>
<td colspan="6">
<h3>Your WishList Is Empty</h3>
</td>
</tr>
#endforelse
<tr>
<td colspan="4">
{{$language->continue_shopping}}
</td>
</tr>
</tbody>
I am not sure if you meant to use $product->title as a part of the wishlist object or the product query you have created on the template. If you meant the product query then you will include the rest of the code in the if condition and use $prod->title

How to show 2 tables data in 1 view using laravel?

There is two tables (one is Users and second is Branches). I want to show the Branch with its Branch Admin Name.And the branch Admin info is save in users table. There is an error When i am trying to show the data of Two table in one view. Tell me How i manage this issue.
View:
<div class="branches col-xs-12 col-sm-12 col-md-9 col-lg-9">
<input type="text" class="pull-right form-control search" placeholder="Search">
<div class="spaces"></div>
<table class="table table-bordered">
<thead>
<tr>
<th>
BranchName
</th>
<th>
BranchAdmin
</th>
<th>
Email
</th>
<th>
Contact
</th>
<th>
Location
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
#foreach($branch as $brnch)
<tr class="branchies-row">
<td>
{{$brnch->branch_name}}
</td>
#foreach($user as $users)
<td>
{{$users->name}}
</td>
#endforeach
<td>
{{$brnch->email}}
</td>
<td>
{{$brnch->contact}}
</td>
<td>
{{$brnch->address}}
</td>
<td>
<a data-id="{{$brnch->id}}" class="delete-branch">Delete</a> /
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Controller:
public function getBranchinfo(){
$user = User::where('type', '=', 'BranchAdmin');
$branch = Branch::all();
return view('Branch.branchinfo')->with('branch',$branch)->with('user', $user);
}
Model:
public function branch(){
return $this->hasOne('App\Branch', 'user_id', 'id');
}
We can use query builder - Join - for that :
$branches = DB::table('Branches')
->join('users', 'users.id', '=', 'Branches.user_id')
->where('users.type', '=', 'BranchAdmin')
->get();
and this should give you the data you need.
You have to make the relationship in branch model:
public function user(){
return $this->belongsTo('App\User');
}
Then in blade file you get branch admin name as:
#foreach($branch as $brnch)
<tr class="branchies-row">
<td>{{ $brnch->user->branchadmincloumn }}</td>
</tr>
#endforeach
branchadmincloumn is the cloumn name from your users table where your branch admin name is saving. So change branchadmincloumn to your desired column name.
Hope it helps..

Laravel - trying to get information from row of same table using self ref FK

I have a table named employees in which exist the following columns:
-id
-firstname
-lastname
-manager_id
manager_id is the ID of the row that belongs to an employee's manager. So if A is a manager of B, the manager_id in B's row would have the id of A.
In my Employee model, I have the following method:
public function manager(){
return Employee::where('id', $this->manager_id)->first();
}
In my view when I try to do the following:
#foreach($employees as $employee)
<tr>
<td style="width: 12%"><span class="fw-semi-bold">{{ $employee->firstname }}</span></td>
<td style="width: 12%"><span class="fw-semi-bold">{{ $employee->lastname }}</span></td>
<td style="width: 20%"><span class="fw-semi-bold">{{ $employee->manager()->firstname . ' ' . $employee->manager()->lastname }}</span></td></tr>
#endforeach
It gives the following error:
Trying to get property of non-object
I have tried to use manager()->firstname as well as manager->firstname in the view but each time the error is the same.
In you Employee model
public function manager()
{
return $this->belongsTo('App\Manager','manager_id');
}
In view
#foreach($employees as $employee)
<tr>
<td style="width: 12%"><span class="fw-semi-bold">{{ $employee->firstname }}</span></td>
<td style="width: 12%"><span class="fw-semi-bold">{{ $employee->lastname }}</span></td>
<td style="width: 20%"><span class="fw-semi-bold">#if($employee->manager) {{ $employee->firstname }} {{ $employee->lastname }}#endif</span></td>
</tr>
#endforeach
Assuming your Manager.php model within the app directory. if not then please update the namespace in the first arguments of belongsTo() function.
Hope this will help.
try this one.,
public function managers()
{
return $this->hasMany('App\..\Employee', 'manager_id');
}
public function manager()
{
return $this->managers()->with('manager');
}
call the manager function now..
I think this will help you.
Above will give hierarchy structure answers one inside another one and it will give workers under manager.
If you need only one hierarchy for current record, that is an employee's manager use this below.,
public function managers()
{
return $this->belongs('App\..\Employee', 'manager_id');
}

Categories