BelongsTo relationship in laravel - php

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>

Related

Information from multiple tables in one foreach loop. Laravel 8

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

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>

How to group rows that have same Group IS as a foreign key?

I just trying to make a group of students who have the same group ID and calculate the CGPA of only those students who have belong to the same Group.
Refrence Example
Here's is the code for Controller
public function View_GroupStudent()
{
$allot_app = AllotmentApps::select(
"AllotmentApps.grpID",
"AllotmentApps.sname",
"AllotmentApps.cgpa"
)
->orderBy('grpID')
->get();
return view('deny', compact('allot_app'));
}
Views:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
<tr>
#foreach ($allot_app as $allot_app)
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ $allot_app->sum('cgpa') }}</td>
</tr>
#endforeach
</table>
Also help me in creating a proper view for that.
I was confused by your image because the red numbers weren't the averages at all. You should be able to achieve what you're trying to do with the following:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
#foreach ($allot_app->groupBy('grpID') as $grouped)
#foreach ($grouped as $allot_app)
<tr>
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ number_format($grouped->avg('cgpa'), 2) }}</td>
</tr>
#endforeach
#endforeach
</table>

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

Custom query using DB::select() on laravel 4.2 controller

I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach

Categories