Keep getting error: implode(): Invalid arguments passed when updating data - php

I keep on getting this error whenever I try to update something into my data. It didn't happen a lot the last time but after putting the new update function, the error keep popping out and for some reason it points back to the new function that I had entered even though I am updating at the old function.
Controller:
//update for user
public function edit($id){
$object = user::find($id);
return view('edit', compact('object'));
}
public function update(Request $request, $id){
$object = user::find($id);
$object->Name = $request->input('Name');
$object->update();
return redirect('/home');
}
//update for Schools table
public function edit1($id){
$object2 = school::find($id);
return view('edit1', compact('object2'));
}
public function update1(Request $request, $id){
$object2 = school::find($id);
$test = array();
$test['School'] = implode(' , ', $request->School);
$test['SDate'] = implode(' , ', $request->SDate);
$test['EDate'] = implode(' , ', $request->EDate);
$object2->update($test);
return redirect('/home');
}
//error start here after putting this whole thing in. (I tried putting it into another separate controller but the error still continues)
public function edit2($id){
$object3 = hobby::find($id);
return view('edit2', compact('object3'));
}
public function update2(Request $request, $id){
$object3 = hobby::find($id);
$test2 = array();
$test2['computer_game'] = implode(' , ', $request->computer_game);
$test2['reading_book'] = implode(' , ', $request->reading_book);
$object3->update($test2);
return redirect('/home');
}
Error is highlighting this part even though when I try to update user or school data
$test2['computer_game'] = implode(' , ', $request->computer_game);
And it says
:implode(): Invalid arguments passed
I have no issue updating the hobby data but it the error keep pointing it back to the hobby implode part.
Is it possible I could only use update once on an implode function? Thanks in advance
edit2.blade.php (edit page for hobby, as shown it is an array)
<form class="form-horizontal" method="post" action="{{ url('/user/show/'.$object3->id) }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
Sports:
</th>
<th class="text-center">
Books read:
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<input type="text" name='computer_game[]' class="form-control"/>
</td>
<td>
<input type="text" name='reading_book[]' class="form-control"/>
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
<input type="submit" class="btn btn-primary" value="Save">

Just simple try the following:
$test2['computer_game'] = implode(' , ', (array)$request->computer_game);
This will convert $request->computer_game in to an array if it is not already.

You are getting the error because $request->computer_game is not an array. This can be verified by dumping the variable and killing the script.
var_dump($request->computer_game); die;

first of all what does var_dump(); gives to that object oriented array. plz share then we'll try to answer.
Then to stop multiple loading you can make a private var ($loaded) and by default it would be falseprivate static $loaded = false; then befor any code starts in function use if statement to detect if loaded is true
if(self::$loaded){
return;
}
self::$loaded = true
this would help you! please post var_dump!

Related

Destroy() in Basic Controller is not working

So I'm printing user complaints in table where I'm also printing a Delete button with every row. When I click that delete button, I want to delete that specific complaint from the table. I'm not using Resource Controller for this but a Basic Controller. Now, this is my code:
ViewComplaint.blade.php (Complaints Table with Delete Button):
<table id="cTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Student Name</th>
<th>Complaint Title</th>
<th>Complaint Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($complaints as $complaint)
<tr>
<td>{{ $complaint->name }}</td>
<td>{{ $complaint->cname }}</td>
<td>{{ $complaint->cbody }}</td>
<td class="btn-group">
{!! Form::open(array('route'=>['complaint.destroy',$complaint->id],'method'=>'DELETE')) !!}
{!! Form::submit('Delete',['type'=>'submit','style'=>'border-radius: 0px;','class'=>'btn btn-danger btn-sm',$complaint->id]) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
Web.php (Routes):
Route::get('/complaint/create','ComplaintController#create')->name('complaint.create');
Route::post('/complaint','ComplaintController#store')->name('complaint.store');
Route::get('/complaint','ComplaintController#index')->name('complaint.index');
Route::delete('/complaint/{$complaint->id}','ComplaintController#destroy')->name('complaint.destroy');
ComplaintController.php (Basic Controller):
class ComplaintController extends Controller
{
public function index() {
$complaints = Complaint::all();
return view('viewcomplaint',compact('complaints'));
}
public function create(User $user) {
$user = User::all();
$user->name = Auth::user()->name;
return view('createcomplaint',compact('user'));
}
public function store(Request $request, Complaint $complaint, User $user) {
$user = User::find($user);
$complaint->name = Auth::user()->name;
$complaint->cname = $request->input('cname');
$complaint->cbody = $request->input('cbody');
//update whichever fields you need to be updated
$complaint->save();
return redirect()->route('home.index');
}
public function destroy(Complaint $complaint,$id)
{
$complaint = Complaint::findOrFail($complaint->id);
$complaint->delete();
return redirect()->route('complaint.index');
}
}
Now when I click the Delete button on the table, it just gives me "404 | Not Found" error. What am I doing wrong here? I would really appreciate some help.
remove the $id from the route
Route::delete('/complain/{id}','ComplaintController#destroy')->name('complaint.destroy');
public function destroy($id) {
}
The route parameter is just a name; you are saying this particular route segment is dynamic and I want the parameter named complaint:
Route::delete('complaint/{complaint}', 'ComplaintController#destroy')->name('complaint.destroy');
Then you can adjust your destroy method to take the parameter complaint typehinted as Complaint $complaint to get the implicit binding:
public function destroy(Complaint $complaint)
{
$complaint->delete();
return redirect()->route('complaint.index');
}
Seems to me you're defining your route wrong. Change your route to:
Route::delete('/complaint/{id}','ComplaintController#destroy')->name('complaint.destroy');
You don't need an array() in your form opening, so hange your form opening to this:
{!! Form::open(['method' => 'DELETE', 'route' => ['complaint.destroy',$complaint->id]]) !!}
And remove the $complaint->id from your submit button, you don't need it there.
All you have to do now inside your function is to find Complaint that has the id you passed in your form:
public function destroy($id)
{
$complaint = Complaint::findOrFail($id);
$complaint->delete();
return redirect()->route('complaint.index');
}
Let me know if you stumble on any errors.

Undefined variable: libros (View: C:\Users\CDS22\Desktop\Crud\resources\views\libros\index.blade.php) on laravel

I'm receiving an array from my controller to my view to map it with the #foreach blade loop, but after an insertion into the database, when I want to redirect my app to the index blade document, this error appears:
This is my controller:
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return view('libros.index');
}
And this is my view:
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Titulo</th>
</tr>
</thead>
<tbody>
#foreach ($libros as $libro)
<tr>
<td>{{ $libro->id }}</td>
<td>{{ $libro->titulo }}</td>
</tr>
#endforeach
</tbody>
</table>
I expect that my controller method redirect send me to the view correctly, but everytime I enter a register to the database and the controller redirects me to my blade view, this error appears and I have to reload the page to enter into the view.
In your store method you either have to redirect the user to the index page, or retrieve and pass all the libros array again.
The former way is preferred.
Assuming you have given a name to your route, you can do something like:
public function store(Request $request)
{
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return redirect()->route('libros.index'); // replace libros.index with the index route name
}
I think it will work if you rewrite like this
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
$libros = Libro::all()->get();
return view('libros.index', compact('libros'));
}

Laravel AJAX and pagination with no url

I have table displaying many rows, and I'm using pagination and sort function, also I'm using ajax to return number of rows and other ajax to return rows between two dates.
The problem is if I wanted to sort rows and in the same time show some rows between two dates this wont work with me. Because when using ajax there is no url.
public function index()
{
$checks = Checks::orderBy('id', 'asc')->get();
$checks= Checks::sortable()->paginate(10);
return view('home',compact('checks'));
}
public function showpage(Request $request)
{
if($request->ajax())
{
$checks= Checks::orderBy('id', 'asc')->paginate($request->inputpage);
return view('layouts.showcheks',compact('checks'));
}
}
public function getCheckReport(Request $request)
{
if($request->ajax()){
$New=$request->StartDate;
$Old=$request->EndDate;
$checks= Checks::whereBetween('postingdate',[$New,$Old])->sortable()->orderBy('postingdate', 'asc')->get();
return view('layouts.showcheks',compact('checks'));
}
}
showchecks.blade.php
#foreach($checks as $indexKey => $check)
<tr >
<td>{{$check->details}}</td>
<td>{{date('m/d/Y', strtotime($check->postingdate))}}</td>
<td>{{$check->description}}</td>
</tr>
#endforeach
homepage:
<table class="table" id="postTable">
<thead>
<tr>
<th>#sortablelink('details','Details')</th>
<th>#sortablelink('postingdate','Date')</th>
<th>#sortablelink('description','Description')</th>
</tr>
{{ csrf_field() }}
</thead>
<tbody>
#foreach($checks as $indexKey => $check)
<tr >
<td>{{$check->details}}</td>
<td>{{date('m/d/Y', strtotime($check->postingdate))}}</td>
<td >{{$check->description}}</td>
</tr>
#endforeach
</tbody>
</table>
{{$checks->appends(Request::input())->links()}}
use a datatable https://datatables.net/ with ajax that is the best way also u can sorting a rows also..

Passing API data to view Laravel

I can't figure out why my data is not being passed to View
I get the message Undefined variable: project
Can someone help identify where i am going wrong?
when i return $project->name straight from the controller it displays the response, however fails to pass to view.
Here is my controller function:
public function getApi(){
$client = new Client();
$response = $client->get('https://play.geokey.org.uk/api/projects/77');
$body = $response->getBody()->getContents();
$project = json_decode($body);
return view ('response', compact($project));
}
And here is my View:
<h1>Welcome</h1>
<table class="table table-bordered">
<tr>
<th>Project Name</th>
<th>Date of Creation</th>
<th>Contribution Total</th>
</tr>
<tr>
<td>{{$project->name}}</td>
</tr>
</table>
Instead of:
return view ('response', compact($project));
Type
return view ('response', compact('project'));
return view ('response', compact('project')); instead of compact($project)

Undefined variable: no_marks (laravel 5.3)

I am new to Laravel and I am getting following error Undefined variable: no_student (Laravel 5.3)
Here is my view. blade
#foreach($no_marks as $no_mark)
<tr class="gradeX odd" role="row" id="">
<td style="display:none"></td>
<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
<input type="checkbox" class="checkboxes checkbox-index" value="">
<span></span>
</label>
</td>
<td></td>
<td></td>
<td></td>
<td>
#endforeach
my index method in controller
public function index()
{
//
$no_marks = Markno::orderBy('id', 'desc')->get();
return view('no-mark.index');
}
I am storing record using this store method
public function store(Request $request)
{
$this->validate($request, [
'ip_range' => 'required|max:255',
]);
$ip_data['ip_range'] = $request->input('ip_range');
$ip_data['min_ip'] = $request->input('ip_range');
$ip_data['max_ip'] = $request->input('ip_range');
$ip_data['list_id'] = $request->input('list_id');
$ip_data['user_id'] = Auth::user()->id;
$no_marks = Markno::create($ip_data);
session()->flash('msg', ' Successfully created');
return view('no-mark.index');
}
I don't know where I am going wrong. Please help to fix it.
You did not passed your variable $no_marks into your view that's why it is undefined to passed it
return view('no-mark.index', array('no_marks' => $no_marks));
To use a variable available on controller you have to pass it on view like:
return view('no-mark.index', array('no_marks' => $no_marks));
here the second parameter is the array, you can access its index on view page
please update your action
public function index()
{
//
$no_marks = Markno::orderBy('id', 'desc')->get();
return view('no-mark.index',compact(['no_marks']));
}

Categories