Laravel 4 pulling data from database - laravel

I have table called 'players'. I want to pull every player from the database, or simply select players that have in column 'online' = 1. I want to display their name (column 'name') in 'players' table.
Here's what I've tried:
public function online()
{
$results = Player::with('online')->find(1)
return View::make('aac.test')->with('results', $results);
}
also tried:
public function online()
{
$results = DB::select('select * from players where level = 1');
return View::make('aac.test')->with('results', $results);
}
None of them works.

Try this:
public function online()
{
$results = Player::where('online', 1)->get('name');
return View::make('aac.test')->with('results', $results);
}
To display it using blade:
<ul>
#foreach($results as $result)
<li>
{{$result->name}}
</li>
#endforeach
</ul>

public function online()
{
$results = Player::where('level','=','1')->get();
return View::make('aac.test')->with('results', $results);
}
To display it using blade:
<ul>
#foreach($results as $result)
<li>
{{$result->name}}
</li>
#endforeach
</ul>

Related

Check SHA-256 Laravel

I have a system that requires a homeowner to submit a form about their guest's details. When they submit a form, each will be assigned a unique 6-digit code. This code will be hashed with SHA-256. In the admin's view, there is a list of all submitted forms. I want to do a search function where when I enter the code, the system will look through the hashed code and check if it exists or not.
This is my GuestController:
public function index()
{
//returns admin's view
$guest = Guest::all();
return view('pages.guest.index', compact('guest'));
}
public function store(Request $request)
{
$guest = new Guest;
$guest->code = random_int(100000, 999999);
$guest->hash = hash('sha256', $guest['code']);
$guest->owner_id = Auth::user()->id;
$guest->owner = Auth::user()->name;
$guest->unit = Auth::user()->unit;
$guest->guestname = $request->input('guestname');
$guest->guestphone = $request->input('guestphone');
$guest->guestic = $request->input('guestic');
$guest->guestcar = $request->input('guestcar');
$guest->numberofguests = $request->input('numberofguests');
$guest->datevisit = $request->input('datevisit');
$guest->timevisit = $request->input('timevisit');
$guest->save();
return redirect('show-pass')->with('status', 'Guest Added Successfully');
}
public function search(Request $request)
{
//Get the search value from the request
$search = $request->input('search');
//Search in the code from the list
$guest = Guest::query()
->where('code', 'LIKE', "%{$search}%")
->get();
//Return the search view with the results compacted
return view('pages.guest.search', compact('guest'));
}
This is my search result blade:
<div class="card-body">
#if($guest->isNotEmpty())
#foreach ($guest as $item)
<div class="post-list">
<p>Owner : {{ $item->owner }}</p>
<p>Unit : {{ $item->unit }}</p>
<p>Guest Name : {{ $item->guestname }}</p>
<p>Guest Phone Number : {{ $item->guestphone }}</p>
<p>Guest IC Number : {{ $item->guestic }}</p>
<p>Guest Car Number : {{ $item->guestcar }}</p>
<p>Date : {{ $item->datevisit }}</p>
<p>Time : {{ $item->timevisit }}</p>
</div>
#endforeach
#else
<div>
<h4>No guests with the code was found</h4>
</div>
#endif
</div>
Is what I'm trying to do possible? If yes, how can I edit my search method to be able to do so? May I get some help?

How to display an image of each category. Google Drive

I store category images in Google Drive. The category in the database stores the name of the image, the same as the name of the image in Google Drive. By this name, I get the url of the image and display it in the category column. However, I cannot think of how to make sure that each category/subcategory has its own picture. Now all I have achieved is that the categories and subcategories have different pictures, but all categories have the same picture, and so do the subcategories.
public function compose(View $view)
{
$catalog = Category::with('children')->where('parent_id', '=', NULL)->get();
//
foreach ($catalog as $cat) {
if(isset($cat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($cat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($cat->img, PATHINFO_EXTENSION))
->first();
};
$catimg = collect(isset($cat->img)?(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL):NULL);
foreach ($cat->children as $subcat) {
if(isset($subcat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($subcat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($subcat->img, PATHINFO_EXTENSION))
->first();
};
$subcatimg = collect(isset($subcat->img)?(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL):NULL);
};
};
return $view->with(['catalog' => $catalog, 'catimg' => $catimg, 'subcatimg' => $subcatimg]);
}
View:
<ul>
#foreach( $catalog as $item )
<li class='has-sub'><a href="#"><img class="catalogimg" src="#if($item->img != NULL && $catimg != NULL){{$catimg}}#else /img/categories/kitchen-utensils.png
#endif"><span class="cat-text">{{ $item->name }}</span></a>
<ul>
#foreach( $item->children as $subitem )
<li><span class="cat-text">{{ $subitem->name }}</span></li>
#endforeach
</ul>
</li>
#endforeach
</ul>
Category Model:
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
Ah I understand your problem now. You can simply create a new field in your array/collections, I call it img_url. Save the respective URL into this field and access it later.
public function compose(View $view)
{
foreach ($catalog as $cat) {
$catimg = null; //define it here as null
if(isset($cat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($cat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($cat->img, PATHINFO_EXTENSION))
->first();
$catimg = collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$cat['img_url'] = $catimg; // create a new field called img_url and assign value
foreach ($cat->children as $subcat) {
$subcatimg = null;
if(isset($subcat->img)){
$contents = collect(Storage::disk('google')->listContents('askldjfDSKLsOe2sdlKJF/', false));
$file = $contents
->where('type', '=', 'file')
->where('filename', '=', pathinfo($subcat->img, PATHINFO_FILENAME))
->where('extension', '=', pathinfo($subcat->img, PATHINFO_EXTENSION))
->first();
$subcatimg = collect(isset($file['path'])?(Storage::disk('google')->exists($file['path'])?Storage::disk('google')->url($file['path']):NULL):NULL);
};
$subcat['img_url'] = $subcatimg;
};
};
return $view->with(['catalog' => $catalog]);
}
View:
<ul>
#foreach( $catalog as $item )
<li class='has-sub'><a href="#"><img class="catalogimg" src="#if(!is_null($item->img_url)){{$item->img_url}}#else /img/categories/kitchen-utensils.png
#endif"><span class="cat-text">{{ $item->name }}</span></a>
<ul>
#foreach( $item->children as $subitem )
<li><span class="cat-text">{{ $subitem->name }}</span></li>
#endforeach
</ul>
</li>
#endforeach
</ul>
In your view access the image urls like: $item->img_url or $subitem->img_url, it is possible that this value is null, but that shouln't be a problem per se.

Trying to get a variable pagination to show me 3 different types of paginations

Here is my Index:
<div class="page-sizer">
<a class="page numbers" href="{{$references->pagination(10)}}">10</a>
<a class="page numbers" href="{{$references->pagination(30)}}">30</a>
<a class="page numbers" href="{{$references->pagination(100)}}">100</a>
<button href="qweqwe.qweqwe" class="btn btn-info float-right>112e1e1e1e"></button>
</div>
My AdminreferenceController:
public function index()
{
$references = Reference::orderBy('priority', 'desc')->orderBy('id', 'desc')->paginate(50);
return view('admin.reference.index', ['references' => $references]);
}
and my Lengthawarepaginator:
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
{
foreach ($options as $key => $value) {
$this->{$key} = $value;
}
$this->total = $total;
$this->perPage = $perPage;
$this->lastPage = max((int) ceil($total / $perPage), 1);
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
$this->items = $items instanceof Collection ? $items : Collection::make($items);
}
. i currently get the error Method Illuminate\Database\Eloquent\Collection::pagination does not exist.
I want to have 3 buttons that show 3 different kinds of paginations like in stackoverflow in the search bar at the bottom.
You are calling pagination in collection try like this
first you need to include the DB class before class deceleration
use Illuminate\Support\Facades\DB;
Then change the query like this
$references = DB::table('reference')->orderBy('priority', 'desc')->orderBy('id', 'desc')->paginate(5o);
return view('admin.reference.index', ['references' => $references]);
In you view you need to access like this
{{ $references->links() }}
https://laravel.com/docs/5.7/pagination
i have currently changed the pagination completly. i wrote a url with
/x/y/perpage/10,
/x/y/perpage/30,
/x/y/perpage/100,
i made a new route for it
route::('/x/perpage/{count})'
and changed my AdminReferenceController to
public function index($perpage = false)
{
if(!$perpage) {
$perpage = 10;
}
$references = Reference::orderBy('priority', 'desc')->orderBy('id', 'desc')->paginate($perpage);
I couldn't get time to work this out. But I found you calling pagination() instead of paginate. See my suggetion
<div class="page-sizer">
<a class="page numbers" href="{{$references->paginate(10)}}">10</a>
<a class="page numbers" href="{{$references->paginate(30)}}">30</a>
<a class="page numbers" href="{{$references->paginate(100)}}">100</a>
<button href="qweqwe.qweqwe" class="btn btn-info float-right>112e1e1e1e"></button>
</div>
Here I removed the paginate from index() and returns collection.
public function index()
{
$references = Reference::orderBy('priority', 'desc')->orderBy('id', 'desc');
return view('admin.reference.index', ['references' => $references]);
}
Hope this helps you.

Laratrust and users

I do have an app in Laravel 5.5 using Laratrust last version to give roles and permissions.
Now, I want to let and Administrator lists only the users he has created and let the Superadministrator see ALL users.
Let's see some code:
UserController.php
public function index()
{
$id = Auth::user()->id;
if ($id = Laratrust::hasRole('superadministrator')) {
$users = User::orderBy('id', 'desc')->paginate(10);
}
$users = User::where('id', '=', $id)->orderBy('id', 'desc')->paginate(10);
return view('manage.users.index')->withUsers($users);
}
Manage navbar:
#if(Laratrust::hasRole('superadministrator|administrator'))
<li>Usuários</li>
<li>
<a class="has-submenu {{Nav::hasSegment(['roles', 'permissions'], 2)}}">Cargos & Permissões</a>
<ul class="submenu">
<li>Cargos</li>
<li>Permissões</li>
</ul>
</li>
#endif
Any ideas, please?
You are overwriting the $users variable in controller

Display Categories and subcategories using CodeIgniter

I have two table in my database, One is Categories and the other is Sub_Categories, I want to display them like this:
Categorie 1
sub categoie 1
sub categoie 2
sub categoie 3
sub categoie 4
Categorie 2
sub categoie 1
sub categoie 2
sub categoie 3
sub categoie 4
But i don't know how to do this.
In my Database table i have this fields :
Categories: ID, Name, Icon.
Sub_Categories: ID, Categ_id, Name
This should work;
public function get_categories()
{
$query = $this->db->get('Categories');
$return = array();
foreach ($query->result() as $category)
{
$return[$category->id] = $category;
$return[$category->id]->subs = $this->get_sub_categories($category->id); // Get the categories sub categories
}
return $return;
}
public function get_sub_categories($category_id)
{
$this->db->where('Category', $category_id);
$query = $this->db->get('Sub_Categories');
return $query->result();
}
All this does is get's all the categories, but then gets all the subcategories for each of the categories. Calling the get_categories() function should return an object in the format you want.
I hope this helps.
Edit
You would call the get_categories function from your controller and pass it to the view;
$data['categories'] = $this->your_model->get_categories();
$this->load->view('view_file', $data);
Then within your view you would display them like this;
<ul>
<?php
foreach ($categories as $category)
{
?>
<li><?php echo $category->name; ?>
<?php
if(!empty($category->subs)) {
echo '<ul>';
foreach ($category->subs as $sub) {
echo '<li>' . $sub->name . '</li>';
}
echo '</ul>';
}
?>
</li>
<?php
}
?>
</ul>
Database
image for database table
Try this
in Model
public function getCategories()
{
$query = $this->db->query('select * from categories where cat_parent=0');
return $query->result_array();
}
public function getCategoriesSub($parent)
{
$query = $this->db->query("select * from categories where cat_parent='$parent'");
return $query->result_array();
}
in Controller
public function categories()
{
$data['mcats'] = $this->admin_model->getCategories();
foreach($data['mcats'] as $key =>$val){
$subcats = $this->admin_model->getCategoriesSub($val['cid']);
if($subcats){
$data['scats'][$val['cid']] = $subcats;
}
}
$this->load->view('admin/header');
$this->load->view('admin/category_list', $data);
$this->load->view('admin/footer');
}
In view
<ul>
<?php
foreach ($mcats as $key =>$val)
{
?>
<li><?php echo $val['cat_name']; ?>
<ul>
<?php
foreach ($scats[$val['cid']] as $sub) {
echo '<li>' . $sub['cat_name'] . '</li>';
}
?>
</ul>
</li>
<?php
}
?>
</ul>
its already working code - i think it is usefull

Resources