I have a Laravel 5.2 website and I am having trouble getting my relationship data to show. It was working fine before but I can't see my data. I am not getting any errors.
View
<table class="table table-striped">
<thead>
<tr>
<th>HAWB</th>
<th>Pallet Count</th>
<th>Piece Count</th>
<th>Weight</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#foreach($cfs->hawbs as $hawb)
<tr>
<td>
<a data-toggle="collapse" data-target="#{{$hawb->hawb}}">
{{$hawb->hawb}}
</a>
</td>
<td>{{$cfs->hawb->pallet_ct}}</td>
<td>{{$hawb->piece_ct}}</td>
<td>{{$hawb->weight_no}} {{$hawb->weight_type}}</td>
<td></td>
<div id="{{$hawb->hawb}}" class="collapse">
Insert more info here
</div>
</tr>
#endforeach
</tbody>
</table>
Controller
public function getCfsStep(Request $request, $step)
{
$cfs = DB::table('cfs_deliveries');
$account = Account::pluck('name', 'id')->all();
return view('cfs.step_' . $step, compact('account', 'cfsDelivery'), ['cfs' => $request->session()->get('cfs')]);
}
CFSDelivery Model
public function hawbs()
{
return $this->hasMany('App\Hawb', 'cfs_delivery_id');
}
Hawb Model
public function cfsDelivery()
{
return $this->belongsTo('App\CfsDelivery');
}
Edit
I'm now getting this error:
Undefined property: Illuminate\Database\Query\Builder::$hawbs (View: /Applications/XAMPP/xamppfiles/htdocs/dsd-cms/resources/views/cfs/step_3.blade.php
Related
I want to create a post but I cannot because it shows me the Undefined variable error for the compact function,
can someone help me?
Below are the samples from files:
AdminPostsController.php
public function index(){
$posts = Post::paginate(5);
return view('admin.posts.index', compact('posts','categories'));
}
admin/posts/index.blade.php
#extends('layouts.admin')
#section('content')
<h1>Posts page</h1>
<table class="table">
<thead>
<th>ID</th>
<th>Owner</th>
<th>Category</th>
<th>Photo ID</th>
<th>Title</th>
<th>Body</th>
<th>Created</th>
<th>Updated</th>
</thead>
<tbody>
#if($posts)
#foreach($posts as $post)
<tr>
<td>{{$post->id}}</td>
<td>{{$post->user->name}}</td>
<td>{{$post->category_id ? $post->category->name : 'No category'}}</td>
<td><img height="50" src="{{$post->photo ? $post->photo->file : 'http://placehold.it/400x400'}}"></td>
<td>{{$post->title}}</td>
<td>{{str_limit($post->body,10)}}</td>
<td>{{$post->created_at->diffForHumans()}}</td>
<td>{{$post->updated_at->diffForHumans()}}</td>
<td>View post </td>
<td>View comments</td>
</tr>
#endforeach
#endif
</tbody>
</table>
<div class="row">
<div class="col-sm-6 col-sm-offset-5">
{{$posts->render()}}
{{--pagination--}}
</div>
</div>
#stop
Make sure your $categories variable is defined in the index function inside your controller.
// AdminPostsController.php
public function index(){
$posts = Post::paginate(5);
$categories = Category::all();
return view('admin.posts.index', compact('posts','categories'));
}
I tried to implement exporting to excel file From View approach using the Laravel Excel. Here is the link of the documentation https://laravel-excel.maatwebsite.nl/3.1/exports/from-view.html. But I can't figure it out yet referencing the example shown in the website. It returns an error saying PhpOffice \ PhpSpreadsheet \ Writer \ Exception
Invalid parameters passed. . I've been changing my codes trying to solve this but no luck at all. Please help me figure this out. Below are my codes. Thank you.
LoansExport.php
<?php
namespace App\Exports;
use App\Loan;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class LoansExport implements FromView
{
public function view(): View
{
return view('partials.view_loan_export', [
'loans' => Loan::all()
]);
}
}
view_loan_export.blade.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>
</body>
LoansController.php
<?php
namespace App\Http\Controllers;
use App\Loan as Loan;
use App\Member as Member;
use Illuminate\Http\Request;
use App\Exports\LoansExport;
use Maatwebsite\Excel\Facades\Excel;
class LoansController extends Controller
{
public function loanexport()
{
return Excel::download(new LoansExport, 'loans.xlsx');
}
}
web.php
Route::get('/loanexport', 'LoansController#loanexport');
error
I have done By different way
LoansController.php
public function loanexport(){
$loan= array();
$loans= loan::all();
$data = [
'success' => 'success',
'loans' => $loans,
];
return Excel::download(new LoansExport($data), 'loans.xlsx');
}
LoansExport.php
public function __construct($data) {
$this->data = $data;
}
public function view(): View
{
//dd($this->data);
return view('partials.view_loan_export',$this->data);
}
view_loan_export.blade.php
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>
just put the table tag and the tag within it in your view
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>
The error is on the view which says that
my $audio variable is not defined.
I want to show everything from the database using a foreach loop.
I tried base on paginate on the laravel documentation https://laravel.com/docs/5.7/pagination#basic-usage
My controller is below:
use Illuminate\Http\Request;
use App\Audio_lectures;
use Illuminate\Support\Facades\DB;
public function index(){
$audio = DB::table('audio_lectures')->latest()->paginate();
return view('welcome', ['audio_lectures' => $audio]);
}
The welcome view is below:
<div class="container">
<div class="row">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Subject</th>
<th scope="col">Date Added</th>
<th scope="col">Download</th>
</tr>
</thead>
<tbody>
#foreach ($audio as $audio_lectures)
<tr>
<th scope="row">{{$audio_lectures->id}}</th>
<td>{{$audio_lectures->subject}}</td>
<td>{{$audio_lectures->created_at}}</td>
<td>Download</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
My route web.php is below:
Route::get('/','Audio_lecturesController#index');
I expect to show all the audio lectures from my database to my view.
try this one
#foreach ($audio_lectures as $audio)
<tr>
<td scope="row">{{$audio->id}}</td>
<td>{{$audio->subject}}</td>
<td>{{$audio->created_at}}</td>
<td>
Download
</td>
</tr>
#endforeach
you are passing from controller audio_lectures array but you
getting as in your view audio
On your view file looping try this
#foreach ($audio_lectures as $value)
<tr>
<td scope="row">{{$value->id}}</td>
<td>{{$value->subject}}</td>
<td>{{$value->created_at}}</td>
<td>Download</td>
</tr>
#endforeach
I am new in this situation, I wan't to get value of "otmain_many_otline" but I don't know how to get it, I think I should put it in array cause when I use this
$list->otmain_many_otline[0]->purpose I only get first data. My problem is I don't know the syntax how to render all data?
View
<table id="example1" class="table table-bordered table-hover">
<thead>
<th>Purpose</th>
</thead>
<tbody>
#foreach($ot_list as $list)
<tr>
<td>
{{$list->otmain_many_otline[0]->purpose}}
</td>
</tr>
#endforeach
</tbody>
</table>
Model
class OTMain extends Model
{
protected $table = 'ot_main';
public function otmain_many_otline(){
return $this->hasMany('App\OTline', 'ot_main_id','id');
}
}
class OTLine extends Model
{
protected $table = 'ot_line';
public function otline_belong_otmain(){
return $this->hasOne('App\OTMain', 'ot_main_id');
}
}
Controller
$ot_list = OTMain::whereIn('created_by', $requestor_list)
->with('otmain_many_otline')
->where('ot_status', 1)
->get();
dd($ot_list);
Output:
Your otmain_many_otline relation is hasMany meaning that it will return you collection of related objects. Make sure to read official documentation on them.
If you need to get first related object then use method first(). In your case:
#foreach($ot_list as $list)
<tr>
<td>
{{ $list->otmain_many_otline->first()->purpose }}
</td>
</tr>
#endforeach
If you need to render all related objects you can also just iterate through omain_many_otline with another #foreach:
...
<td>
#foreach ($list->otmain_many_otline as $otline)
<p>{{ $otline->purpose }}</p>
#endforeach
</td>
...
You can simply do this:
#foreach($ot_list as $list)
<tr>
<td>
#foreach($list->otmain_many_otline as $ot) // foreach for $otmain_many_otline
{{$ot->purpose}} ,
#endforeach
</td>
</tr>
#endforeach
i get this error when i get section data and the problem with section result part
the book part and author part working good
when i remove section part it working with out section_name
Laravel Framework version 5.1.25 (LTS)
Trying to get property of non-object (View: C:\xampp\htdocs\lib\resources\views\libraryViewsContainer\summary.blade.php)
my Book Model
class Book extends Model
{
public function section()
{
return $this->belongsTo('App\Section','id');
}
public function author()
{
return $this->belongsToMany('App\Author','books_authors_relationship','book_id','author_id')->withTimestamps();
}
my BooksController
public function summary(){
$all_results = Book::with('section')->with('author')->get();
return view('libraryViewsContainer.summary',compact('all_results',$all_results));
}
my summary blade
<div class="container">
<h1 class="well text-center">Library Summary</h1>
<table class="table">
<tr>
<th style="width: 25%">Section Name</th>
<th style="width: 25%">Book Title</th>
<th style="width: 25%">Book Description</th>
<th style="width: 25%">Authors</th>
</tr>
#foreach($all_results as $bookModel)
<tr>
<td>
<a href="/section/{{$bookModel->section->id}}">
<span class="label label-info">{{$bookModel->section->section_name}}</span>
</a>
</td>
<td>
{{$bookModel->book_title}}
</td>
<td>
{{$bookModel->book_description}}
</td>
<?php $authors = $bookModel->author; ?>
<td>
#foreach($authors as $author)
<a href="/author/{{$author->id}}">
<span class="label label-danger">{{$author->first_name}} {{$author->last_name}}</span>
</a>
#endforeach
</td>
</tr>
#endforeach
</table>
</div>
#stop
In controller, method "with" can be written like $all_results = Book::with('section', 'author')->get();
Method compact() takes the variable names, not the variables.
return $all_results before view to see your results to get whether you're getting the values correctly or not.
the error was in Book model
i changed
public function section()
{
return $this->belongsTo('App\Section','id');
}
to
return $this->belongsTo('App\Section');
and it worked thanks for all help me