Laravel: Array to string conversion - laravel

SettingController.php
public function SendPicture(Request $request) {
$title = "Picture Purchase";
$domain = $_SERVER['SERVER_NAME'];
$email = DB::table('users') - > where('domain', $domain) - > get();
$content = "$email purchase your picture : ";
foreach($request - > input('pic') as $key => $value) {
$content. = "$value".".jpg ";
}
}
ErrorException in SettingController.php line 373: Array to string
conversion
line 373: $content = "$email purchase your picture : ";

If you've only one record you can use value method for getting direct string:
$email = DB::table('users')->where('domain', $domain)->value('email');
$content = $email ." purchase your picture : ";
Note : There is a logical error in your each loop too, remove "" from $value :
foreach($request->input('pic') as $key => $value) {
$content. = $value.".jpg ";
}

$email variable have more then one record. If you use $email as email address from users table then you can follow below code :
$email = DB::table('users')->where('domain', $domain)->first();
$content = $email."purchase your picture : ";
Hope this help for you!!!

Related

I am trying to send multiple title and multiple files into database using laravel but i am getting error

I am trying to send multiple title and multiple files into database using laravel but i am getting error please help me how can i resolve this issue ? thanks.
getting error
Cannot use object of type Illuminate\Http\UploadedFile as array
CONTROLLER
public function store(Request $request)
{
foreach ($request->title as $key => $value) {
$audiodetail = new AudioDetail;
$extension = Str::random(40) . $request->file('audio_file_upload')->getClientOriginalExtension();
$audiodetail->audio_file = Storage::disk('audiofile')->putFileAs('', $request->audio_file_upload[$key], $extension);
$audiodetail->title = $value;
$audiodetail->audio_id = $event->id;
$audiodetail->save();
}
return redirect()->route('events');
}
title and audio_file_upload both are different array. So you need to combine both :
public function store(Request $request)
{
$data = array_combine($request->title, $request->audio_file_upload);
foreach ($data as $value) {
$audiodetail = new AudioDetail;
$extension = Str::random(40) . $value['audio_file_upload']->getClientOriginalExtension();
$audiodetail->audio_file = Storage::disk('audiofile')->putFileAs('', $value['audio_file_upload'], '.' . $extension);
$audiodetail->title = $value['title'];
$audiodetail->audio_id = $event->id;
$audiodetail->save();
}
return redirect()->route('events');
}

How to convert MySqli Loop Code Laravel controller

I want to work on a while loop in a foreach loop but I can't convert it to Laravel controller system. Please help.
foreach (range('a', 'z') as $i)
{
echo $i = strtolower($i);
$sql = "SELECT * FROM `list` Where name like '$i%' Limit 30";
$result = mysqli_query($con, $sql);
echo"<hr><h3>Name Starting with ".strtoupper($i)."</h3>";
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = $row['id'];
$name = $row['name'];
echo "<li>".$row['name']."</li>";
}
echo "<li>More</li>";
echo "</ul>";
}
The code in Laravel that I have is
foreach (range('a', 'z') as $i)
{
$list = DB::table('list')->where('name', 'LIKE', $i.'%')->limit(30)->get();
return view('pages.all', ['list' => $list]);
}
This code only gives data for names starting with alphabet "a" but won't proceed with the rest of the characters.
Store it on the array based on your search.
$list = array();
foreach (range('a', 'z') as $i) {
$list[$i] = DB::table('list')->where('name', 'LIKE', $i.'%')->limit(30)->get();
}
return view('pages.all', ['list' => $list]);
Customize your view, to showing the list against each character (a-z).
[Edit]
View file can be changed like below,
#foreach ($list as $keyI => $moredata)
<hr>
<h3>Author's Name Starting with "{{strtoupper($keyI)}}"</h3>
<ul class="tags-list">
#foreach ($moredata as $data)
<li>{{$data->name}}</li>
#endforeach
</ul>
#endforeach

message: undefined variable in view

I am trying to get the data in my view in codeigniter but it gives me a Message: Undefined variable: topics_array
here is my model :
public function studentTopics($courseId, $periodId)
{
$this->dbi->select('topicitem.TopicItemID, topicItem.Content');
$this->dbi->from('topicitem, topics, periods');
$this->dbi->where('topics.PeriodItemID', $periodId)
->where('topics.TopicItemID = topicitem.TopicItemID')
->where('topics.PeriodItemID = periods.PeriodItemID')
->where('periods.CourseID', $courseId);
$res = $this->dbi->result_array();
return $res;
}
here is my controller
public function student_topic($courseId, $periodId)
{
$this->load->library('table');
$this->load->model('Student_model');
$data['topics_array'] = $this->Student_model->studentTopics($courseId, $periodId);
$this->load->view('Student/StudentView', $data);
}
and my view
<?php
$courseID = $this->uri->segment(4);
$periodID = 1;
foreach ($topics_array as $key =>$value) {
echo "<tr>
<td>".$value['Content']. "</td>
</tr>"; }?>
You seem to be missing a get().
$res = $this->dbi->get()->result_array();
return $res;
Hope this will help you :
change this line of code : $res = $this->dbi->result_array();
$this->dbi->select('topicitem.TopicItemID, topicItem.Content');
$this->dbi->from('topicitem, topics, periods');
$this->dbi->where('topics.PeriodItemID', $periodId)
->where('topics.TopicItemID = topicitem.TopicItemID')
->where('topics.PeriodItemID = periods.PeriodItemID')
->where('periods.CourseID', $courseId);
$query = $this->dbi->get(); // missing this line of code
$res = $query->result_array();
for more : https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data

Severity Notice: Undefined offset

With some changes I got the error, This work fine in raw php but in codeigniter I'm trying to get this result_array in a while(list), but I don't know what would be the alternative for fetch_row in codeigniter. Please help me to get this in a way at while (list($d, $sn, $s) = each($IDS-)) line.
Model:
public function raw_attendance_Data() {
$this->db->select('attnDate, TraineeID, Status');
$this->db->from('tbl_attendance');
$this->db->order_by('TraineeID');
$query = $this->db->get();
return $query->result_array();
}
Controller:
public function rawAttendance(){
$data['IDS'] = $this->AttendanceModel->raw_attendance_Data();
$this->load->view('attendance/rawAttendance', $data);
}
View:
$curname='';
$tdata = '';
reset($IDS);
while (list($d, $sn, $s) = each($IDS)) {
if ($curname != $sn) {
if ($curname) {
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
}
$rowdata = $emptyRow;
$curname = $sn;
}
$rowdata[$d] = $s;
}
$tdata .= "<tr><td>$curname</td><td>" . join('</td><td>', $rowdata). "</td></tr>\n";
$query->result_array(); returns an array and not an object. Thus the $IDS in $IDS->fetch_row() is an array (a non-object).
I don't understand, where you are getting the function fetch_row()?
If I understand what you are trying to accomplish then this should work.
View:
$tdata = '';
foreach($IDS as $row)
{
$curname = isset($row['TraineeID']) ? $row['TraineeID'] : "";
$date = isset($row['attnDate']) ? $row['attnDate'] : "";
$status = isset($row['Status']) ? $row['Status'] : "";
if(!empty($curname){
$tdata .= "<tr><td>$curname</td><td>$date</td><td>$status</td></tr>";
}
}

ErrorException preg_replace(): Parameter mismatch, pattern is a string while replacement is an array. Laravel

I have a form input field that accepts an array of values:
<input name="subjects_studied[]" type="text" placeholder="" size="30" class="form-styles" >
In my routes.php script I have a closure like thus:
Route::post('apply', function() {
$v = ApplicationForm::validate(Input::all());
if ( $v->passes() ) {
$data = array(
// 'subjects_studied' => Input::get('subjects_studied'),
'subjects_studied' => (is_array(Input::get('subjects_studied'))) ?
implode(" , " , Input::get('subjects_studied')) :
Input::get('subjects_studied'),
);
ApplicationForm::create($data);
}
}
I've tried both configs and they both give me an error:
ErrorException
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array.
As a last throw of the dice, I tried:
$subjects_studied = Input::get('subjects_studied');
foreach($subjects_studied as &$studied){
$_subjects[] = $studied;
}
$x = implode($_subjects);
$data = array('subjects_studied' => $x,)
Could you help me insert an array of value(s) from a HTML form into a MySQL database using laravel?
What I understand is that you need a loop to get all this values and save to database.
What I would do is:
$subjects_studied = Input::get('subjects_studied');
for ($i=0; $i < count($subjects_studied) ; $i++) {
$subject = new ApplicationForm;
$subject->subjects_studied = $subjects_studied[$i]
$subject->save();
}
or try with foreach
foreach ($subjects_studied as $key => $value) {
$subject = new ApplicationForm;
$subject->subjects_studied = $value
$subject->save();
}
good luck

Resources