Data is not displayed in view Laravel - php

I am trying to display data from database to detail.blade.php, but no data is displayed in the view.
Here is the detail.blade.php view:
<h6 class="card-title">Order No. {{ $order->order_number }}</h6>
<form>
<div class="form-group">
<label for="date">Date</label>
<input type="text" class="form-control" name="order_date" value="{{ $order->order_date}}" readonly>
</div>
<div class="form-group">
<label for="handle">Handle by</label>
<input type="text" class="form-control" value="." readonly>
</div>
<div class="form-group">
<label for="status">Status</label>
<input type="text" class="form-control" name="status" value="{{ $order->status}}" readonly>
</div>
<div class="form-group">
<label for="subtotal">Subtotal</label>
<input type="text" class="form-control" name="billing_subtotal" value="{{ $order->billing_subtotal}}" readonly>
</div>
</form>
OrderMenuController:
public function show(Order $order)
{
$data = DB::table('order_menu')
->join('menus', 'menus.id', '=', 'order_menu.menu_id')
->join('orders', 'orders.id', '=', 'order_menu.order_id')
->select('orders.*', 'menus.name', 'order_menu.quantity')
->where('orders.id', $order->id)
->get();
return view('admin.order.detail')->with([
'order' => $order,
'data' => $data,
]);
}
And for the route:
Route::namespace("App\Http\Controllers\Admin")->prefix("admin")->name("admin.")->middleware('can:adminpage')->group(function () {
Route::resource("/ordermenu", OrderMenuController::class);
});
Tried dd($order) in the controller, and this is what comes up:
How to solve this? thank you

your dd() shows that you don't have any attributes for the order instance, so it makes sense that you got any result back
Try To change the resource route into orders it might work for you

Related

Request contains null although the input has been entered

I'm trying to update my data with Laravel. I'm able to create, read, delete the data but somehow i cannot update my data. I already checked my controller,model,route and view but i don't think there's any typo or anything. It only redirects to it's index page without being updated although i have entered new input. There's no error message at all so i checked where is the problem. So i checked my update function in my controller and tried to show the request by echo "$request->kode_kontak"; and echo $request->kode_kontak; but it shows nothing which i assume that it's null/empty but when i echo "yes" it showed on the screen "yes" i tested this because i want to know if the function itself is working so the problem here is that the request contains null, no wonder i cannot update it. Why is the request isn't passed? why is it like this? and how to fix it?
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit')->name('contact.edit');
Route::patch('contact/{contact}','ContactController#update')->name('contact.update');
Controller with edit and update function
use Illuminate\Http\Request;
use App\Contact;
use DB;
public function edit($kode_kontak){
$contact = DB::table('contact')->where('kode_kontak',$kode_kontak)->get();
return view('contact.edit',['contact' => $contact]);
}
public function update(Request $request){
DB::table('contact')->where('kode_kontak',$request->kode_kontak)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Model
class Contact extends Model
{
public $timestamps = false;
protected $table = 'contact';
protected $fillable = [
'kode_kontak',
'kode_pegawai',
'email',
'telepon'
];
protected $primaryKey = 'kode_kontak';
}
View of edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="{{ route('contact.update', ['kode_pegawai' => $p->kode_pegawai]) }}" method="POST">
#csrf
#method('patch')
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
Your issue is that you have disabled those inputs. Disabled inputs will not be submitted.
If you want to display the disabled inputs, but still PATCH the values, you will need to add hidden inputs with those values like:
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
<input type="hidden" name="kode_kontak" value="{{ $p->kode_kontak}}">
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
<input type="hidden" name="kode_pegawai" value="{{ $p->kode_pegawai}}">
</div>
Hope that helps!
$request->kode_kontak is $contact here, $request->kode_kontak is not available in $request, change $contact instead :
public function update(Request $request, $contact){
DB::table('contact')->where('kode_kontak',$contact)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}

ErrorException Trying to get property of non-object laravel

umm hello.
Im new in laravel and i want to create a program for list of workers.
I'm trying to access this route:
http://127.0.0.1:8000/posts/create
and there's error message.
ErrorException
Trying to get property '{"role":"pegawai","name":"asdasdasdasd1","email":"1asdad#ifocaproject.com","updated_at":"2020-05-11T18:26:31.000000Z","created_at":"2020-05-11T18:26:31.000000Z","id":7}' of non-object
this is my controller.
public function create(Request $request)
{
$user = new \App\User;
$user->role = 'pegawai';
$user->name = $request['nama_pegawai'];
$user->email = $request['email'];
$user->password = bcrypt('rahasia');
$user->remember_token = Str::random(60);
$user->save();
$request ->request->add(['user_id'-> $user->id]);
$pegawai = \App\Pegawai::create($request->all());
return redirect('/pegawai')->with('sukses','Data Berhasil Di-input');
}
and this is my blade.
<div class="modal-body">
<form action="/pegawai/create" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="exampleFormControlInput1">Nama Pegawai</label>
<input name="nama_pegawai" type="text" class="form-control"
id="exampleFormControlInput1" placeholder="Joni">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Email</label>
<input name="email" type="text" class="form-control"
id="exampleFormControlInput1" placeholder="eve#ifocaprojec.com">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Jenis Kelamin</label>
<select name="jenis_kelamin" class="form-control" id="exampleFormControlSelect1">
<option value="Laki-Laki">Laki-laki</option>
<option value="Perempuan">Perempuan</option>
<option value="-none-">-none-</option>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Umur</label>
<input name="umur" type="text" class="form-control" placeholder="Cth:21">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Agama</label>
<input name="agama" type="text" class="form-control" placeholder="Cth:Islam">
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Alamat</label>
<textarea name="alamat" class="form-control" rows="3"></textarea>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Divisi</label>
<select name="divisi" class="form-control">
<option value="Inbound">Inbound</option>
<option value="Outbound">Outbound</option>
</select>
</div>
</div>
table user:
table pegawai:
What Am I missing? Any help would be greatly appreciated, Thanks.
And I'm sorry, I'm not very good at English.
You may have typo in code.
Here it must be => instead of ->:
$request->request->add(['user_id' => $user->id]);
I believe you need to reload data from DB after ->save():
.......
$user->save();
$request ->request->add(['user_id'-> $user->fresh()->id]);

POST request is not tallied with the form in Laravel Blade

I am a newbie in Laravel.
My problem is, when I tried to update my form, it kept saying that the tablename not found eventhough I already mentioned it inside my Model. But when I debugged, I found that the request is not the same from what I put inside my form.
But my form actually doesn't have that.
Any idea how this is happening, guys? Pretty sure I missed something but unsure what is it.
Event Model
class Event extends Model
{
//
protected $fillable = ['title', 'objective', 'date', 'venue', 'description', 'slug'];
public function getRouteKeyName()
{
return 'slug';
}
}
Event Controller
class EventController extends Controller
{
public function update(Request $request, Event $event)
{
//
$validated = $request->validate([
'title' => 'required|string|unique:event|min:5|max:100',
'objective' => 'required|string|min:5|max:2000',
'date' => 'required|string|min:5|max:2000',
'venue' => 'required|string|min:5|max:2000',
'description' => 'required|string|min:5|max:2000'
]);
// Create slug from title
$validated['slug'] = Str::slug($validated['title'], '-');
// Update Post with validated data
$event->update($validated);
// Redirect the user to the created post woth an updated notification
return redirect(route('events.edit', [$event->slug]))->with('notification', 'Event updated!');
}
Edit Blade Page
<form method="post" action="{{ route('events.update', [$event->slug]) }}">
#csrf
#method('patch')
#include('partials.errors')
<div class="field">
<label class="label">Title</label>
<div class="control">
<input type="text" name="title" value="{{ $event->title }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Objective</label>
<div class="control">
<textarea name="content" class="textarea" placeholder="Content" minlength="5" maxlength="2000" required rows="10">{{ $event->objective }}</textarea>
</div>
</div>
<div class="field">
<label class="label">Date</label>
<div class="control">
<input type="text" name="title" value="{{ $event->date }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Venue</label>
<div class="control">
<input type="text" name="title" value="{{ $event->venue }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Description</label>
<div class="control">
<textarea name="content" class="textarea" placeholder="Content" minlength="5" maxlength="2000" required rows="10">{{ $event->description }}</textarea>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-link is-outlined">Update</button>
</div>
</div>
</form>
Thank you for your time!
You need to change the name attribute of Date, Venue and Description and Objective.
On your edit blade page, the input names are alternating 'title' and 'content'
can you rename the input names so that they can be unique

I have the following error: "Type error: Too few arguments to function AlbumController::postEdit(), 1 passed and exactly 2 expected"

I have the following problem when trying to edit an "album", hopefully they can help me, I'm a little frustrated haha.
The Form
<form name="editalbum" action="{{ action('AlbumController#postEdit', $album->id) }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<fieldset>
<h2>Editar <strong>{{$album->name}}</strong></h2>
<br></br>
<div class="form-group">
<label for="name">Nombre del proyecto</label>
<input name="name" type="text" class="form-control" value="{{ $album->name }}" required>
</div>
<div class="form-group">
<label for="description">Descripción del proyecto</label>
<textarea name="description" rows="10" cols="50" type="text" class="form-control" value="{{ $album->description }}" required></textarea>
</div>
<div class="form-group">
<label for="location">Locación:</label>
<input name="location" type="text" class="form-control" value="{{ $album->location }}" required>
</div>
<div class="form-group">
<label for="year">Año:</label>
<input name="year" type="text" class="form-control" value="{{ $album->year }}" required>
</div>
<button type="submit" class="btn btn-primary">Editar</button>
</fieldset>
</form>
So far I think everything is going well because I try to post in the ID of the model.
The function:
public function postEdit(Request $request, $id)
{
$album = Album::find($id);
$album = Album::all();
if(count($album) > 0){
$album->name = Input::get('name');
$album->description = Input::get('description');
$album->year = Input::get('year');
$album->location = Input::get('location');
$album->save();
Alert::success('Successfully Updated', 'Congratulations');
return view('admin.dashboard');
} else {
Alert::error('Facilities not found', 'Error');
return view('galeries');
}
I think you made error in routes.php
It should look like this:
Route::post('albums/update/{id}', ['uses' => 'AlbumController#postEdit']);
One solution will be to remove the DI Request object
public function postEdit($id)
{
//rest of code
}
note: the param has to be passed as a array
action="{{ action('AlbumController#postEdit', ['id' => $album->id]) }}"

How to get array data in form's value field in laravel?

I am passing a variable with form view to get values from database in form.
public function getGeneralSettings()
{
$generalsetting = DB::table('generalsettings')
->where('id', '=', 1)
->get();
return view('backend.generalsettings.add-general')->with('general', $generalsetting);
}
What i am getting is :
[{"id":1,"sitename":"eqwe","tagline":"ewqewq","logo":"1492084338.jpg","firstemail":"qwewq","secondemail":"ewqewq","firstphone":21321,"secondphone":32132,"address":"
dassa<\/p>","facebook":"dqw","twitter":"q","linkedin":"q","google":"q","youtube":"q","instagram":null,"reddit":"q","rss":"q","created_at":"2017-04-13 11:52:18","updated_at":"2017-04-13 11:52:18"}]
I wana show these values in my form fields.
<div class="form-group">
<label for="sitename">Site Name</label>
<input type="text" id="sitename" name="sitename" class="form-control" value="" placeholder="Site name">
</div>
<div class="form-group">
<label for="tagline">Tagline</label>
<input type="text" id="tagline" name="tagline" placeholder="Tagline" value="" class="form-control">
</div>
How to call them in particular value field. Please help
simply use {{}}
<div class="form-group">
<label for="sitename">Site Name</label>
<input type="text" id="sitename" name="sitename" class="form-control" value="{{$general->sitename}}" placeholder="Site name">
</div>
<div class="form-group">
<label for="tagline">Tagline</label>
<input type="text" id="tagline" name="tagline" placeholder="Tagline" value="{{$general->tagline}}" class="form-control">
</div>
Also use first() instead of get() to get single object
Change your controller code to
public function getGeneralSettings()
{
$generalsetting = DB::table('generalsettings')
->where('id', '=', 1)
->first();
return view('backend.generalsettings.add-general')->with('general', $generalsetting);
}
Whenever you want to retrieve a single row result, use first() instead of get(), get() is used for getting multiple records.

Categories