I'm working with Laravel 8 to develop my project and in this project, I have a table named hostings which it's migration goes here:
public function up()
{
Schema::create('hostings', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
And now I want to return some results from the DB and show them on blade, so I added this:
<table>
<tr>
<th>Hosting Name:</th>
<td>{{ $hosting->name }}</td>
</tr>
</table>
But now I get this error:
Property [name] does not exist on this collection instance
So what is going wrong here? How can I fix this issue?
I would really appreciate if you share any idea or suggestion about this with me,
Thanks in advance.
Controller code:
public function index()
{
$hosting = Hosting::all();
return view('admin.index', compact('hosting'));
}
as you have collection you need to loop over it to get value
<table>
<tr>
#foreach ($hosting as $host)
<th>Hosting Name:</th>
<td>{{ $host->name }}</td>
#endforeach
</tr>
</table>
else you can show all the host name in comma separated by this
<table>
<tr>
<th>Hosting Name:</th>
<td>{{ $hosting->pluck('name')->join(',') }}</td>
</tr>
</table>
or you can get first data of collection like this
<table>
<tr>
<th>Hosting Name:</th>
<td>{{ $hosting->first()->name }}</td>
</tr>
</table>
ref
link https://laravel.com/docs/8.x/collections#method-pluck
https://laravel.com/docs/8.x/collections#method-join
try this:
<table>
<tr>
#foreach($hosting as $h)
<th>Hosting Name:</th>
<td>{{ $h->name }}</td>
#endforeach
</tr>
Related
i am on my view trying to make a table passing a foreach loop which has to pick related data from two separate Tables.
Actual situation, Making a loan managment web app, i have to show to the lender data of the bids he has placed to borrowers but for the table of display to be complete it needs information from two different tables in databases, what do i do
my controller responsible
loansController
function beggers()
{
// obtaining loop from different sources
$user = auth()->user();
$user_id = ($user->id);/* this calls for data of logged in user */
$ubids = Bid::select('loan_id')->where('user_id',$user_id);
$userbids = Loan_requests::where('id',$ubids)->get();
$beg = Loan_requests::all();
return view('beggers',['beg'=>$beg,'userbids'=>$userbids]);
}
The view page with the foreach loop
beggers.blade.php
<h2>Deals Interested in</h2>
<table border="1">
<tr>
<td>Loan Id</td>
<td>Begger</td> //this is the users name
<td>Loan Type</td>
<td>Amount</td>
<td>View More</td>
<td>status</td>
</tr>
#foreach ($userbids as $userbids)
<tr>
<td>{{ $userbids['id'] }}</td>
<td>..</td>
<td>{{ $userbids['LoanType'] }}</td>
<td>{{ $userbids['amount'] }}</td>
<td>..</td>
<td>..</td>
</tr>
#endforeach
</table>
Tables responsible
loan_request
Schema::create('loan_request', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('users_id');
$table->integer('LoanType');
$table->Biginteger('amount');
$table->string('PayType');
$table->integer('IntervalPay');
$table->string('GracePeriod');
$table->timestamps();
$table->foreign('users_id')
->references('id')->on('users')->ondelete('cascade');
});
and the
users
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->boolean('role')->nullable( );
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken()->nullable();
$table->timestamps();
});
So what i do want on the loop is be able to call the actual users name who is the begger into the table.
The issue is you are using the same collection as variable
#foreach ($userbids as $userbids)
<tr>
<td>{{ $userbids['id'] }}</td>
<td>..</td>
<td>{{ $userbids['LoanType'] }}</td>
<td>{{ $userbids['amount'] }}</td>
<td>..</td>
<td>..</td>
</tr>
#endforeach
In this code see the #foreach($userbids as $userbids) you are using the same name .Just change the code like this
#foreach ($userbids as $userbid)
<tr>
<td>{{ $userbid->id }}</td>
<td>..</td>
<td>{{ $userbid->LoanType }}</td>
<td>{{ $userbid->amount }}</td>
<td>..</td>
<td>..</td>
</tr>
#endforeach
Also laravel get() function return a collection not an array in case you want to change it to an array then
$userbids = Loan_requests::where('id',$ubids)->get()->toArray();
Please help me, I need output total on top of data shown...
I have Looping like this in my blade.php
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>
and output will like this:
And now how to get output total before firs row like image below?
The laravel collection has an inbuilt function called sum which returns the sum of Collection.
Learn here about collection function
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Total Pencil</td>
<td>{{ $datas->sum('amount') }}</td>
</tr>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>
convert object to array
$datasArray = json_decode(json_encode($datas), true);
collect all 'amount' in an array
$amountArray = array_column($datasArray,'amount');
Sum all element of $amountArray
$total = array_sum($amountArray );
print $total where you want
In a database table - about_us, I have a column named subject
I am trying to make a table from this data in laravel
Column subject looks like this:
[
{
"name1": "1",
"name2": "2"
},
{
"name1": "3",
"name2": "4"
}
]
from controller
public function about()
{
$about = About::find(1);
return view('pages.about-us', compact('about'));
}
in about-us.blade.php
{{$about->subject}}
How can I extract the data to make a table like this?
The contents of your subject property is a JSON string. Which means that you will have to decode it through json_decode() into an array/object before your can iterate over it.
<table>
<thead>
<tr>
<th>right</th>
<th>left</th>
</tr>
</thead>
<tbody>
#foreach(json_decode($about->subject) as $subject)
<tr>
<td>{{ $subject->name1 }}</td>
<td>{{ $subject->name2 }}</td>
</tr>
#endforeach
</tbody>
</table>
Try code below:
<table>
<tr>
<td>right</td>
<td>left</td>
</tr>
#foreach(json_decode($about->subject) as $subject)
<tr>
<td>{{ $subject->name1 }}</td>
<td>{{ $subject->name2 }}</td>
</tr>
#endforeach
</table>
<table>
<thead>
<th><strong>right</strong></th>
<th><strong>left</strong></th>
</thead>
<tbody>
#foreach($about->subject as $subject)
<tr>
<td>{{ $subject->name1 }}</td>
<td>{{ $subject->name2 }}</td>
</tr>
#endforeach
</tbody>
</table>
Just loop over the data
Try this
<table>
<thead>
<tr>
<th>Right</th>
<th>Left</th>
</tr>
</thead>
<tbody>
#foreach($about->subject as $subject)
<tr>
<td>{{ $subject->name1 }}</td>
<td>{{ $subject->name2 }}</td>
</tr>
#endforeach
</tbody>
</table>
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
I am trying to have a one to many relationship, each Customer can be assigned to multiple entries, here are my migration tables
customers table:
Schema::create('customers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('idtype');
$table->string('idnumber');
$table->string('company');
$table->timestamps();
and here is my assignee table:
Schema::create('assignees', function (Blueprint $table) {
$table->increments('id');
$table->string('cabinet');
$table->time('timein');
$table->time('timeout');
$table->string('refnumber');
$table->timestamps();
$table->integer('customer_id')->unsigned()->index()->nullable();
here is my assignee controller where the belongs to function:
class Assignee extends Model
{
//
protected $fillable = [
'cabinet', 'customer_id','timein','timeout','refnumber',
];
public function cust()
{
return $this->belongsTo('App\Customer');
}
}
and here is my index.blade.php
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Entry id:</th>
<th>Person Name</th>
<th>Referance No:</th>
<th>timein</th>
<th>timeout</th>
<th width="280px">Action</th>
</tr>
#foreach ($assignees as $assignee)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $assignee->id }}</td>
<td>{{$assignee->customer-name}}</td>
<td>{{ $assignee->refnumber }}</td>
<td>{{ $assignee->timein }}</td>
<td>{{ $assignee->timeout }}</td>
when running the page I am getting the following error:
Use of undefined constant name - assumed 'name' (View: /Users/user/Documents/Laravel/blog/resources/views/assignees/index.blade.php)
When creating the "Assignee, laravel is not enforcing the relation checks,
what am I doing wrong? should i declare the relationship in the migration folder or having it in the Model is enough?
There are two issues in your code.
There is a syntax error in this line. Which throws the mentioned issue.
<td>{{$assignee->customer-name}}</td>
It should be
<td>{{$assignee->customer->name}}</td>
But, You named your relationship as cust not customer. So you need to fix that as well.
<td>{{$assignee->cust->name}}</td>
This should fix your code.
Your problem is here <td>{{$assignee->customer-name}}</td>
it should be <td>{{$assignee->cust->name}}</td> and you missed this -> so it assumed that name is a constatnt.
You should try this;
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Entry id:</th>
<th>Person Name</th>
<th>Referance No:</th>
<th>timein</th>
<th>timeout</th>
<th width="280px">Action</th>
</tr>
#foreach ($assignees as $assignee)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $assignee->id }}</td>
<td>{{$assignee->customer->name}}</td>
<td>{{ $assignee->refnumber }}</td>
<td>{{ $assignee->timein }}</td>
<td>{{ $assignee->timeout }}</td>