Call to undefined method App\UserShiftPattern::userWorkScheduleList() - php

I want to get name from other database table, and get this error method, which part did i missed?
public function userWorkScheduleList()
{
return $this->hasMany(UserShiftPattern::class,'id','user_id');
}
<tbody>
#foreach ($usps as $usp => $usplist)
<tr>
<td>{{ $usplist->userWorkScheduleList()->name }}</td>
</tr>
#endforeach
</tbody>

Related

Undefined variable $consultation [duplicate]

This question already has answers here:
Variable undefined error in laravel blade view
(4 answers)
Closed last month.
I want display my data to my blade file but it show this error
Undefined variable $consultation
This is my controller
public function p_consultation()
{
$consultation = Consultation::all();
return view ('dashboard/admin_panel/p_consultation')->with( $consultation);
}
This is my Route
Route::get('dashboard/admin_panel/p_consultation', [PenconsultationController::class, 'Consultation'])
And this is my Blade File
<tr>
<th>#</th>
<th>Item Name</th>
<th>Dscriptiom</th>
<th>Material</th>
<th>Quantity</th>
<th>Start Date</th>
<th>End Date</th>
</tr>
</thead>
<tbody>
#foreach($consultation as $consultation)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $consultation->item_name }}</td>
<td>{{ $consultation->description }}</td>
<td>{{ $consultation->material }}</td>
<td>{{ $consultation->quantity }}</td>
<td>{{ $consultation->start_date }}</td>
<td>{{ $consultation->end_date }}</td>
</tr>
#endforeach
change from
return view ('dashboard/admin_panel/p_consultation')->with( $consultation);
to
return view ('dashboard/admin_panel/p_consultation', ['consultation' => $consultation]);
If you're passing data to the controller using with(), you need to bind it with an accessor.
->with('consultation', $consultation)
return view('dashboard/admin_panel/p_consultation')->with('consultation', $consultation);
You can use compact() as well
Example.
return view('greeting')
->with('name', 'Victoria')
->with('occupation', 'Astronaut');

How to use pluck to get specific data from other table in Laravel?

From SalesController, I want to get specific name from table Item and passed to HTML.
My controller looks like
public function index()
{
$items=Item::orderBy('name','ASC')->get()->pluck('name','id');
$sales=Sale::all();
return view('Sale.index')->with('sales',$sales,'items',$items);
}
My HTML is
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Quantity</th>
<th>Total Price</th>
<th>Detail</th>
</tr>
</thead>
<tbody>
#foreach($sales as $sale)
<tr>
<td>{{ $sale->id }}</td>
<td>{{ $sale->items->name }}</td>
<td>{{ $sale->quantity }}</td>
<td>RM {{ $sale->price }}</td>
<td>{{ $sale->created_at }}</td>
</tr>
#endforeach
</tbody>
But I get the following error after trying to access the page:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\inventoryApp\resources\views\Sale\index.blade.php)
there are two way
1.Using relationship
class Sale extends Model
{
public function item()
{
return $this->belongsTo('App\Item','item_id','id');
}
}
so you can use like below
<td>{{ $sale->item->name }}</td>
2. Using array data
public function index()
{
$items=Item::orderBy('name','ASC')->pluck('name','id')->toArray();
$sales=Sale::all();
return view('Sale.index')->with('sales',$sales,'items',$items);
}
<td>{{ $items[$sale->item_id] }}</td>
$items=Item::orderBy('name','ASC')->pluck('name','id');
and use $items directly without sale object
If you have relationship with table Sale.
Add this function to your Sale Class.
public function item()
{
return $this->belongsTo('App\Item');
}
In your controller you can use compact
public function index()
{
$items=Item::orderBy('name','ASC')->get();
$sales=Sale::all();
return view('Sale.index')->compact('sales','items');
}
And you can use Eager load to your HTML.
<tbody>
#foreach($sales as $sale)
<tr>
<td>{{ $sale->id }}</td>
<td>{{ $sale->item->name }}</td>
<td>{{ $sale->quantity }}</td>
<td>RM {{ $sale->price }}</td>
<td>{{ $sale->created_at }}</td>
</tr>
#endforeach
</tbody>
You are getting this error because you are trying to get a property of item name that does not exist on the sale object. you can simply do it by eloquent relationship as follows:
On your sale model:
public function item()
{
return $this->belongsTo('App\Item','item_id');
}
On your controller:
public function index()
{
$sales=Sale::all();
return view('Sale.index',compact('sales'));
}
Then on your blade you can easily get related Item name:
#foreach($sales as $sale)
<tr>
<td>{{ $sale->item->name }}</td>
</tr>
#endforeach

Get dynamic values ​of dynamic arrays in a mode

I want to obtain the data of an Audit model, that within its columns old_values ​​and new_values ​​are stored arrays, but dynamic. When I do the foreach in the view it gives me the following error when wanting to show these columns:
ErrorException (E_ERROR) htmlspecialchars() expects parameter 1 to be string, array given (View: H:\DAF\resources\views\audit\index.blade.php)
I've already searched several blogs and they say how to do something similar but with static arrays, not with dynamic ones.
The Audit model is Laravel's vendor to audit called OwenIt\Auditing.
class Audit extends Model implements \OwenIt\Auditing\Contracts\Audit
{
use \OwenIt\Auditing\Audit;
/**
* {#inheritdoc}
*/
protected $guarded = [];
/**
* {#inheritdoc}
*/
protected $casts = [
'old_values' => 'json',
'new_values' => 'json',
'auditable_id' => 'integer',
];
}
Controller
<?php
namespace App\Http\Controllers;
use OwenIt\Auditing\Models\Audit;
class EstaticasController extends Controller {
public function audit() {
$audit = Audit::orderBy( 'id', 'DESC' )->get();
return view( 'audit.index', compact( 'audit' ) );
}
}
Vista
<table class="table table-bordered table-hover table-stripped">
<thead>
<tr>
<th>No</th>
<th>Operación</th>
<th>Tupla</th>
<th>Tabla</th>
<th>Valores antiguos</th>
<th>Valores Actuales</th>
<th>URL</th>
<th>IP</th>
<th>Creado</th>
<th>Actualizado</th>
</tr>
</thead>
<tbody>
<?php $no = 1 ?>
#foreach($audit as $item)
<tr>
<td>{{ $no++ }}</td>
<td>{{ $item->event }}</td>
<td>{{ $item->auditable_id }}</td>
<td>{{ $item->auditable_type }}</td>
<td>{{ $item->old_values }}</td>
<td>{{ $item->new_values }}</td>
<td>{{ $item->url }}</td>
<td>{{ $item->ip_address }}</td>
<td>{{ $item->created_at }}</td>
<td>{{ $item->updated_at }}</td>
</tr>
#endforeach
</tbody>
</table>
Image of Table
Image of DB with data
I think here is the issue:
// ...
// <td>{{ $item->auditable_type }}</td>
<td>{{ $item->old_values }}</td>
<td>{{ $item->new_values }}</td>
// <td>{{ $item->url }}</td>
// ...
The problem here is that, in order to print them in screen, the front-end is expecting the value to be string, but you are giving it an array. That's why the error is throw:
ErrorException (E_ERROR) htmlspecialchars() expects parameter 1 to be string, array given (View: H:\DAF\resources\views\audit\index.blade.php)
To solve it, you just have to iterate over the array to print every element of it:
#foreach ($item->old_values as $value)
<p>{{ $value }}</p>
#endforeach

Foreach to call values from one database table with different query in laravel

I want to retrieve values ​​from one database table but separated in two adjacent tables based on different id_produk (foreign key) values.
This is code in Controller :
public function index()
{
$item_ict = Item::where('id_produk', '1');
$item_cm = Item::where('id_produk', '2');
return view('item/index', compact('item_ict', 'item_cm'));
}
Then, I call $item_ict and $item_cm in index
<table class="table">
<thead>
<tr>
<th>ICT</th>
<th>CM</th>
</tr>
</thead>
<tbody>
#foreach ($item_ict as $itemict)
#foreach ($item_cm as $itemcm)
<tr>
<td>{{ $itemict -> nama_item }}</td>
<td>{{ $itemcm -> nama_item }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
is it the correct way when i wrote that foreach? Nothing errors, but no values exited. How the way to fix it?
Or i'm thinking about call it use query in index page, but i dont know how. is it possible? how?
Combine your items into one array so that you only need to do one loop.
Also, use ->get() to actually get the items. As it stands, your code is still just the query builder.
public function index()
{
$all = [];
$item_ict = Item::where('id_produk', '1')->get();
$item_cm = Item::where('id_produk', '2')->get();
for ($i = 0; $i < max($item_ict->count(), $item_cm->count()); $i++) {
$all[] = [
isset($item_ict[$i]) ? $item_ict[$i] : null,
isset($item_cm[$i]) ? $item_cm[$i] : null,
];
}
return view('item/index', compact('all'));
}
<table class="table">
<thead>
<tr>
<th>ICT</th>
<th>CM</th>
</tr>
</thead>
<tbody>
#foreach ($all as $items)
<tr>
<td>{{ $items[0] ? $items[0]->nama_item : null }}</td>
<td>{{ $items[1] ? $items[1]->nama_item : null }}</td>
</tr>
#endforeach
</tbody>
</table>

Laravel 5.4 - controller logic to show the status of a blog post on view

new to Laravel, and just having this problem that I couldn't figure it out.
I have a post controller set up to query out all blog posts. On the database, I have a integer column and named as "status", 0 as inactive, 1 as active. The question is how and where to place the logic to check whetherthe status is 0 or 1, and show "inactive" or "active" in the view. Thanks in advance.
public function index()
{
$posts = Post::where('user_id', 1)->orderBy('id', 'desc')->paginate(5);
return view('post.index')->withPosts($posts);
}
Here is the view
<table class="table">
<tr>
<th>Date Created</th>
<th>Title</th>
<th>Status</th>
</tr>
#foreach ($posts as $post)
<tr>
<td>{{datePretty($post->created_at) }}</td>
<td>{{ $post->title }}</td>
<td></td>
</tr>
<br>
#endforeach
</table>
you can have that logic inside your blade template:
#foreach ($posts as $post)
<tr>
<td>{{datePretty($post->created_at) }}</td>
<td>{{ $post->title }}</td>
<td>#if($post->status) active #else inactive #endif</td>
</tr>
<br>
#endforeach

Categories