What to Show some data On my view but there is a Error In Laravel - laravel

Here I am trying to add, Add Comment feature and it's causing a problem to show data.
public function store(Request $request)
{
$chatterreply = new Chatterreply;
$chatterreply->reply = $request->body;
$chatterreply->chatter_post_id = $request->chatter_post_id;
$chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
$chatterreply->save();
$chatterreplies = Chatterreply::where('chatter_post_id',$request->chatter_post_id)->get();
$chatter_editor = config('chatter.editor');
if ($chatter_editor == 'simplemde') {
// Dynamically register markdown service provider
\App::register('GrahamCampbell\Markdown\MarkdownServiceProvider');
}
echo "<pre>"; print_r($chatterreplies); die;
return view('chatter::discussion', compact('chatterreplies','chatter_editor'))->with('chatter_alert','Add Comment Successfully');
}
And Here Is Where I am passing the variable
$chatter = session()->pull('chatter');
return view('chatter::discussion', compact('discussion', 'posts', 'chatter_editor'))->with('chatterreplies',$chatter);

Related

How to stay on previous page after signin in codeigniter framework

Hii guys i'm new in stackoverflow , if this question is very long please pardon me, This is a Consumer complain website here is a view page called separate_user_admin_reply2.php in this page i want to apply a functionality, If a user without login click on the replay button then Signin.php view page will appear then after login the user come back to the previous page, the previous page link is : http://localhost/A2Zcomplainboard/index.php/Complain/Separate_single_complain_user/COMP008
So what i can do ..please help me (thanks in advance)
The controller is :
public function Separate_single_complain_user($cid)
{
//$cid = $this->input->get('compid');
$this->load->model('Complain_model');
$res = $this->Complain_model->getSingleComplain($cid);
$this->load->model('Replay_Model');
$res1 = $this->Replay_Model->Separate_single_complain_replay($cid);
if($res){
$this->load->view('separate_user_admin_reply2',['res'=>$res,'res1'=>$res1]);
}
}
The model is:
public function getSingleComplain($cid)
{
$complain = $this->db->query("SELECT comp_id,comp_title,comp_description,post_datetime,status,publish_status,comp_catagory,posted_by,id,first_name,email FROM log_tbl l INNER JOIN comp_tbl c ON l.id = c.posted_by WHERE comp_id = '$cid'");
$res = $complain->result_array();
if($res)
return $res;
else
return false;
}
public function Separate_single_complain_replay($cid){
$rep = $this->db->query("SELECT first_name,rep_id,rep_description,rep_datetime,rep_by,rep_against,rep_flag,rating FROM log_tbl l INNER JOIN reply_tbl r ON l.id = r.rep_by WHERE r.rep_against = '$cid 'AND rep_flag = '1'");
$res = $rep->result_array();
if($res)
return $res;
else
return false;
}
enter image description here
This is the view page image
separate_user_admin_reply2.php
And the signin controller is :
public function index()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->load->model('Login_Model');
$data = $this->Login_Model->isvalidate($email,$password);
if($data)
{
$this->load->library('session');
$this->session->set_userdata('email',$data['email']);
$this->session->set_userdata('type',$data['type']);
$this->session->set_userdata('id',$data['id']);
if($data['type']=='User' ){
return redirect('User/index');
}
else
{
return redirect('Admin/adminDashboard');
}
}else{
echo "<script>alert('Details not matched')</script>";
header("refresh:1");
return redirect('Complain/login');
}
}
model:-
public function isvalidate($email,$password)
{
$q = $this->db->where(['email'=>$email,'password'=>$password])
->get('log_tbl');
if($q->num_rows()){
$x = $q->row()->email;
$y = $q->row()->type;
$z = $q->row()->id;
$userData = array('email' => $x, 'type' => $y, 'id' => $z);
return $userData;
}else{
return false;
}
}
signin view page image:
Store the previous page url in session data. You can find a detailed description of how to do that in the codeigniter documentation. When the user successfully logged in, check if the previous page data is set, if so flush it and redirect to it.

Laravel returns old model after update

When i store a new record, Laravel returns the new record. Everything works fine.
When i update a record, Laravel returns the old record. I like to return the updated record.
Controller
public function store(StoreProjectRequest $request)
{
$data = $this->repo->create( $request->all());
return response()->json(new ProjectResource($data));
}
public function update(UpdateProjectRequest $request, Project $project)
{
$data = $this->repo->update($project, $request->all());
return response()->json(new ProjectResource($data));
}
Repository
public function create( $data)
{
$this->model->name = $data['name'];
$this->model->description = $data['description'];
$this->model->sales_order_id = $data['sales_order_id'] ? $data['sales_order_id'] : NULL;
$this->model->project_leader_id = $data['project_leader_id'];
$this->model->project_type_id = $data['project_type_id'];
$this->model->project_status_id = $data['project_status_id'];
$this->model->creator_id = Auth()->id();
$this->model->save();
return $this->model;
}
public function update($model, $data)
{
$model->name = $data['name'];
$model->description = $data['description'];
$model->sales_order_id = $data['sales_order_id'];
$model->project_leader_id = $data['project_leader_id'];
$model->project_type_id = $data['project_type_id'];
$model->project_status_id = $data['project_status_id'];
$model->save();
return $model;
}
When i add $data = Project::find($project->id)i receive the updated model.
But is this the only way?
The reason the $data is returning as the old data is because it still stores the data from when the variable was created. When you call $data->fresh() it will go fetch the new data and return it. Does that make sense?

want to generate pdf from laravel request data

i tried to generate pdf from my request data function. but it can't pass variable into my pdf generate function. i get requested data from this function.
public function fetch_data()
{
if(request()->ajax())
{
if(request()->from_date != '' && request()->to_date != '')
{
$data = DB::table('sales')
->whereBetween('dlvd_date', array(request()->from_date, request()->to_date))
->get();
} else {
$data = DB::table('sales')->orderBy('dlvd_date', 'desc')->get();
}
echo json_encode($data);
}
}
i want to get this $data value into my pdf generated function like below.
function convert_sales_data_to_html($data)
{
$sales_data = $data;
dd($sales_data);
$output = '';
return $output;
}
and pass to this function
public function pdf($data)
{
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($this->convert_sales_data_to_html($data))->setPaper('a4', 'landscape');
return $pdf->stream();
}
i tried this so many times but show me errors with missing argument. how can i fix this or have another method to generate pdf from requested data in laravel.
this is my routes
Route::post('salesreport/fetch', 'Report\SalesReportController#fetch_data')->name('salesreport.fetch_data');
Route::get('salesreport/pdf', 'Report\SalesReportController#pdf');

Call to a member function setPath() on a non-object in laravel

In my Laravel web application, getting error like "Call to a member function setPath() on a non-object" while using pagination.
My code is
In model,
public function scopegetApartments()
{
$list1 = DB::table('properties')->orderBy('id','DESC')->paginate(1);
$list = array();
foreach($list1 as $listeach){
$imgs = DB::table('property_images')->where('property_id',$listeach->id)->get();;
$images = array();
foreach($imgs as $img){
array_push($images, $img->image);
}
$lists = array(
'id'=>$listeach->id,
'name'=>$listeach->name,
'rent'=>$listeach->rent,
'shortdescription'=>$listeach->shortdescription,
'images'=>$images,
);
array_push($list, $lists);
}
if(count($list)>0){
return $list;
}else{
return '[]';
}
}
In view,
$apartmentlist->setPath('serviceapartments');
echo $apartmentlist->render();
How to resolve this problem?

How to prevent code duplication for CodeIgniter form validation?

This is sample of function in the Staff controller for this question
function newStaff()
{
$data = array();
$data['departmentList'] = $this->department_model->list_department();
$data['branchList'] = $this->branch_model->list_branch();
$data['companyList'] = $this->company_model->list_company();
$this->load->view('staff/newstaff', $data);
}
function add_newStaff()
{
//when user submit the form, it will call this function
//if form validation false
if ($this->validation->run() == FALSE)
{
$data = array();
$data['departmentList'] = $this->department_model->list_department();
$data['branchList'] = $this->branch_model->list_branch();
$data['companyList'] = $this->company_model->list_company();
$this->load->view('staff/newstaff', $data);
}
else
{
//submit data into DB
}
}
From the function add_newStaff(), i need to load back all the data from database if the form validation return false. This can be troublesome since I need to maintain two copy of codes. Any tips that I can use to prevent this?
Thanks.
Whats preventing you from doing the following
function newStaff()
{
$data = $this->_getData();
$this->load->view('staff/newstaff', $data);
}
function add_newStaff()
{
//when user submit the form, it will call this function
//if form validation false
if ($this->validation->run() == FALSE)
{
$data = $this->_getData();
$this->load->view('staff/newstaff', $data);
}
else
{
//submit data into DB
}
}
private function _getData()
{
$data = array();
$data['departmentList'] = $this->department_model->list_department();
$data['branchList'] = $this->branch_model->list_branch();
$data['companyList'] = $this->company_model->list_company();
return $data;
}
Alternately you change the action your form submits to so that it points to the same service you use for the initial form request with something like the following. This would also mean that you'd have the POST values retained between page-loads if you wanted to retain any of the submitted values in your form.
function newStaff()
{
// validation rules
if ($this->validation->run() == TRUE)
{
//submit data into DB
}
else
{
$data = array();
$data['departmentList'] = $this->department_model->list_department();
$data['branchList'] = $this->branch_model->list_branch();
$data['companyList'] = $this->company_model->list_company();
$this->load->view('staff/newstaff', $data);
}
}

Resources