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
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();
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>
Hi I'm building a human resources management system, I want to display only users that are not admin in a data table but I'm getting error dont know what I might be doing wrong. Here is my code;
<table class="table table-hover">
<thead>
<th>Username</th>
<th>Department</th>
<th>Salary</th>
</thead>
<tbody>
#foreach($this->$users()->get() as $user)
#if($user->is_admin !== 1)
<tr>
<td>{{$user->username}} </td>
<td>{{$user->department}} </td>
<td>{{$user->salary}} </td>
</tr>
#endif
#endforeach
</tbody>
Table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->boolean('is_admin')->nullable();
$table->string('department')->nullable();
$table->integer('salary')->nullable();
$table->string('image',255)->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
you can try #continue to skip loop
<table class="table table-hover">
<thead>
<th>Username</th>
<th>Department</th>
<th>Salary</th>
</thead>
<tbody>
#foreach($this->$users()->get() as $user)
#php
if($user->is_admin == 1){
#continue
}
#endphp
<tr>
<td>{{$user->username}} </td>
<td>{{$user->department}} </td>
<td>{{$user->salary}} </td>
</tr>
#endforeach
</tbody>
Instead of looping through the users table and checking every user to see if they are an admin user, you can extend your query by only selecting users that aren't admin users in the first place.
where('is_admin', '!=' 1)
This way you'll already have all the users that you want to show and you won't have to add extra logic to the view.
I had forgotten to write code in the controller
public function addEmployee()
{
$users = User::all();
return view('admin/addEmployee')->with('users',$users);
}
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>
I need to make a survey for my website. I have this:
Route:
Route::resource('survey','surveyController');
Controller:
public function index()
{
$show=survey_title::find(1);
return view('survey',compact('show'));
}
Here we just read first survey.
Migrations:
Schema::create('survey_titles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
Schema::create('survey_questions', function (Blueprint $table) {
$table->increments('id');
$table->integer('survey_title_id')->unsigned()->index();
//$table->integer('user_id')->unsigned()->index();
$table->foreign('survey_title_id')->references('id')->on('survey_titles')->onDelete('cascade')->onUpdate('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('answer');
$table->integer('voted')->nullable();
$table->timestamps();
});
Models:
survey_question:
public function survey_titles(){
return $this->hasOne('App\survey_title');
}
public function users(){
return $this->hasOne('App\User');
}
survey_title:
public function survey_questions(){
return $this->hasMany('App\survey_question');
}
user:
public function survey_questions(){
return $this->hasOne('App\survey_question');
}
View:
<div class="col-md-8 col-md-offset-2">
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>Voted by 0 People </th>
<th>{{$show->title}}</th>
<th>Question ID: {{$show->id}}</th>
</tr>
<tr>
<th>Check</th>
<th>Answer</th>
<th>ID</th>
</tr>
</thead>
<tbody>
#foreach($show->survey_questions as $ans)
<tr>
<td><input type="radio" name="check[]"></td>
<td>{{$ans['answer']}}</td>
<td>{{$ans['id']}}:</td>
</tr>
#endforeach
</tbody>
</table>
<a class="btn btn-primary" href="{{action('surveyController#update',$ans->id)}}">Vote</a>
</div>
Now I put data manually in database and I retrieve.
When click on my 'Vote' I want it to send my Answer Id but instead it always sends last ID from table.
What's wrong?
This is happening because the:
<a class="btn btn-primary" href="{{action('surveyController#update',$ans->id)}}">Vote</a>
is out of the foreach loop, so after the loop is finished, the last id is being set to the link.
Put the href link inside the foreach loop, so it generates a link for each answer
<tr>
<th>Voted by 0 People </th>
<th>{{$show->title}}</th>
<th>Question ID: {{$show->id}}</th>
<th>Vote</th>
</tr>
#foreach($show->survey_questions as $ans)
<tr>
<td><input type="radio" name="check[]"></td>
<td>{{$ans['answer']}}</td>
<td>{{$ans['id']}}:</td>
<td><a class="btn btn-primary" href="{{action('surveyController#update',$ans->id)}}">Vote</a></td>
</tr>
#endforeach