ErrorException in Collection.php line 1043: Undefined offset: 0 in laravel - php

controller: HomeController.php
public function index()
{
// home page redirect query
$data['redirect'] = SiteSettings::where('name','default_home')->get();
$data['popular_rooms'] = Rooms::where('status','Listed')->get();
$data['property_rooms'] = PropertyRooms::select("property_room.*","rooms_photos.name", "rooms_price.night")
->join("rooms_photos","rooms_photos.room_id","=","property_room.room_id")
->join("rooms_price","rooms_price.room_id","=","property_room.room_id")
->where('rooms_photos.featured','Yes')
->get();
$data['city_count'] = HomeCities::all()->count();
$data['result'] = ThemeSettings::get();
$data['browser'] = '';
if(isset($_SERVER['HTTP_USER_AGENT']))
{
$agent = $_SERVER['HTTP_USER_AGENT'];
if(strlen(strstr($agent,"Chrome")) > 0 )
{
$data['browser'] = 'chrome';
}
}
$data['home_page_media'] = SiteSettings::where('name', 'home_page_header_media')->first()->value;
$data['home_page_sliders'] = Slider::whereStatus('Active')->orderBy('order', 'asc')->get();
$data['home_page_bottom_sliders'] = BottomSlider::whereStatus('Active')->orderBy('order', 'asc')->get();
$data['host_banners'] = HostBanners::all();
$data['home_city'] = HomeCities::all();
$data['languagess'] = Language::where('default_language', '1')->first()->value;
$data['bottom_sliders'] = BottomSlider::whereStatus('Active')->orderBy('order', 'asc')->get();
$data['our_community_banners'] = OurCommunityBanners::limit(3)->get();
//home page two data start
$data['reservation'] = Reservation::orderBy('id', 'desc')->where('status','Accepted')->groupBy('room_id')->limit(10)->get();
$data['view_count'] = Rooms::orderBy('views_count', 'desc')->where('status','Listed')->groupBy('id')->get();
$data['recommented'] = Rooms::orderBy('id', 'desc')->where('recommended','Yes')->where('status','Listed')->groupBy('id')->get();
$data['res_count'] = count($data['reservation']);
$data['room_view_count'] = count($data['view_count']);
$data['room_recommented_view'] = count($data['recommented']) ;
//redirect home page
if($data['redirect'][0]->value == 'home_two')
{
$data['default_home'] = 'two' ;return view('home.home_two',$data);
}
else
{
return view('home.home', $data);
}
}
route:
Route::group(['middleware' => ['install','locale']], function () {
Route::get('/', 'HomeController#index');
});
In this code I have to run my index file inside the controller but when I server php artisan it throw an offset error i.e. ErrorException in Collection.php line 1043: Undefined offset: 0. I don't know why where I am doing wrong? Please help me.
Thank You

You need to check that the index is present before accessing it.
isset($data['redirect'][0])
if(isset($data['redirect'][0])) {
if($data['redirect'][0]->value == 'home_two') {
$data['default_home'] = 'two';
return view('home.home_two',$data);
}
}

Related

Creating default object from empty value for information

I'm trying to edit and save some articles, but I receive this error : Creating default object from empty value. What's wrong in my code?Because, at edit I have my subject, but and submit I get this error.
My error : https://imgur.com/a/eWGqc5B
Controller
public function update($type, $id)
{
/* print_r(Input::all()); die; */
if($type == "News")
{
$article = \App\News::find($id);
$article->subject = Request::input('subject');
$article->public = Request::input('public');
$article->category_id = Request::input('category_id');
$article->information = Request::input('information');
$article->update();
}
if($type == "Event")
{
$article = \App\Event::find($id);
$article->subject = Request::input('subject');
$comm->comments = Request::input('comments');
$article->public = Request::input('public');
$article->category_id = Request::input('category_id');
$article->event_type_id = Request::input('event_type_id');
$article->country = Request::input('country');
$article->starts = Request::input('starts');
$article->ends = Request::input('ends');
$article->organizer = Request::input('organizer');
$article->address = Request::input('address');
$article->city = Request::input('city');
$article->website = Request::input('website');
$article->email = Request::input('email');
$article->telephone = Request::input('telephone');
$article->information = Request::input('information');
$article->update();
}
return redirect(URL::previous());
Line 669 in ArticleController.php
$article->subject = Request::input('subject');
Here is my editEvent code : https://codepen.io/anon/pen/YodwKO
my articles.blade code : https://codepen.io/anon/pen/jjXWOb
My route:
`
Route::post('admin/article/update/{type?}/{id?}', [ 'as' => 'update.article', 'uses' => 'ArticleController#update']);
`
The variable $comm is not defined. It looks like a typo.

Laravel Error: Only variable references should be returned by reference

I am working on a web app with php laravel framework. I'm also using framwork eloquent.
When i create a new "user" and i send it to my database the error with
$myValue->save();
This is the error:
"at HandleExceptions->handleError(8, 'Only variable references should be returned by reference',
'C:\\wamp64\\www\\project\\Fiq-spsslsj\\app\\Http\\Controllers\\DossierController.php', 97, array('request' => object(Request), 'employe' => object(employe)))
in DossierController.php line 97"
I've seen some similar problem but they are not quite really usefull (the problem in the other topics talk about code ingniter and common.php).
public function ajouter(Request $request){
$employe = new employe;
$employe->No_Employe = $request->input('No_Employe');
$employe->Nom = $request->input('Nom');
$employe->Prenom = $request->input('Prenom');
$employe->Email = $request->input('Email');
$employe->Adresse = $request->input('Adresse');
$employe->Date_Naissance = $request->input('Date_Naissance');
$employe->Titre_Emploi = $request->input('Titre_Emploi');
$employe->Telephone = $request->input('Telephone');
$employe->Annee_Embauche = $request->input('Annee_Embauche');
$employe->Present_Travail = $request->input('optTrav');
$employe->Sexe = $request->input('optSexe');
$employe->Fumeur = $request->input('optFum');
$employe->Langue = $request->input('Langue');
if (DB::table('liste_ville')->where('Nom_Ville','=',$request->input('Ville'))->get() != null)
{
$employe->Fk_Id_Ville = DB::table('liste_ville')->where('Nom_Ville','=',$request->input('Ville'))->value('Id_Ville');
}
else
{
$newVille = ucfirst($request->input('Ville'));
DB::table('liste_ville')->insertGetId(['Nom_Ville' => $newVille]);
$MoreVille = DB::table('liste_ville')->get();
foreach ($MoreVille as $key => $v)
{
if(strtolower($request->input('Ville')) === strtolower($v->Nom_Ville)){
$employe->Fk_Id_Ville = $v->Id_Ville;
break;
}
}
}
$employe->Code_Postal = $request->input('Code_Postal');
$employe->Actif = $request->input('optActif');
if (DB::table('provinces')->where('Nom_Province','=',$request->input('Province'))->get() != null) {
$employe->Fk_Id_Province =DB::table('provinces')->where('Nom_Province','=',$request->input('Province'))->value('Id_Province');
}
else
{
$newProv = ucfirst($request->input('Province'));
DB::table('provinces')->insertGetId(['Nom_Province' => $newProv]);
foreach ($provinces as $key => $p)
{
if(strtolower($request->input('Province')) === strtolower($p->Nom_Province)){
$employe->Fk_Id_Province = $p->Id_Province;
break;
}
}
}
$employe->save();
return redirect('/dossiers');
}
The code above is the code in my controller.
I hope you'll be able to help me.
Try this in your controller
return redirect()->to('/dossiers');

Laravel : Undefined variable in blade?

In am getting Undefined variable: score (View: C:\Users\Sarthak\blog\resources\views\submitquestion.blade.php) in blade view when executing :
Controller part
public function question(Request $request)
{
static $startscore = 0;
$getidvalue = Input::get('getid');
$getanswervalue = Input::get('getanswer');
$dbscore = DB::table('5question')->select('question_id', 'correct_answer', 'question_marks')->where('question_id', '=', $getidvalue)->get();
foreach($dbscore as $value) {
if ($getanswervalue == ($value->correct_answer)) {
$getscore = $startscore + $value->question_marks;
}
elseif ($getanswervalue == null) {
$emptyvalue = - 1;
$getscore = $startscore + $emptyvalue;
}
else {
$novalue = 0;
$getscore = $startscore + $novalue;
}
}
echo "$getscore";
Session::push('getscoresession', $getscore);
$getsession = ['qid' => $getidvalue, 'answer' => $getanswervalue];
Session::push('answer', $getsession);
// return response()->json(['qid'=>$getidvalue,'answer'=>$getanswervalue]);
$score = array_sum(Session::get("getscoresession"));
// return view('submitquestion',compact('score'));
return view('submitquestion', ['score' => $score]);
}
Blade part :
You have submitted quiz and your score is : {{ $score }}>
LOGOUT
try this one:
return view('submitquestion', compact('score'));
you can pass variable form controller to view using with and compact:
$request->session()->put('getscoresession', $getscore);
$getsession = ['qid' => $getidvalue, 'answer' => $getanswervalue];
$request->session()->put('answer', $getsession);
if ($request->session()->has('getscoresession')) {
$score = array_sum($request->session()->get("getscoresession"));
}else{
$score = 0;
}
using with :
$score= 10;
return view('submitquestion')->with('score',$score);
using compact :
return view('submitquestion',compact('score'));
Try it. If you get error from your controller send $score = 10;
return view('submitquestion')->with(['score' => $score]);

getting warning Invalid argument for foreach in codeigniter

In foreach loop i am getting warning Invalid argument supplied for foreach() below is my loop code and modal code please let me know where i done wrong
Controller
$concond = array("con_id"=>1,"con_status"=>1);
$this->data['contact']=$this->Frontend_model->get_contact($concond);
$this->load->view('frontend/clienthome',$this->data);
foreach Loop
if($contact)
{
foreach($contact as $foot)
{
$footadr1 = $foot->con_addr_line_1;
$footadr2 = $foot->con_addr_line_2;
$footcity = $foot->con_city;
$footstate = $foot->con_state;
$footcountry = $foot->con_country;
$footpin = $foot->con_pincode;
$footp1 = $foot->con_phone_1;
$footp2 = $foot->con_phone_2;
$footp3 = $foot->con_phone_3;
$footp4 = $foot->con_phone_4;
$footemail = $foot->con_email_id;
}
}
modal
function get_contact($concond)
{
$this->db->select('*');
$this->db->from('is_addres_contact');
$this->db->where($concond);
$query = $this->db->get();
return $query->result();
}
Seems like I got your problem!
You should try to do it like this:
$data['contact'] = $this->Frontend_model->get_contact($concond);
$this->load->view('frontend/clienthome', $data);
Try in model
function get_contact($con_id, $con_stat)
{
$this->db->where('con_id', $con_id);
$this->db->where('con_status', $con_stat);
$query = $this->db->get('is_addres_contact');
return $query->result();
}
Controller
$con_id = '1';
$con_stat = '1';
$this->data['contact'] = $this->frontend_model->get_contact($con_id, $con_stat);
$this->load->view('frontend/clienthome',$this->data);
Can you just try this,
In model,
function get_contact($concond)
{
$this->db->select('*');
$this->db->from('is_addres_contact');
$this->db->where($concond);
$query = $this->db->get();
return $query->result_array();
}
In View
if($contact)
{
foreach($contact as $foot)
{
$footadr1 = $foot['con_addr_line_1'];
$footadr2 = $foot['con_addr_line_2'];
$footcity =$foot['con_city'];
$footstate = $foot['con_state'];
$footcountry = $foot['con_country'];
$footpin = $foot['con_pincode'];
$footp1 = $foot['con_phone_1'];
$footp2 = $foot['con_phone_2'];
$footp3 = $foot['con_phone_3'];
$footp4 = $foot['con_phone_4'];
$footemail = $foot['con_email_id'];
}
}

pagination with CodeIgniter

Hi i'm trying to get the pagination right with code igniter but it seems that it doesn't want to work correctly. I get the second page, but there the pagination disappears, I still got the right table though and I have 2 errors:
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: models/evaluation_model.php
Line Number: 37
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/evaluation_model.php
Line Number: 37
The controller function:
function showEvaluations($offset = 0)
{
if($this->login->is_logged_in())
{
$limit = 5;
$result = $this->evaluation_model->getAllEvaluations($limit, $offset);
if ($this->session->userdata('type') == 'admin')
{
$data['evaluations'] = $result['evaluations'];
$data['total'] = $result['num_rows'];
$data['notallowed'] = false;
$config = array();
$config['base_url'] = base_url("evaluation/showEvaluations/");
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('allevaluations_view', $data);
}
else
{
$data['notallowed'] = true;
$this->load->view('allevaluations_view', $data);
}
//$this->load->view('allevaluations_view', $data);
}
else
{
$this->load->view('login_view');
}
}
and the Model:
function getAllEvaluations($limit, $offset)
{
$q = $this->db->select('tblPunten.PK_PuntID, tblPunten.Titel, tblPunten.Score, tblVakken.Vak, tblUsers.username, tblUsers.Voornaam, tblUsers.Achternaam')
->from('tblPunten')
->join('tblVakken', 'tblPunten.FK_VakID = tblVakken.PK_VakID')
->join('tblUsers', 'tblPunten.FK_UserID = tblUsers.PK_UserID')
->limit($limit, $offset);
$query['evaluations'] = $q->get()->result();
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('tblPunten')
->limit($limit, $offset);
$tmp = $q->get()->result();
$query['num_rows'] = $tmp[0]->count;
return $query;
}
Line 37: $query['num_rows'] = $tmp[0]->count;
Found it, the second query:
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('tblPunten')
->limit($limit, $offset);
must be without the limit:
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('tblPunten')
In codeigniter, ->result() an object.
if you want in that form of array then we can use ->result_array() so that we can get the result set in the form of array.
$tmp = $q->get()->result_array();//made changes
$query['num_rows'] = $tmp[0]['count'];//will work

Categories