I am trying display all product name,
I am getting the error :
ErrorException in 4938c589ea3db6bab0c4c788473314a4 line 46:
Undefined variable: allproduct (View: C:\Server\WebDocs\CRUD\resources\views\product.blade.php)
I had try return View::make('product',$allproduct); and
return View::make('product')->with('product',allproduct); also cannot, i don't know why, this is my first Laravel program.
in my ProductController
public function index()
{
// get all the Products
$allproduct = Product::all();
return View::make('product',$allproduct);
}
in my product.blade.php
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>No</td>
<td>Product</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
#foreach($allproduct as $key => $value)
<tr>
<td>$key</td>
<td>{{ $value->product }}</td>
<!-- we will also add show, edit, and delete buttons -->
<td>
</td>
</tr>
#endforeach
</tbody>
</table>
You could try this,
return View::make('product', compact('allproduct'));
Trying using this code in Controller:
return View::make('product')->with(['allproduct' => $allproduct]);
try passing variable like
public function index()
{
// get all the Products
$allproduct = Product::all();
return View::make('product')->with('allproduct', $allproduct);
}
and in view
what about just doing #foreach($allproduct as $product) and then in <td>, <td>{{$product->product}}</td>. and make sure your have product column in db
controller:
return View::make('product')->with('allproduct',$allproduct);
blade:
#foreach($allproduct as $key => $value)
<div>
{{ $key . ' => ' . $value }}
</div>
#endforeach
Related
Im using Laravel 8 with PHP 8 as well as barryvdh/laravel-domPDF. I have a crud that displays employee information and you can print out their payslips. When i dont pass through the $id it works correctly and prints all the employees information however when i do it returns the error:
Attempt to read property "name" on bool
This is my route:
Route::get('/print/{id}', ['App\Http\Livewire\EmployeesTable', 'print'])->name('livewire.employees-table.print');
This is my model function
public function print($id){
$employees = Employees::find($id);
$teams= Team::all();
$pdf = PDF::loadView('employees.payslip', compact('teams', 'employees'));
return $pdf->download('invoice.pdf');
}
This is my user relationship:
public function employees(){
return $this->hasMany(Employees::class);
}
This is my employees relationship:
public function user(){
return $this->belongsTo(User::class);
}
.
<thead>
#foreach ($employees as $employee)
<tr>
<td>
{{$employee->name}} {{$employee->surname}}
{{$employee->phone}}
{{$employee->role}}
</td>
</tr>
#endforeach
</thead>
<thead>
#foreach ($teams as $team)
<tr>
<td>
{{$team->companyName}}
{{$team->street}}
{{$team->compound}}
{$team->suburb}}
{$team->phone}}
</td>
</tr>
#endforeach
</thead>
</table>
I think $employees return empty result so better check before looping
#if(isset($employees)&&count((array)$employees))
#foreach ($employees as $employee)
<tr>
<td>
{{$employee->name}} {{$employee->surname}}
{{$employee->phone}}
{{$employee->role}}
</td>
</tr>
#endforeach
#endif
I want to get name from other database table, and get this error method, which part did i missed?
public function userWorkScheduleList()
{
return $this->hasMany(UserShiftPattern::class,'id','user_id');
}
<tbody>
#foreach ($usps as $usp => $usplist)
<tr>
<td>{{ $usplist->userWorkScheduleList()->name }}</td>
</tr>
#endforeach
</tbody>
Below is my controller code.
foreach ($events as $eventId) {
$eventArray[] = Events::where('id', $eventId['event_id'])->get();
}
View
#foreach ($eventArray as $key => $value)
<tr>
<td>{{ $value }}</td>
</tr>
#endforeach
The result obtained is as follows.
[{"id":1,"event_title":"yoga","event_date":"2019-06-24 00:00:00"}]
How can I display the above results in a table?
You can change get to first as you are fetching event id wise data,
// first here to fetch one data
$eventArray[] = Events::where('id',$eventId['event_id'])->first();
Then in blade you can access with object syntax as ->
#foreach ($eventArray as $key => $value)
{{ dd($value)}}
<!-- or -->
{{ $value->id }}
#endforeach
Then you will know how to use it.
Recommended approach,
// fetch the required event ids
$eventIds = array_column($events, "event_id");
// then fetch the data for those ids
$eventArray = Events::whereIn('id', $eventIds)->get();
There is no need for that loop in your controller:
$events = Events::whereIn('id', array_column($events, 'event_id'))->get();
return $this->view('yourview', compact($events));
In your view:
<table>
<thead>
<tr>
<th>Title</th>
<th>Date</th>
</tr>
</thead>
<tbody>
#foreach ($events as $event)
<tr>
<td>{{$event->event_title}}</td>
<td>{{$event->event_date}}</td>
</tr>
#endforeach
</tbody>
</table>
I keep getting this error after fixing a new code that I had just put in and I have no idea why it is giving me this error "Property [id] does not exist on this collection instance" which point to the id of my route in home.blade.php. Can someone help me take a look thanks a lot
The part that I added in was the hire_status part and after fixing it, it then gave me this error.
home.blade.php
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Hire Status: </big></strong></th>
<th><strong><big>Action: </big></strong></th>
</tr>
<td>
<tr>
#foreach($data as $value)
<tr>
<th>{{$value->Name}}</th>
<th>
#foreach($data1 as $value1)
{{$value1->hire_status}}
</th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="get">
{{ csrf_field() }}
<button type="submit">Delete</button>
</form></th>
</tr>
#endforeach
#endforeach
</tr>
</tr>
</table>
HomeController:
public function getData(){
$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
$data1 = DB::table('hires')->where('hire_status','Yes')->get();
if(count($data)>0){
return view('home',compact('data','data1'));
}else{
return view('home');
}
}
Just remove key from array,
you are trying to get data in your view directly so get it directly not to data of data...
$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
change it to,
$data = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
I was working with Laravel and I am trying to paginate my table of books. I got this error "Call to a member function links() on array in Laravel". It may be as the duplicate's error, but I still can't figure out how to solve my thing.
BookController fiddle:
public function index()
{
$books = Book::simplePaginate(3)->all();
$authors = Author::all();
$genres = Genre::all();
return view('books.all')->withBooks($books)->withAuthors($authors)->withGenres($genres);
}
books/all.blade.php fiddle
<table class="table table-hover">
<tr class="info">
<td>#</td>
<td>Name</td>
<td>Author</td>
<td><center>Visit</center></td>
</tr>
#foreach($books as $book)
<tr>
<td width="50px"><img width="50px" height="75px" src="{{ asset('images/' . $book->image) }}"></td>
<td width="50px">{{ $book->name }}</td>
<td width="50px">{{ $book->author->name }}</td>
<td width="50px"><center><button class="btn btn-success">Visit</button></td>
</tr>
#endforeach
</table>
{{ $books->links() }}
Just remove the ->all() method from the $books variable.
$books = Book::paginate(10);
paginate() function considers taking all content from the table, which is taken here by Book model