Fetching particular data from <table> (using Id) in Laravel - php

View:
<td>
<div class="template-demo">
<button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt/{$id}') }}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>
</td>
Route:
`Route::get('View_PL_Accnt', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt/{$id}');`
Controller:
public function View_PL_Accnt(Request $request){
$id = $request->id;
$data = User::find($id);
return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id']));
}
View_PL_Accnt.blade.php :
<h3 class="SupAd_name">{{ data['name'] }}</h3>
Error:
Use of undefined constant data - assumed 'data' (this will throw an Error in a future version of PHP) (View: C:\Users\CuatrosMarias\Documents\GitHub\IPS\resources\views\dashboards\SupAd\pages\index\View_PL_Accnt.blade.php)
Error

You need to send variables using with() in your controller
return view('dashboards.SupAd.pages.index.View_PL_Accnt')->with('data',$data)->with('id',$id);
Your View Accnt.blade.php you have used data as constant you need to use it as variable
Eloquent result gives object so you can access the name property of your result object like below
{{ $data->name }}

typo error {{ $data['name'] }} $ is missing at the view

Try this one works fine in my side always..
->with(compact('data', 'id'));

View:
<button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt', [$user->id]) }}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>
Route:
Route::get('View_PL_Accnt/{id}', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt');
Controller:
public function View_PL_Accnt($id){
$data = User::find($id);
return view('dashboards.SupAd.pages.index.View_PL_Accnt', compact (['data', 'id']));
View_PL_Accnt.blade.php :
<h3 class="SupAd_name">{{ $data->name }}</h3>

Related

How to get the index of the field from my array properly in Laravel blade?

This is the "show" function in my Controller :
public function show($nform)
{
$plan = PlanFormation::findOrFail($nform);
$plan_props = PlanFormation::select(
'clients.raisoci',
'clients.nrc_entrp',
'intervenants.nom',
'intervenants.prenom')
->join('clients', 'clients.nrc_entrp', '=', 'plan_formations.nrc_e')
->join('intervenants', 'intervenants.id_interv', '=', 'plan_formations.id_inv')
->where('plan_formations.n_form', '=', $nform)
->get();
return view('planformation.detail', ['plan_props' => $plan_props, 'plan' => $plan]);
}
Here is my Blade :
<div class="card-header">
<a class="btn btn-dark btn-sm bu-lg-ic" href="/planformation"><i class="fa fa-arrow-left"></i></a>
<h3 class="card-title card-h3">Plan N° {{ $plan->n_form }} > {{ $plan_props['raisoci'] }}</h3>
</div>
I got "Undefined index 'raisoci' when I use $plan_props['raisoci'] in the blade
In your example $plan_props is not an array with element ['raisoci'] but a collection of items. So you can get the first element of this collection and call its 'raisoci' property:
{{ $plan_props[0]['raisoci'] }}
or
{{ $plan_props[0]->raisoci }}
You may also call first() method instead of get() in your query. In this case you'll get exactly what you expected in your example:
$plan_props = PlanFormation::select(...)
...
->where('plan_formations.n_form', '=', $nform)
->first();
It worked when I used ->first() and I left {{ $plan_props['raisoci'] }} as it's
The third answer is the correct answer
Thank you Manssor for your help

invalid argument supplied for foreach() laravel relationship

I'm trying to show a book's comments by ISBN (book table's PK) and I get the following error:
Invalid argument supplied for foreach
I'm using Laravel relationship and a foreach to get the records from the following query, in a Helper class:
function getCommentsByISBN($data)
{
foreach (Book::where("ISBN", $data)->get() as $comments) {
return $comments->comments;
}
}
And here's my Book model, comment relationship:
public function comments()
{
return $this->hasMany(Comment::class, "ISBN", "ISBN");
}
And here's the view where I show the comments:
#foreach(getCommentsByISBN(session("isbn")) as $comment)
<div class="form-group">
<h3>
{{ getUserWhoPostedComment($comment->email) }}
</h3>
<p>
{{ $comment->commentary }}
</p>
#if(Session::has("username") && isAdmin(session("username")))
<button type="button" onclick="openModal('{{$comment->commentary}}')"
name="warningButton" class="btn btn-warning" data-toggle="modal"
data-target="#modalWarning">
<i class="fas fa-exclamation-circle"></i>
</button>
#endif
<hr>
<span>
Hace: {{ getTimeWherePostedComment($comment->publicated_at) }}
</span>
</div>
#endforeach
Thanks in advance!
I think you helper returns void because no book was found with that ISBN.
I think it should look like this btw
function getCommentsByISBN($isbn)
{
if ($book = Book::where("ISBN", $isbn)->first()) {
return $book->comments;
}
}
and in your blade your top should look like
#php
$comments = getCommentsByISBN(session("isbn")) ?: [];
#endphp
#foreach($comments as $comment)
<div class="form-group">
/// Rest of the code

Creating delete query in Laravel

I'm kind of new using laravel.
I created delete function but it doesnt work as I wanted to. Here are the codes in view, controller as well as routes. Could you guys tell me what was wrong in the code? Thanks
View:
<div class="btn-group">
<a class="btn btn-info" href="{{ URL::to('/delete_data_tanah/{id}') }}">
<i class="fa fa-close"
onclick="return confirm('Are you sure you want to delete this data?');">----</i>
</a>
</div>
Controller:
public function delete($id){
\App\Tbl_object::where('id_objects', '=', $id)->delete();
return redirect('/list_tanah')->with('Success', 'Data telah dihapus');
}
Routes:
Route::post('/delete_data_tanah/{id}', 'formulir_tanah#delete');
Controller:
class formulir_tanah extends Controller
{
public function index()
{
$query_tanah = \App\Tbl_object::where('id_objects_referencies', '=', '1')->get();
$query_view = \App\Tbl_view::where('id_objects_referencies', '=', '1')->get();
$data = ['page_title' => 'Kertas Kerja Penilaian Tanah', 'query_tanah' => $query_tanah, 'query_view' => $query_view];
return view('admin/list_tanah')->with($data);
}
}
Try this:
in your blade.php, change {id} to {$data->id}
<div class="btn-group">
<a class="btn btn-info" href="{{ URL::to('/delete_data_tanah/{$data->id}') }}">
<i class="fa fa-close"
onclick="return confirm('Are you sure you want to delete this data?');">----</i>
</a>
then in your controller, add the id variable:
class formulir_tanah extends Controller { public function index(){
//ambil semua data dari table categories
$query_tanah = \App\Tbl_object::where('id_objects_referencies', '=', '1')->first();
$query_view = \App\Tbl_view::where('id_objects_referencies', '=', '1') ->first();
$data = ['id' => $query_view->id, 'page_title' => 'Kertas Kerja Penilaian Tanah', 'query_tanah' => $query_tanah, 'query_view' => $query_view ];
return view('admin/list_tanah')->with($data);
}
your code doesn't work since laravel doesn't know what row to delete since there is no specific id in your return.
Apply these changes to your code:
Route::delete('/delete_data_tanah/{id}', 'formulir_tanah#delete');
and in your view you need to wrap the button in a form
<form action="{{ URL::to('/delete_data_tanah/{id}') }}" method="post">
#method('DELETE')
#csrf
<button class="btn btn-danger" type="submit">Delete</button>
</form>

Laravel 5.2 - Delete from db

I am using Laravel Framework version 5.2.45.
I have created a simple view that outputs my todos:
#foreach($todos as $todo)
{{ $todo->todo }} <button href="{{ route('todo.delete', ['id' => $todo->id]) }}" class="btn btn-danger">x</button>
<hr>
#endforeach
Within my routes I have created the following route to delete a todo:
Route::get('/todo/delete/{id}', [
'uses' => 'TodosController#delete',
'as' => 'todo.delete'
]);
Within my TodosController I created the following delete method:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Todo;
class TodosController extends Controller
{
public function delete($id) {
$todo = Todo::find($id);
$todo->delete();
return redirect()->back();
}
// ...
When I press the button in the frontend nothing happens. I do not get any error...
Any suggestions what is wrong with my implementation?
Appreciate your replies!
You are using button not tag
turn your code from
#foreach($todos as $todo)
{{ $todo->todo }} <button href="{{ route('todo.delete', ['id' => $todo->id]) }}" class="btn btn-danger">x</button>
<hr>
#endforeach
to
#foreach($todos as $todo)
{{ $todo->todo }} x
<hr>
#endforeach
Try Below code, You have used a button instead of a tag
#foreach($todos as $todo)
{{ $todo->todo }} x
<hr>
#endforeach
You should do like this :
Delete Button :
<a class="btn btn-primary" href="{{ route('todo.delete',$todo->id) }}">Delete</a>
And delete function look like below :
public function delete($id) {
try {
$delete_flag = Todo::where(['id' => $id])->first();
$delete_flag->delete();
return redirect()->back()->with('success', 'Todo deleted successfully');
} catch (Exception $ex) {
return redirect()->back()->with('error', 'Something went wrong');
}
}
#foreach($todos as $todo)
{{ $todo->todo }} x
#endforeach
delete code--
$toDo = Todo::findOrFail($id)->delete();
if($toDo){
return response()->josn(['message'=>'deleted']);
}

Laravel Undifined Variable in View

I cant't figure out what's going wrong with a link from my "welcome" page to a "profile" page.
It says "Undefined variable: restaurants (View: /Users/beyerdynamic/Documents/Developer/dev1/resources/views/welcome.blade.php)"
The strange thong is that I pass the Variable to my view.
My PagesController is:
public function welcome() {
$restaurants = User::orderByRaw('RAND()')->take(3)->get();
return view('welcome')->withRestaurants($restaurants);
}
My View is:
#foreach($restaurants as $key => $restaurant)
<div class="col-md-4">
<div class="card">
<div class="image">
<img src="{{asset('images/frontend/profile/dummy/profilehero/hero4.png')}}" alt="..." />
<div class="filter filter-white">
<a href="{{ URL::to('profile/'.$restaurant->id)}}" type="button" class="btn btn-info btn-round btn-fill">
<i class="fa fa-heart"></i> View Restaurant
</a>
</div>
</div>
</div>
</div>
#endforeach
The Restaurants Variable is passed properly to my welcome view, but when I click the link the error occurs on /profile/{id} url.
Change return view('welcome')->withRestaurants($restaurants);
to
return view('welcome')->with('restaurants', $restaurants);
OR
return view('welcome', compact('restaurants');
Try with this,
return view('welcome')->with("restaurants",$restaurants);
OR
return view('welcome',["restaurants" => $restaurants]);
Check laravel docs : https://laravel.com/docs/master/views#passing-data-to-views
Change the following line:
return view('welcome')->withRestaurants($restaurants); // there is nothing like withRestaurants in laravel
to
return view('welcome', array('restaurants' => $restaurants));
and try again.

Categories