Information from multiple tables in one foreach loop. Laravel 8 - php

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();

Related

Retrieve data from multiple tables using their relationships-Laravel

I have a table I need to complete, for now, it has 3 columns DATE, SHIFT and STATION.
I only want the data that is attached to the logged-in user.
I have a pivot table where I can call the data from for DATE and SHIFT, but Stations is a separate table,
My pivot table is to display my many to many relationships between USER and SHIFTS called shift_user. this works,
the STATIONS table relationship is a one to many relationship with USER
as a User will belong to one station, but a station will have multiple users
Stations model
public function user()
{
return $this->hasMany('App\User');
}
}
User Model
public function station()
{
return $this->belongsTo('App\Station');
}
Controller for Project
$user = Auth::user();
return view('layouts/timesheet/index', ['user' => $user]);
}
Blade view to get info into tables
<table class="table table-sm table-success">
<tr>
<th scope="col">DATE/DAY</th>
<th scope="col">SHIFT</th>
<th scope="col">STATION</th>
</tr>
<tbody class="table-light">
#foreach ($user->Station as $station)
#endforeach
#foreach ($user->shifts as $shift)
<tr>
<td>{{ $shift->created_at }}</td>
<td>{{ $shift->Name }}</td>
<td>{{ $station->Name }}</td>
</tr>
#endforeach
Here is a picture of the table I want to fill in. I want the Station name to display, but I just get this ID which I think is the user I.D
Please can you let me know what I can do to call the data from another table via the relationship?
Thank you for any help
Make sure you create a function that's same as when you call it on Blade View.
Station.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function stations()
{
return $this->hasMany('App\Station');
}
Then on blade view, try this:
#foreach($user as $u)
<tr>
<td>{{ $u->shift->created_at }}</td>
<td>{{ $u->shift->Name }}</td>
<td>{{ $u->stations->name }}</td> // I assume the column in stations table is "name"
</tr>
#endforeach

Laravel 8: Property [name] does not exist on this collection instance

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>

BelongsTo relationship in laravel

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>

Laravel inverse relation

So i have two tables:
Schema::create('goods', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('amount');
$table->integer('shop_id');
$table->timestamp('onPurchase');
$table->timestamps();
});
Schema::create('shops', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('adress')->nullable();
$table->timestamps();
});
I want to print list of all goods and add name of the shop to every line of the good. When route opens this function runs:
function toBuy(){
$good = Good::all();
$data ['good'] = $good;
return view('tobuy', $data);
}
Blade:
#foreach ($good as $ginfo)
<tr>
<td>{{$ginfo->name}}</td>
<td>{{$ginfo->amount}}</td>
<td>
#foreach ($ginfo->shop as $shop)
{{$shop->name}}
#endforeach
</td>
<td>
Delete
</td>
</tr>
#endforeach
So how do i form a proper relationship using models Good and Shop and print what i need. Tried many ways and failed. Please help.
Add this to your Good model
public function shop()
{
return $this->belongsTo('App\Shop');
}
Controller code
public function toBuy()
{
$goods = Good::with('shop')->get();
return view('tobuy')->with(['goods' => $goods]);
}
View code
#foreach ($goods as $good)
<tr>
<td>{{ $good->name }}</td>
<td>{{ $good->amount }}</td>
<td>{{ $good->shop->name }}</td>
<td>
Delete
</td>
</tr>
#endforeach

Obtaining field from table using primary key

Using Laravel and Revisionable Package for tracking changes. In my view I'm populating my table:
#foreach($revisions as $revision)
#if($revision->key == 'created_at' && !$revision->old_value)
<tr>
<td>{{ $revision->revisionable_type }}</td>
<td>{{ $revision->revisionable_id }}</td>
<td width="50">{{ $revision->userResponsible()->first_name }}</td>
<td Width="50"></td>
<td></td>
<td>{{ $revision->newValue() }}</td>
<td width="150">{{ $revision->created_at }}</td>
</tr>
#else
<tr>
<td>{{ $revision->revisionable_type }}</td>
<td>{{ $revision->revisionable_id }}</td>
<td width="50">{{ $revision->userResponsible()->first_name }}</td>
<td width="50">{{ $revision->fieldName() }}</td>
<td>{{ $revision->oldValue() }}</td>
<td>{{ $revision->newValue() }}</td>
<td width="150">{{ $revision->updated_at }}</td>
</tr>
#endif
#endforeach
The second column {{ $revision->revisionable_id }} happens to be the primary key of the user record that was modified. I would like to return the email address or first_name/last_name of that users record. I'm fairly new at this and could use a push in the right direction!
Thanks!
You can access the model that given revision relates to by accessing the revisionable relation of that revision. In your case, in order to display the email property of related model, you could do the following:
<td>{{ $revision->revisionable->email }}</td>
from the variable names revisionable_type & revisionable_id I assume you're using many to many Polymorphic relation. based on that
you've to define a relation in the Revision Model with the User like so:
public function users()
{
return $this->morphedByMany('App\User', 'taggable');
}
so you can use all the revisions related to this user $revision->users this will return array
and if you're sure that you've only one record related to this object like your case as I think. you can use $revision->users->first()->email or username, etc..
Laravel Docs

Categories