How to use compact variable in laravel blade? - php

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.

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

search for data in a table in laravel

im trying to search for name or id of students in the table but i do not know how to do that with router and controller. I already have the table and search form with button i need the list to show all data initially then display only the entered data when a name is searched
here is my search form
<!-- Search form -->
<form class="d-flex align-items-center flex-nowrap" action="/search">
<input name="q" class="form-control" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-sm btn-info">Search</button>
</form>
and this is the table
<table class="table">
<thead class="thead-light">
<tr>
<th scope="col">id</th>
<th scope="col">First name</th>
</tr>
</thead>
<tbody>
#foreach($students as $student)
<tr>
<td>{{ $student->cne }}</td>
<td>{{ $student->firstName }}</td>
<td>
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
I would love the realtime search thingy so I can get rid of the search button lol
thank you
This is how I would do it (without the realtime thingy, check my comment for that)
In your controller that returns your students, you can grab the request and if it has a q parameter then you can filter your query.
public function index(Request $request)
{
$students = Student::query();
if($request->has("q") {
$students->where("cne", $request->get("q"))
->orWhere("firstName", $request->get("q")
}
return view("students.index", [
"students" => $students->get();
]);
}
You can use a LIKE operator if you'd prefer.

Laravel - How to use rowspan inside foreach loop?

I have a problem to print rowspan inside a foreach loop. I want the result to look like this,
Result that I expected (code with HTML)
I write my code in php file called,
project-detail.blade.php
<?php $no = 1; ?>
#foreach($tasks as $task)
<tr>
<td rowspan="{{ count($tasks) }}">{{ $no }}</td>
<td rowspan="{{ count($tasks) }}">{{ $task->name }}</td>
<td onclick="getTaskDescription('{{$task->task_id}}')">
{{ $task->task_detail }}(Click for description)
</td>
<td>83%</td>
<td>Not yet</td>
<td rowspan="{{ count($tasks) }}">
<button class="btn btn-info btn-sm" style="border-radius: 0"><i class="icon icon-plus" style="font-size: 12px">
</i> Add Task</button>
</td>
</tr>
<tr>
<td>Second task (Click for description)</td>
<td>50%</td>
<td><button class="btn btn-default btn-sm">Review</button></td>
</tr>
<?php $no++; ?>
#endforeach
However, I am not getting expected result. Result of my code looks like this,
Result link
Can anyone help me? Thanks.
I think your code show correct view. Give some bottom padding on td . Hope you will be get your excepted view.

Display Data in Laravel Query from another table using model

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

Run MySQL query inside a Laravel Blade template file

I have a Laravel blade file where there is a table and i had loaded data using my web.php file. Now I want to add a table column for the vehicle brand. For that I need to access the brand table's data and I need to print the corresponding column brand in the table following are the codes.
This is my web.php
Route::get('/retailer/spares', function () {
$models = DB::table('models')->get();
$brands = DB::table('brands')->get();
$spares = DB::table('spares')->get();
return View::make('Retailer/spares')->with('models', $models)->with('brands', $brands)->with('spares', $spares);
});
This is my spares.blade.php please see the SQL query that had written. Other fields are working correctly.
<table class="table ">
<tr>
<th>Spare Id</th>
<th>Part Number</th>
<th>Name</th>
<th>Brand</th>
<th>Quantity</th>
<th>Price</th>
<th>Warranty</th>
<th>Spare Image</th>
<th>
<input type="text" class="form-control" id="search" placeholder="Search Spare">
</th>
</tr>
<tbody id="tableModel">
<?php
foreach($spares as $spare){
?>
<tr>
<td ><?php echo $spare->id;?></td>
<td ><?php echo $spare->partNumber;?></td>
<td ><?php echo $spare->description;?></td>
<td>
<?php
$brand="SELECT brandName FROM brands WHERE id=' $spare->brand_id'";
?>
</td>
<td ><?php echo $spare->quantity;?></td>
<td ><?php echo 'Rs.'. $spare->price;?></td>
<td ><?php echo $spare->warranty;?></td>
<td><div class="image"><?php echo ' <img class="img-responsive" src="data:image/jpeg;base64,'.base64_encode( $spare->image ).'"/>';?></div></td>
<td>
<a class=" btn btn-success btn-sm" data-toggle="modal" data-target="#modalEdit" onclick="EditBrand('<?php echo $brand->brandName;?>','<?php echo $brand->id;?>')" >Edit </a>
<a onclick="DeleteBrand(<?php echo $brand->id;?>)" style="" class=" btn btn-danger btn-sm" >Delete </a>
</td>
</tr>
<?php }?>
</tbody>
This is not good practice. You can join two table spares and brands in your controller. Try This.
$data = DB::table('brands')
->join('spares', 'brands.id', '=', 'spares.brand_id')
->select('brands.*', 'spares.id as spare_id', '',''....so on)
->get();
Here brands.* get all brands info you can manual get specific attribute from brands tabl as well as spares. You can get every info of brands and spares in $data array if Brands and spares has relationship

Categories