I am working in Laravel Framework, i have tried to fetch the image from the database. But it's not getting pulled properly from the DB, can anyone help me on this?
Here is my code-
public function getAllPost()
{
$posts = DB::table('posts')
->join('categories','posts.category_id','categories.id')
->select('posts.*','categories.name')
->get();
return view('posts.all_posts', compact('posts'));
}
public function getIndividualPosts($id)
{
$posts = DB::table('posts')->
join('categories','posts.category_id','categories.id')
->select('posts.*','categories.name')
->where('posts.id', $id)->first();
return view('posts.view_post', compact('posts'));
}
view.blade.php
#foreach($posts as $post)
<tr>
<td>{{ $post-> id}}</td>
<!--The name came from the categories table-->
<td>{{ $post-> name}}</td>
<td>{{ $post-> title}}</td>
<td>{{ $post-> details}}</td>
<td><img src="{{ URL::to($post->image)}}" style="height: 70px; width: 100px"></td>
<td>
Edit
Delete
View
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
Related
Departemen =
'nama_departemen',
'nama_manager',
'jumlah_pegawai',
Pegawai = 'nomor_induk_pegawai',
'nama_pegawai',
'id_departemen',
'email',
'telepon',
'gender',
'status'
PegawaiController
public function index()
{
$pegawai = Pegawai::Join('departemens','pegawais.id_departemen','=','departemens.id')->paginate(5);
return view ('pegawai.index', compact('pegawai'));
}
public function destroy($id)
{
Pegawai::find($id)->delete();
return redirect()->route('pegawai.index')->with(['success'=> 'Item Berhasil Dihapus']);
}
public function edit($id)
{
$pegawai=Pegawai::join('departemens','pegawais.id_departemen','=','departemens.id')->find($id);
$departemen = Departemen::all();
return view('pegawai.edit',compact('pegawai','departemen'));
}
pegawai.index
#forelse ($pegawai as $item)
<tr>
<td class="text-center">{{ $item->nomor_induk_pegawai }}</td>
<td class="text-center">{{ $item->nama_pegawai }}</td>
<td class="text-center">{{ $item->nama_departemen }}</td>
<td class="text-center">{{ $item->email }}</td>
<td class="text-center">{{ $item->telepon }}</td>
#if($item->gender==0)
<td>Wanita</td>
#else
<td>Pria</td>
#endif
#if($item->status==0)
<td>Inactive</td>
#else
<td>Active</td>
#endif
<td class="text-center">
<form onsubmit="return confirm('Apakah Anda Yakin ?');" action="{{ route('pegawai.destroy', $item->id ) }}" method="POST">
Edit
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Hapus</button>
</form>
</td>
</tr>
#empty
<div class="alert alert-danger">
Data Pegawai belum tersedia
</div>
#endforelse
routes.web
Route::get('/', function () {
return view('dashboard');
});
Route::resource('/departemen',\App\Http\Controllers\DepartemenController::class);
Route::resource('/pegawai',\App\Http\Controllers\PegawaiController::class);
so i tried the Hapus and Edit button. it returning id_departemen instead id from table pegawai. i assume this is because im using join. i use join because i want to show nama_departemen from table departemen in pegawai. so what should i do to fix this.
maybe table departemen should look like this
'id', 'nama_departemen', 'nama_manager', 'jumlah_pegawai'
and table pegawai should look like this :
'id','nomor_induk_pegawai', 'nama_pegawai', 'id_departemen', 'email', 'telepon', 'gender', 'status'
and the controller should look like this :
Pegawai::select ('pegawai.nomor_induk_pegawai','pegawai.nama_pegawai','departemen.nama_departemen','pegawai.email','pegawai.telepon','pegawai.gender','pegawai.status','pegawai.id as id_pegawai','departemen.id as id_departemen')->Join('departemens','pegawais.id_departemen','=','departemens.id')->paginate(5);
just choose which do you want to use, id_pegawai or id_departemen
you can change the join with leftjoin if you need
read this documention for another resolved
https://laravel.com/docs/9.x/queries#select-statements
The issue here is that your are joining two tables that both have an ID column.
To get around this you need to be specific in what columns you want to return.
For example;
$pegawai = Pegawai::Join('departemens','pegawais.id_departemen','=','departemens.id')->select('pegawais.id','departemens.otherColumnName')->paginate(5);
Hope that helps.
I have done one-to-many relationships but when I loop through it in my view only one subject displays even when I have more than one subjects.
This is what I have done in my controller
$broadsheet = Broadsheet::with(['subject', 'session', 'term', 'student', 'classarm'])
->where('session_id', $request->sessions)
->where('term_id', $request->terms)
->where('class_arm_id', $request->classarms)
->selectRaw('sum(total) as totals, count(subject_id) as countrow, student_id, subject_id, session_id, term_id, class_arm_id, total')
->groupBy('student_id')
->orderBy('totals', 'desc')
->get();
return view('broadsheet.index')->with('broadsheet', $broadsheet);
This is the index view
#extends('layouts.app')
#section('content')
{{--#foreach($broadsheet as $subjects)
#foreach ($subjects->subject as $sub)
<th> {{ strtoupper($sub->subject->subject_name) }}</th>
#endforeach
#endforeach--}}
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
#foreach($broadsheet as $subject)
<th> {{ strtoupper($subject->subject->subject_name) }}</th>
#endforeach
<th>TOTAL</th>
<th>AVERAGE</th>
<th>POSITION</th>
</tr>
</thead>
<tbody>
#foreach($broadsheet as $result)
<tr>
<td>{{ $result->total }}</td>
<td>{{ $result->totals }}</td>
<td>{{ number_format($result->totals/$result->countrow,2) }}</td>
<td>{{ $i++ }}</td>
<td>{{ $result->exams }}</td>
<td>{{ $result->total }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
and this is my Broadsheet schema
class Broadsheet extends Model
{
use HasFactory;
protected $fillable = [
'student_id',
'subject_id',
'session_id',
'term_id',
'class_arm_id',
'subject_total',
];
public function student()
{
return $this->belongsTo(Student::class, 'student_id');
}
public function subject()
{
return $this->belongsTo(Subject::class, 'subject_id');
}
public function session()
{
return $this->belongsTo(Session::class, 'session_id');
}
public function term()
{
return $this->belongsTo(Term::class, 'term_id');
}
public function classarm()
{
return $this->belongsTo(ClassArm::class, 'class_arm_id');
}
}
I want to display students results like this
Intended result
Please, I would really need assistance to get this working as any help would be greatly appreciated.I just started learning laravel recently. I don't really know where the issue is coming from
I'm trying to fetch value from different tables. I get the value but it repeat same quantity. I'm getting this see this image
I expect this see this image
Here is my controller code
$orders = Order::all();
view('admin.order.index', compact('orders'));
Here is my model relationship
return $this->belongsToMany(FoodItem::class, 'order_food_items', 'order_id', 'food_item_id')->withTimestamps();
}
public function orderItems() {
return $this->hasMany(OrderFoodItem::class);
}
public function user() {
return $this->belongsTo(User::class);
}
Here is in blade code, May be I'm doing wrong
#foreach ($orders as $key => $order)
#foreach ($order->orderFoodItems as $product)
#foreach ($order->orderFoodItemPrice as $price)
<tr>
<th scope="row">{{ $key + 1 }}</th>
<td>{{ $product->name }}</td>
<td>
<img src="{{ asset('/storage/items/food/' . $product->image) }}"
alt="">
</td>
<td>{{ $order->user->name }}</td>
<td>
<span class="badge badge-primary">Pending</span></td>
<td>something</td>
<td>{{ $price->discounted_price }}</td>
</tr>
#endforeach
#endforeach
#endforeach
Can anyone help me?
I am using Laravel with mySql. I have two table in blade view, one in main div which contains a button to open the second which is in aside div and fill it with data retrieved from the controller. Please find below my code:
my routes
// showing list of orders
Route::get('orders/show/all', 'OrderController#showOrders')->name('orders.show.all');
// showing order details
Route::get('orders/details/{id}', 'OrderController#showOrderDetails')->name('orders.details');
my controller (OrderController), copied only two functions which have to do with this
public function showOrders(){
$orders = Order::orderBy('total_amount', 'desc')
->where('user_id','=', Auth::user()->id)
->get();
return view('orders.undo_sales', compact('orders'));
}
public function showOrderDetails($id){
$ordered_item = Order::findOrFail($id);
return redirect('orders/show/all', compact('ordered_item'));
}
}
my blade.php file
<main class="col-sm-8">
#include('partials.messages')
<div class="card">
<div class="card-header">List of all sales</div>
<div class="card-body">
<table class="table ">
<thead>
<tr>
<th>Order #</th>
<th>Order Date</th>
<th>Total Amount</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ($orders as $order)
<tr>
<td>{{ $order->id }}</td>
<td>{{ $order->created_at }}</td>
<td>{{ number_format($order->total_amount) }}</td>
<td>
Order details
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</main>
<aside class="col-sm-4" id="aside">
<div class="card">
<div class="card-header">Order Details</div>
<div class="card-body">
<table class="table ">
<thead>
<tr>
<th>Item Name</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
#foreach ($ordered_item->items as $value)
<tr>
<td>{{ $value->item_name }}</td>
<td>{{ number_format($value->pivot->quantity_on_order) }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</aside>
</div>
<script>
let showDetails = document.querySelector('.show-details')
let aside = document.querySelector('#aside')
aside.style.display = 'none'
function showDiv(){
aside.style.display = 'block'
}
</script>
There is no problem with filling the main (first) table with data from the controller. The problem is getting the object returned by the function showOrderDetails($id) to the same view but on a different table and display those records. I tried to dd() the data in the controller on this method and I could see the item_name and the respective quantity. Which proved that there is no problem with this code in blade file:
#foreach ($ordered_item->items as $value)
<tr>
<td>{{ $value->item_name }}</td>
<td>{{ number_format($value->pivot->quantity_on_order) }}</td>
</tr>
#endforeach
But when I sent to the view I got the error in my view file:
Undefined variable: ordered_item
Please help me to get the data across.
Thank you.
What you're trying to do is to redirect to the same page with an additionnal variable. But it can't work like that.
A simple answer would be this:
web.php
Route::get('orders/show/all', 'OrderController#showOrders')->name('orders.show.all');
Controller.php
public function showOrders(){
$orders = Order::orderBy('total_amount', 'desc')
->where('user_id','=', Auth::user()->id)
->get();
// I have added the same condition. I believe, you don't want the user to be able to see all user's orders
$ordered_item = Order::where('user_id', '=', Auth::user()->id)
->find(request('order_id'));
return view('orders.undo_sales', compact('orders', 'ordered_item'));
}
}
blade.php
....
Order details
....
#if($ordered_item)
<aside class="col-sm-4" id="aside">
#foreach ($ordered_item->items as $value)
....
#endforeach
</aside>
#endif
im having a problem with my code.
I want to show the record of the student who login but im having an
undefined variable on my view.blade
Here's my Model
class Attendance extends Eloquent {
public function users()
{
return $this->belongsTo('User', 'id');
}
}
Here's my Controller
public function viewstudentAttendance()
{
$students = Auth::user()->id;
//load view and pass users
return View::make('student.view')
->with('attendances', $students);
}
Finally here's my view.blade
#extends('layouts.master')
#section('head')
#parent
<title>View Attendance</title>
#stop
{{ HTML::style('css/bootstrap.css'); }}
#section('content')
</ul>
#if ($students->count())
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
#foreach ($students as $students)
<tr>
<td>{{ $students->id }}</td>
<td>{{ $students->firstname }}</td>
<td>{{ $students->lastname }}</td>
#endforeach
</tr>
{{ HTML::script('js/bootstrap.js'); }}
</tbody>
</table>
#else
There are no attendance recorded yet
#endif
#stop
I think the problem is with my view or how i declare the variable? Please help? :(
public function viewstudentAttendance() {
//This code turns ID ONLY Check the website out for a code that retrieves data so you can loop it.
$students = Auth::user() - > id;
//Code that counts number of student
$studentCount = XXX(Google It)
//load view and pass users
return View::make('student.view') - > with('attendances', $students);
//Return StudentCount too
}
Inside your blade template, use :
#if ($studentCount > 10)
instead of
#if ($students->count())
Your students is returning ID, how could you "Count"
http://laravel.com/docs/4.2/eloquent
Inside your blade you kept doing if($students) bla bla, just to let you know it's attendances
->with('attendances', $students);
Attendances is the variable your blade will see, $student is the data you are pushing into attendances for blade