What's the cause Undefined property: stdClass? - php

I don't know what this error cause. I didn't change anything in my codes. But when I go to my search view. It always returns me the undefined property error. This error cause when I tried to foreach all my columns in my table data. I already solve this error not once. Because it cannot find the $id of selected options. But this time I can't fix it.
Error:
Undefined property: stdClass::$id (View: C:\Users\JohnFrancis\LaravelFrancis\resources\views\document\show.blade.php)
View
show.blade.php - This view will list all the values in my tables.
#section ('content')
<div class = "col-md-12">
<table class = "table">
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Category</th>
<th>Sender</th>
<th>Date Received</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach ($documentLists as $list)
<tr class = "info">
<td>{{ $list->title }}</td>
<td>{{ strip_tags(substr($list->content, 0, 50)) }} {{ strlen($list->content) > 50 ? "..." : '' }}</td>
<td>{{ $list->category_type }}</td>
<td>{{ $list->username }}</td>
<td>{{ date('M j, Y', strtotime($list->dateReceived)) }}</td>
<td>
<button type = "submit" class = "btn btn-info">Read</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
read.blade.php - This where it will redirect to the current view that selected.
<!--DOCUMENT CONTROLLER-->
<div class = "col-md-6">
<form class = "vertical">
<div class = "form-group">
<textarea id = "content">{{ $documentLists->content }}</textarea>
</div>
<div class = "form-group">
<button type = "submit" class = "btn btn-success">Approve</button>
</div>
</form>
</div>
Controller
//SHOW
public function showDocuments()
{
$documentLists = DB::table('document_user')->select('documents.title', 'documents.content', 'categories.category_type', 'users.username', 'document_user.dateReceived')
//Table name //PK //FK
->join('users', 'users.id', '=', 'document_user.sender_id')
->join('documents', 'documents.id', '=', 'document_user.document_id')
->join('categories', 'categories.id', '=', 'documents.category_id')
->where('sender_id', '!=', Auth::id())
->where('user_id', '!=', Auth::id())->get();
//VIEW
return view ('document.show')->with('documentLists', $documentLists);
}
//READ
public function readDocuments($id)
{
//Find the document in the database and save as var.
$documentLists = Document::find($id);
return view ('document.read')->with('documentLists', $documentLists);
}
routes
Route::get('/show',
[
'uses' => '\App\Http\Controllers\DocumentController#showDocuments',
'as' => 'document.show',
'middleware' => 'auth',
]);
Route::get('/receive/documents/{id}',
[
'uses' => '\App\Http\Controllers\DocumentController#readDocuments',
'as' => 'document.read',
'middleware' => 'auth',
]);

In below you are not selecting id
$documentLists = DB::table('document_user')->select('documents.title', 'documents.
but calling in your blade {{ route ('document.read', $list->id) }}

$documentLists = DB::table('document_user')->select('documents.id','documents.title', 'documents.content', 'categories.category_type', 'users.username', 'document_user.dateReceived');
You need to select the column documents.id.But you have missed it

Related

Fetching and Printing data from Database in Laravel 8

Hi I just started working in laravel 8 and I would like to know how to fetch data in database and print data
I have a template of a table with a search bar to see and find user in our database
Here is our controller for the table
DashboardController
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function dashboard()
{
$dashboardTitle = "Dashboard";
$isCurrent = "dashboard";
return view('dashboard.index', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
public function profile()
{
$dashboardTitle = "Profile";
$isCurrent = "Profile";
return view('dashboard.profile', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
public function directory()
{
$dashboardTitle = "Directory";
$isCurrent = "Directory";
return view('dashboard.directory', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
public function journal()
{
$dashboardTitle = "Journal";
$isCurrent = "Journal";
return view('dashboard.journal', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
public function files()
{
$dashboardTitle = "Files";
$isCurrent = "Files";
return view('dashboard.files', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent
]
);
}
}
Here is the view
directory
#extends('dashboard.layouts.dashboard-layout')
#push('css')
<!-- Custom styles for this page -->
<link href="{{asset('dashboards/vendor/datatables/dataTables.bootstrap4.min.css')}}" rel="stylesheet">
#endpush
#section('content')
<!-- Page Heading -->
<h1 class="h3 mb-2 text-gray-800">Tables</h1>
<p class="mb-4">DataTables is a third party plugin that is used to generate the demo table below.
For more information about DataTables, please visit the <a target="_blank"
href="https://datatables.net">official DataTables documentation</a>.</p>
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">DataTables Example</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<td></td>
<td></td>
<td></td>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
</div>
<a class="scroll-to-top rounded" href="#page-top">
<i class="fas fa-angle-up"></i>
</a>
#endsection
#push('script')
<!-- Page level plugins -->
<script src="{{asset('dashboards/vendor/datatables/jquery.dataTables.min.js')}}"></script>
<script src="{{asset('dashboards/vendor/datatables/dataTables.bootstrap4.min.js')}}"></script>
<!-- Page level custom scripts -->
<script src="{{asset('dashboards/js/demo/datatables-demo.js')}}"></script>
#endpush
Our table name is users and I want to print it in the table using a loop
Database
Add new rows in database if you haven't (position, office ...)
Controler
...
$users = User::all();
return view('dashboard.directory', [
'users' => $users
]
...
Blade
#foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->postion }}</td>
<td>{{ $user->office }}</td>
<td>{{ $user->age }}</td>
<td>{{ $user->start_date }}</td>
<td>{{ $user->salary }}</td>
</tr>
#endforeach
To retrieve all users in your db, you have to :
Dashboard Controller
public function directory(){
$dashboardTitle = "Directory";
$isCurrent = "Directory";
$users = User::all();
return view('dashboard.directory', [
'dashboardTitle' => $dashboardTitle,
'isCurrent' => $isCurrent,
'users' => $users
]);
}
In Blade
#foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->postion }}</td>
<td>{{ $user->office }}</td>
<td>{{ $user->age }}</td>
<td>{{ $user->start_date }}</td>
<td>{{ $user->salary }}</td>
</tr>
#endforeach

Why do I get a "Trying to get property of non-object" error when using Sortable package in Laravel?

I've encountered a problem with the Kyslik\Column-sortable package for Laravel which has frustrated me for weeks now. :)
The sorting links displays correctly, but when I click on one of the sorting links, I get the following error:
🧨 Trying to get property 'title' of non-object (View: C:\wamp64\www\GeekCollector\resources\views\Lp\show.blade.php)
What's even more strange is that the route goes to the BrowseResults view, but the message sais that the error was encountered in the Show view.
If I remove all the sortable related stuff from the BrowseResults view, the Show view works perfectly.
Here is my model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;
use Kyslik\ColumnSortable\Sortable;
class Lp extends Model
{
use HasFactory;
use Sortable;
public $sortable = ['title', 'artist', 'year'];
}
Here is my BrowseResults controller method:
public function browseResults(Request $request)
{
$chosenCategory = $request->category;
$lps = Lp::where('category','like','%'.$chosenCategory.'%');
$result = $lps->sortable('title')->paginate(5);
return view('Lp.browseResults', compact('result'));
}
And here is my blade view:
<x-app-layout>
<x-slot name="header">
<h1 class="font-semibold text-xl text-gray-800 leading-tight">
Lp-skivor
</h1>
</x-slot>
#sortablelink('title')
#sortablelink('artist')
#sortablelink('year')
<table>
<tr>
<td>Titel</td>
<td>Artist</td>
<td>Kategori</td>
<td>Plats</td>
<td>Position</td>
</tr>
#foreach ($result as $lp)
<tr>
<td>
{{ $lp->title }}
</td>
<td>{{ $lp->artist }}</td>
<td> {{ $lp->category }}</td>
<td>{{ $lp->place }}</td>
<td>{{ $lp->position }}</td>
<td>
Uppdatera
</td>
<td>
<form action="{{ route('lp.destroy', $lp->id) }}" method="post">
#csrf
#method('DELETE')
<button type="submit">Ta bort</button>
</form>
</td>
</tr>
#endforeach
</table>
{!! $result->appends(\Request::except('page'))->render() !!}
</x-app-layout>
The route looks like this:
Route::post('/lp/browse-results', [LpController::class, 'browseResults'])->name('lp.browseResults');
Thanks very much in advance for your help!
All the best!

Illegal operator and value combination when paginating in Laravel

I am a newbie at Laravel 6. In the view, I created a form, and when it's submitted, it should return a table filtered with the links of pagination at the bottom. All works correctly, but when I click a link of the pagination appears the following error: "Illegal operator and value combination."
I tried even using the "render" method in the view, but nothing changed. I see the links, but when I click one of them, the error appears.
View
#if (isset($meetings))
<div class="container">
<table class="table table-condensed table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Participants</th>
<th>Description</th>
<th>Room</th>
<th>Date</th>
<th>Start Hour</th>
<th>End Hour</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach($meetings as $meeting)
<tr>
<td>{{ $meeting->id }}</td>
<td>{{ $meeting->id_participants }}</td>
<td>{{ $meeting->description }}</td>
<td>{{ $meeting->id_room }}</td>
<td>{{ $meeting->date }}</td>
<td>{{ $meeting->start_hour }}</td>
<td>{{ $meeting->end_hour }}</td>
<td><button type="button" class="btn btn-primary">Edit</button></td>
<td>
<form action="{{ route('nextMeetingsDeleteMeeting', ['id' => $meeting->id]) }}" method="POST">
#csrf
{{ method_field('PUT') }}
<button type="submit" class="btn btn-danger" id={{ $meeting->id }}>Delete</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
<div>
{{ $meetings->links() }}
</div>
</div>
#endif
Controller
public function fetch_table(Request $request)
{
$this->validate($request, [
'start_date' => 'date',
'end_date' => 'date',
]);
$start_date = $request['start_date'];
$end_date = $request['end_date'];
$participants = $request['participants'];
$room_input = $request['rooms'];
$rooms = Room::all();
$users = User::all()->where('is_active', '1')->sortBy('surname');
$meetings = $this->build_table($room_input, $start_date, $end_date, $participants);
return view('reportArea', compact('users', 'rooms', 'meetings', 'start_date', 'end_date', 'participants',
'room_input'))->withInput($request->all());
}
public function build_table($room, $start_date, $end_date, $participants)
{
if (!empty($room) && !empty($participants)) {
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('id_room', $room)
->where('date', '>', $start_date)
->where('date', '<', $end_date)
->where(function ($query) use ($participants) {
$query->where('id_participants', $participants)
->orWhere('id_participants', 'like', '%;'.$participants)
->orWhere('id_participants', 'like', $participants.';%')
->orWhere('id_participants', 'like', '%;'.$participants.';%');
})
->paginate(2);
} elseif (!empty($participants)) {
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('date', '>', $start_date)
->where('date', '<', $end_date)
->where(function ($query) use ($participants) {
$query->where('id_participants', $participants)
->orWhere('id_participants', 'like', '%;'.$participants)
->orWhere('id_participants', 'like', $participants.';%')
->orWhere('id_participants', 'like', '%;'.$participants.';%');
})
->paginate(2);
} elseif (!empty($rooms)) {
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('date', '>', $start_date)
->where('date', '<', $end_date)
->where('id_room', $room)
->paginate(2);
} else {
$meetings = DB::table('meetings')
->where('is_active', '1')
->where('date', '>', $start_date)
->where('date', '<', $end_date)
->paginate(2);
}
return $meetings;
}
Route
Route::get('/reportarea/fetchtable', 'ReportAreaController#fetch_table')->name('reportAreaFetchTable');
Currently, all works OK, but when I click a link, the mentioned error appears. In other words, if I add the method paginate(2) I see correctly only two rows on the table, but when I click the link to see the other ones, it doesn't work correctly. Is anyone able to help me solve this problem?
You will probably need to be appending to the query string for the links for the pager to pass your other parameters you need. You are passing null as a value for those query parameters and getting the error.
As an example:
{{ $meetings->appends(['start_date' => ..., 'end_date' => ..., ...])->links() }}
Or just passing all the current query parameters (the paginator will ignore what ever key is used for the current page):
{{ $meetings->appends(request()->query())->links() }}
Laravel 6.x Docs - Pagination - Displaying Results - Appending To Pagination Links
Laravel 6.x Docs - Requests - Retrieving Input - Retrieving Input From The Query String

Laravel 5.4 change user role

I trying make changing role in my panel admin. But when i made it my form dont sending post request. Now only working showing user role. I cant fix it becouse when i click button to switch i dont get any error and role is not changing.
I use HttpRequester when i use url /admin/change its showing this error:
MethodNotAllowedHttpException in RouteCollection.php line 233
There is my code:
View:
#extends('layouts.app')
#section('content')
<h2>Panel Admina</h2>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>ImiÄ™</th>
<th>Nazwisko </th>
<th>E-mail </th>
<th>Nr.tele </th>
<th>Użytkownik </th>
<th>Moderator </th>
<th>Admin </th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<form action="{{route('change')}}" method="post">
{{ csrf_field() }}
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->lastname }}</td>
<td>{{ $user->email }}<input type="hidden" name="email" value="{{ $user->email }}"></td>
<td>{{ $user->phonenumber }}</td>
<td><input type="checkbox" {{ $user->hasRole('User') ? 'checked' : '' }} name="role_user"/></td>
<td><input type="checkbox" {{ $user->hasRole('Moderator') ? 'checked' : '' }} name="role_moderator"/></td>
<td><input type="checkbox" {{ $user->hasRole('Admin') ? 'checked' : '' }} name="role_admin"/></td>
<td><input type="submit" class="btn btn-default btn-sm" value="Przydziel rolÄ™"></td>
</form>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
Controller:
public function change(Request $request)
{
$user = User::where('email', $request['email'])->first();
$user->roles()->detach();
if ($request['role_user']) {
$user->roles()->attach(Role::where('name', 'User')->first());
}
if ($request['role_moderator']) {
$user->roles()->attach(Role::where('name', 'Moderator')->first());
}
if ($request['role_admin']) {
$user->roles()->attach(Role::where('name', 'Admin')->first());
}
return redirect()->back();
}
Routing:
Route::get('/admin', [
'uses' => 'AdminController#index',
'middleware' => 'roles',
'roles' => ['Admin']
]);
Route::post('/admin/change', [
'uses' => 'AdminController#change',
'as' => 'change',
'middleware' => 'roles',
'roles' => ['Admin']
]);
I really dont know how i can resolve my problem.
try to pass the user id to your route and your form action and instead of post make it put for example:
<form action="{{route('change', $user->id)}}" method="post">
and in your route:
Route::put('/admin/change/{id}', [
'uses' => 'AdminController#change',
'as' => 'change',
'middleware' => 'roles',
'roles' => ['Admin']
]);
and in your controller:
public function change(Request $request, $id)

How to populate database values in table

I'm trying to fetch my records in my users table and populate them in table if the input is correct using Raw SQL Queries. Trying this convert from SQL to Laravel Queries:
SELECT * FROMusersWHERE username = 'shadow'
Unfortunately it gives me a error.
Undefined variable: users (View: C:\Users\JohnFrancis\LaravelFrancis\resources\views\account\search.blade.php)
Controller:
User is my Model which has a protected $table = 'users which is my database.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Requests;
use DB;
class AccountController extends Controller
{
public function getEmployee()
{
return view ('account.search');
}
public function postEmployee(Request $request)
{
$this->validate($request, [
'username' => 'required|alpha|max:20',
]);
$username = $request['username']; //Input will be passed to $request array. Accessing search field.
$user = new User();
$user = DB::select('select * from users where username = ?', [1]);
return view ('account.search', ['user' => 'myuser']);
}
}
View:
#section ('content')
<div class = "col-md-10">
<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('account.search') }}">
<div class = "form-group {{ $errors->has('username') ? ' has-error' : '' }}">
<label for = "username" class = "control-label">Search</label>
<input type = "text" name = "username" class = "form-control" placeholder = "Find people">
#if ($errors->has('username'))
<span class = "help-block">{{ $errors->first('username') }}</span>
#endif
</div>
<div class = "form-group">
<button type = "submit" class = "btn btn-default">Search</button>
</div>
<input type = "hidden" name = "_token" value = "{{ Session::token() }}">
</form>
<table class = "table">
<br>
<thead>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Middlename</th>
<th>Email</th>
<th>Username</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach ($user as $myuser)
<tr class = "success">
<td>{{ $myuser->id }}</td>
<td>{{ $myuser->first_name }}</td>
<td>{{ $myuser->last_name }}</td>
<td>{{ $myuser->middle_name }}</td>
<td>{{ $myuser->email }}</td>
<td>{{ $myuser->username }}</td>
<td></td>
<td>
<button type = "submit" class = "btn btn-warning">Update</button>
<button type = "submit" class = "btn btn-danger">Archive</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
Routes:
Route::get('/search',
[
'uses' => '\App\Http\Controllers\AccountController#getEmployee',
'as' => 'account.search',
]);
Route::post('/search',
[
'uses' => '\App\Http\Controllers\AccountController#postEmployee',
]);
This should do what you want:
$user = User::where('username', $username)->get();
if ($user->isEmpty()) {
dd('Failed');
else {
dd('Success');
}
If you are using a model, there is no need for DB::select. You would find a User by their username like so:
$user = User::where('username', '=', (the username))->get();

Categories