ErrorException (E_NOTICE) Undefined offset: 0 laravel - database

I am trying to read from the database and display data into a form but I Keep getting this error.
This is my controller:
public function create()
{
/* this function gets data from the database (marks table) and render it to the view view */
$data['data']=DB::table('marks')->get();
if(count($data[0])>0){
return view('view',$data);
}
else{
return view('view');
}
}
And this is how I have defined the route:
Route::resource('claude', 'viewcontroller');

The variable $data doesn't have an index of 0.
But it has a key called data.
So you have to access it via the key.
It should be
if(count($data['data']) > 0){
return view('view',$data);
}

get() will return a collection, you can check if its has items by
if ($data['data']->count()) {
return view('view',$data);
} else {
return view('view');
}

if(is_array($data) && isset($data)){
return view('view',$data);
}

Related

how to use hasOne for two other detail models in laravel

I have one main table and two detail tables
table names: tb_main, tb_detail1, tb_detail2
tb_main
id, detail_code
tb_detail1
id, main_id
tb_detail2
id, main_id
I want get detail data using one function in main model
public function detail()
{
if(detail_code == 1)
return $this->hasOne(Detail1::class, 'main_id', 'id');
else
return $this->hasOne(Detail2::class, 'main_id', 'id');
}
Any ideas? please help me!
And sorry for my bad English.
You can use $this->detail_code
public function detail() {
if($this->detail_code == 1) {
return $this->hasOne(Detail1::class, 'main_id', 'id');
}else{
return $this->hasOne(Detail2::class, 'main_id', 'id');
}
Don't do that in the relation method. First, declare all relations in the main model like so:
public function detail1()
{
return $this->hasOne(Detail1::class, 'main_id');
}
public function detail2()
{
return $this->hasOne(Detail2::class, 'main_id');
}
Then write one method to decide what to do based on detail_code :
public function detail()
{
if($this->detail_code == 1)
return $this->detail1()->first();
return $this->detail2()->first();
}

How to unset a laravel session array

Hi I'm trying to remove a product array inside my cart session when the quantity is 1 and user tries to remove it, simply unsets that.
Here is my code:
public function remove($id)
{
if (session('cart')) {
foreach (session('cart') as $product) {
if ($product['id'] == $id) {
if ($product['qty'] == 1) {
} else {
$product['qty'] = $product['qty'] - 1;
}
};
}
Session::put('cart', session('cart'));
return redirect()->back();
}
}
I tried to use Session::forget('cart.'$num) but having some more issues.
Take your session edit it, and then re-set it:
$cart = session('cart');
array_forget($cart, $num);
session()->put('cart', $cart);

ES6 class getter function returns undefined in react after refresh

I am able to call the getter function. But upon refresh, the class is unable to find the getter function and returns undefined.
This is to save data into a class and then save the class into a state.
I have console logged the information and all the data appears present.
export class metaComparison {
constructor(__key, __operator=null, __compareValue=null) {
this._key = __key;
this._op = __operator;
this._compVal = __compareValue;
}
set key(__key) { this._key = __key; }
set operator(__op) { this._op = __op; }
set compareVal(__val) { this._compVal = __val; }
get key() { console.log("key is: ", this._key); return this._key;}
get operator() { return this._op; }
get compareVal() { return this._compVal;}
}
File 2:
import { metaComparison } from './meta-query-class'
.
.
.
this.setState(prevState => ({
metaQueryConditional: [...prevState.metaQueryConditional,
new metaComparison(this.state.selectedMetaKey)]...
.
.
this.state.metaQueryConditional.map((singleQuery, idx) => {
return(
.
.
<h2> Metakey: {singleQuery.key} {console.log("rendering single query which is", singleQuery, "and key is: ", singleQuery["_key"], singeQuery.key)}</h2>
Expected results is singleQuery.key returns the key value. The key value is returned. But upon refresh, I start getting undefined.

Angularjs Cannot read property of undefined on function returning Object

I have a function which I use to validate some fields in the frontend, this is being called using the ng-click directive. Inside the $scope.validateAgainstStudent() I am calling another function getStudentObj(value) which should return one of the 3 student objects.
Whatever I've tried so far is just not working. The error I get is TypeError: Cannot read property 'firstName' of undefined
$scope.student1 = {};
$scope.student2 = {};
$scope.student3 = {};
//This is being called from the frontend ng-click directive
$scope.validateAgainstStudent = function(value) {
console.log(getStudentObj(value).firstName);
if(getStudentObj(value).firstName==null || getStudentObj(value).lastName==null || getStudentObj(value).address==null){
alert('Please fill out all required fields');
} else{
//do something
}
}
};
//This should return the student object
function getStudentObj(value) {
if(value==1){
return $scope.student1;
}
if(value==2){
return $scope.student2;
}
if(value==3){
return $scope.student3;
}
};
I could however do something like this, without the help of the function getStudentObj(value) but I will endup with lots of duplicate code since the student object properties are the same. i.e firstName, lastName...etc
$scope.student1 = {};
$scope.student2 = {};
$scope.student3 = {};
//This is being called from the frontend ng-click directive
$scope.validateAgainstStudent = function(value) {
if(value==1){
if($scope.student1.firstName==null || $scope.student1.lastName==null || $scope.student1.title==null){
alert('Please fill out all required fields');
} else{
//do something
}
}
if(value==2){
if($scope.student2.firstName==null || $scope.student2.lastName==null || $scope.student2.title==null){
alert('Please fill out all required fields');
} else{
//do something
}
}
if(value==2){
if($scope.student3.firstName==null || $scope.student3.lastName==null || $scope.student3.title==null){
alert('Please fill out all required fields');
} else{
//do something
}
}
};
It looks like the value is matching any of the if statements. You could try adding a console.log to see if you can debug the issue.
//This should return the student object
function getStudentObj(value) {
if(value==1){
return $scope.student1;
}
if(value==2){
return $scope.student2;
}
if(value==3){
return $scope.student3;
}
console.log(value);
return {};
};
The value that you entered in getStudentObj(value) is different from 1, 2 and 3. For that reason getStudentObj return undefined. Make a console.log(value) in place of console.log(getStudentObj(value).firstName) to check value.

How to retrieve query parameter in Server Script

I'm using Windows Azure Mobile Service to build the backend for my app. For server script's read operation, now I want to retrieve the query parameter like $filter, $select in the script, etc. Any idea?
After hacking around with the 'query' object in the 'read' function's parameter (by using console.log ), I finally found the solution:
function isObject(variable) {
return variable !== null &&
variable !== undefined &&
typeof variable === 'object';
}
// Find all the member-value pairs from the expression object
function findMemberValuePairsFromExpression (expr, ret) {
if (!isObject(expr)) {
return null;
}
ret = ret || {};
for (var name in expr) {
if (expr.hasOwnProperty(name)) {
var prop = expr[name];
if (name === 'parent') { // Ignore parent property since it's added by us
continue;
}
else if (name === 'left') { // member expression are in the left subtree
if (isObject(prop)) {
prop.parent = expr; // Remember the parent
findMemberValuePairsFromExpression(prop, ret);
}
}
else if (name === 'member') {
// Found a member expression, find the value expression
// by the knowledge of the structure of the expression
var value = expr.parent.right.value;
ret[prop] = value;
}
}
}
if (expr.parent) {
// Remove the added parent property
delete expr.parent;
}
return ret;
}
// Get the filters component from query object and
// find the member-value pairs in it
function findMemberValuePairsFromQuery (query) {
var filters = query.getComponents().filters;
return findMemberValuePairsFromExpression(filters);
}
function read (query, user, request) {
request.execute();
}
Remember that this approach heavily relies on the inner structure of the query object so it may break in the future.
query.getComponents() also returns other parts of the query, like 'select', 'skip', 'top', etc. Basically anything of the oData protocol

Resources