Laravel Trying to get property of non-object when i get data - laravel

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

Related

Button in table row redirect to another page with the data of the row in Laravel

I would like to insert a button in every row of a table and when I click on this button, it redirect me to another page with the data of the single table row using Laravel
How can I do this?
This is my form:
<html>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">USERNAME</th>
<th scope="col">MAC ADDRESS</th>
</tr>
</thead>
<tbody>
#foreach ($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->username}}</td>
<td>{{$item->mac_addr}}</td>
<td>
<form action="{{url('singleDevice')}}" method="get">
<button class="btn btn-primary" type="submit">Select</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</body>
</html>
This is my controller:
class DeviceController extends Controller
{
public function index()
{
$data=Device::all();
return view('device', compact("data"));
}
public function create()
{
return view('registrationDevice');
}
public function store(Request $request)
{
$input = new Device;
//On left field name in DB and on right field name in Form/view
$input -> username = $request->username;
$input -> mac_addr = $request->mac_address;
$input->save();
return redirect('registrationDevice')->with('message', 'DATA SAVED');
}
public function show(Device $device)
{
return view('singleDevice');
}
}
Thanks in advance
Change your form like :
<tbody>
#foreach ($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->username}}</td>
<td>{{$item->mac_addr}}</td>
<td>
Select
</td>
</tr>
#endforeach
</tbody>
If you want to use route name, you can change by :
<td>
Select
</td>
Change your route like :
Route::get('/singleDevice/{deviceID}', [DeviceController::class, 'show'])->name('show');
Change your show function of the controller like:
public function show($deviceID)
{
$device = Device::firstWhere('id', $deviceID);
return view('singleDevice', compact("device"));
}
Why do you need a form? use the link
<form action="{{url('singleDevice')}}" method="get">
<button class="btn btn-primary" type="submit">Select</button>
</form>
replace with
Select
and configure the URL in the router as /singleDevice/{device}
Route::get('/singleDevice/{device}', [DeviceController::class, 'show'])->name('show');
You also can use this as you have already used form in your code
<form action="{{ route('show', $item->id) }}" method="get">
<button class="btn btn-primary" type="submit">Select</button>
</form>
public function show(Device $device)
{
return view('singleDevice');
}
For Route you can pass $device:
Route::get('/singleDevice/{$device}', [DeviceController::class, 'show'])->name('show');

How to fix " (1/1) ErrorException compact(): Undefined variable: categories in AdminPostsController.php line 23 '"

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'));
}

Foreach loop has undefined variable in my view

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

How to render all data in relation in laravel

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

laravel 5.2 relationship not showing

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/view‌​s/cfs/step_3.blade.p‌​hp

Resources