Laravel PHP Update HTML Table view with Drop downs - php

I have read a lot into this and I am trying to find a way to refresh an HTML table in my view.
I want to use some drop down menus to select variables that are then inserted into my queries.
In my controller I have queries that populate the drop downs, and a query that is populating the table and paginating it, but I need to find out how to make the list of entries in the table change when the drop down is changed.
I have read into this quite a bit and a lot of people use javascript with onchange, but I am having trouble figuring out how to make that work with more than one drop down changing, and if possible was interested in changing the view without changing the url, but if not need some pointers or guidance on how to keep track of the variables in the url when there is more than one filter.
Edit:
View:
#extends('layouts.master')
#section('title', 'Vulnerabilities')
#section('sidebar')
#parent
#endsection
#section('content')
<div class="container">
<div class="left">
<div class="form-group"{{ $errors->has('vulns') ? ' has-error' : '' }}>
<label for="vulnerability" class="col-sm-1 control-label">Vulnerability</label>
<select class="form-control" name="cve_id" id="cve_id" onchange="javascript:location.href = this.value;">
<option value="" selected>{{ $vuln }}</option>
#foreach ($vulns as $vn)
<option value="{{ $vn->cve }}">{{ $vn->cve }}</option>
#endforeach
</select>
#if ($errors->has('vulns'))
<span class="help-block">
<strong>{{ $errors->first('vulns') }}</strong>
</span>
#endif
</div>
Vulnerable Hosts:<br/>
#foreach ($vhost as $object)
{{ $object->host }}<br/>
#endforeach
</div>
<div class="right">
Description:<br/>
#foreach ($desc as $object)
<p>{{ $object->description }}</p>
<br/>
#endforeach
Solution:<br/>
#foreach ($sltn as $object)
<p>{{ $object->solution }}</p>
<br/>
#endforeach
</div>
</div>
#endsection
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Item;
class VulnerabilityController extends Controller {
/**
* Return View file
*
* #var array
*/
public function Vulnerability() {
return redirect('vulnerability/CVE');
}
public function listHosts(Request $request) {
$vuln = $request->vuln;
$vhost = DB::table('items')
->select('host')
->where('cve', 'like', $vuln)
->groupBY('host')
->get();
$vulns = DB::table('items')
->select('cve')
->groupBY('cve')
->get();
$desc = DB::table('items')
->select('description')
->where('cve', 'like', $vuln)
->groupBy('description')
->get();
$sltn = DB::table('items')
->select('solution')
->where('cve', 'like', $vuln)
->groupBy('solution')
->get();
return view('vulnerability', compact('vhost', 'vulns', 'vuln', 'desc', 'sltn'));
}
}
Routes:
Route::get('vulnerability', 'VulnerabilityController#vulnerability');
Route::get('vulnerability/{vuln}', 'VulnerabilityController#listHosts');

Related

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance

I have a Many to Many relationship between Wallet & User Models:
User.php:
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');
}
Wallet.php:
public function users()
{
return $this->belongsToMany(User::class,'user_wallet','wallet_id','user_id')->withPivot('balance');
}
And the pivot table user_wallet goes like this:
Now I need to get the name of wallets and the balance of that wallet in a table at Blade.
So at the Controller, I added this:
public function index($id)
{
// $id is the user id
$userWallet = Wallet::with("users")->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();
}
And then at the Blade:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users()->balance }}
</div>
#empty
Empty
#endforelse
But I get this error:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance
So what's going wrong here? How can I properly get the balance at the pivot table?
You have to loop user
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
#if(isset($wallet->users)&&count((array)$wallet->users))
#foreach($wallet->users as $user)
{{$user->pivot->balance }}
#endforeach
#endif
</div>
#empty
Empty
#endforelse
Your query should be
$userWallet = Wallet::with(["users"=>function ($query)use($id){
$query->where('user_id',$id);
}])->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();
That's because you use the relation's query builder object with users() but you need user property to get the collection:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->balance }}
</div>
#empty
Empty
#endforelse
But even the collection instance doesn't have a balance property as long as it is not a custom collection. You probably need to loop over the collection or use pluck to get the model's balance property:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->pluck('balance')->join(', ') }}
</div>
#empty
Empty
#endforelse

can`t access laravel Query Builder result from view

I can`t access the values from this variable $post from controller:
in blade view:
#foreach($post as $p)
<div class="col-md-3">
<div class="boxpromo">
<h4>{{ $p->title }}</h4>
<p>{{ $p->post_description }}</p>
<div class="price">
<div class="data">{{ $p->out_date }}</div>
<div class="cost">{{ $p->price }}</div>
</div>
</div>
</div>
#endforeach
In controller i have:
public function postSearchTransport(Request $request, Post $post){
...
// create a new query builder
$post = $post->newQuery();
// add some filters from the user in the search query
if($request->has('searchWord')){
$post->where('title', 'LIKE', '%'.$request->input('searchWord').'%');
}
...//more filters
$post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $post);
}
What i did wrong? I should be able to access in the view all the values from the query result, but i can`t this way.
Should i convert that $post std object from controller into an array then return it to the view???
You must pass to the view the returned value of paginate method like this :
$posts = $post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $posts);

model function call in blade view laravel

My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
Although its no good Practice Accessing the DB in Blade (better do this in the controller and pass the data) you can do:
<div class="price"><strong>Products</strong>
<span>
{{ BasicModel::where('sub_id', $row->sub_id)->count() }}
</span>
</div>
Its not tested, but have a look at the Eloquent docs, the count() method is explained there.
Update: I am not shure if laravel will find the class BasicModel (I never would access Models directly in blade, as stated do this in the controller and pass the data.) So maybe you need to write it with the full Namespace most likely {{ \App\BasicModel::where() }}.

Laravel search for topic by title and generate link

I'm laravel newbie. I have something like this:
Now I wan't to make when click first topic redirect to that topic url. For that I need to use DB:: function. I wan't search by given title, and then get id, etc.. For example i have posts:
table name same as topic title in image above, so I need use DB::select or ::table and search by that name, then print results. How to?
Layout:
#extends('layouts.main')
#section('content')
<div class="panel panel-default">
<div class="panel-heading">Forum</div>
<div class="panel-body">
#forelse($forums as $forum)
#forelse($topics as $topic)
{{ $topic->title }}<br>
#empty
<div class="alert alert-danger">Apgailėstaujame tačiau šis forumas yra tuščias!</div>
#endforelse
#empty
<div class="alert alert-danger">Apgailėstaujame tačiau forumas nerastas</div>
#endforelse
</div>
</div>
{!! $topics->render() !!}
#stop
Thanks in advance ;(
UPDATED
viewForum Controller:
<?php
namespace App\Http\Controllers;
use DB;
use View;
class viewForum extends Controller
{
public function showForum($fname, $fid)
{
$forums = DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->get();
$topics = DB::table('topics')
->where('forum_id', $fid)
->select()
->paginate(1);
return View::make('forum', compact('forums', 'topics'));
}
}
In your topic href do this:
{{ $topic->title }}<br>
In your routes.php add this
Route::get('topic/{fname}/{fid}', 'viewForum#showForum');

Laravel pagination Links not working on queries

I have this search engine with some advanced search options but I get errors when I use advance options.
When I go to the next pages I see errors.
Here is my code:
Controller:
$Input = Input::all();
$makethis = Input::flash();
$items = Gamefarm::where('roost_hen', '=', Input::get('sex'))
->paginate(6);
return View::make('gamefarms/index', compact('items', 'makethis'));
For my views
#foreach(array_chunk($items->all(), 3) as $row)
<div class="row">
#foreach ($row as $item)
<div class="col-md-4">
<h2>{{ $item->bname}}</h2>
<img src="{{$item->img_loc}}">
<div>{{$item->desc}}</div>
</div>
#endforeach
</div>
#endforeach
{{ $items->appends(Request::except('page'))->links() }}
return View::make('gamefarms/index',compact('items','makethis'))->with('items',$items);

Categories