I am working on an application and need to pass data from Blade back to the controller
I am passing the data to the blade like this
$reports = $query->paginate();
return view('admin.report', compact('reports'));
this works fine, however I want to have a button to pass $reports collection to this function
public function exportToCSV(Document $reports)
this is going to create a CSV file that will be downloaded including every entry from $reports
I have doing a lot of googling on this and have even have someone help me try to figure it out to no avail.
One way to achieve this would be with a form. You can have a hidden input, with the collection as the value. You would define a route for the form to POST to, and call your function from the route in web.php.
<form action="/route/to/wherever/" method="POST">
{{ csrf_field() }}
<input type="hidden"
name="key"
value="{{ $collection }}"
>
</form>
Be sure to add a submit button. In your method you would receive the collection like:
public function myFunction(Request $request){
$data = $request->key;
}
Related
Am trying to display information of a specific student in my database using student ID. I seem to get null value yet the student exist in the database. i seriously do not know why am getting this null value.
this is what i did
public function showResult(Request $request, $std_id)
{
$user = Student::find($std_id);
if (empty($user)) {
return redirect('/user')
}
$academic = $request->input('academic_year');
$std = $request -> input('studentID');
$results = DB::table('students')
->select('*')
->join('results', 'students.std_id', 'results.student_results_id')
->where('student_results_id', '=', $std)
->where('academic_year', '=', $academic)
->get();
return view('resultsdisplay',[
'results' => $results,
]);
}
}
This is my Route:
Route::get('/student/resultshow/{std_id}', 'ResultsController#showResult');
My studentprofile.blade.php
<li class="list-group-item">
<form action="{{URL::to('/student/resultshow/{!! $studentprofilePage->std_id !!}')}}" method="GET" enctype="multipart/form-data">
<input type="text" name="studentID" value="{!! $studentprofilePage->std_id !!}" hidden="">
<input type="text" name="academic_year" placeholder="Academic year..">
<button>view</button>
</form>
</li>
A couple of things to test. First, check the $std_id variable as it comes into the method to make sure this is translating correctly from your form / route into the method.
public function showResult(Request $request, $std_id){
dd($std_id);
If this is a properly formed integer/id, check the database just to make sure that id is actually assigned within the students table, and it is not soft-deleted if you are using soft-deletes (deleted_at column).
If all good there, take a look at the Student model itself. Is the model pointing to the right table? Laravel assumes the table is named 'students'. If not, have you declared the table within the model?
public $table = 'mySpecialStudentsTableName';
Also, is the id type correct against what is coming in. IE I assume it is an integer, but if it is looking for a string / uuid, is that properly coming out of that $std_id variable?
Finally - your form is not sending the correct id:
<form action="{{URL::to('/student/resultshow/{!! $studentprofilePage->std_id !!}')}}"
Should be:
<form action="{{URL::to('/student/resultshow/'. $studentprofilePage->std_id)}}"
I'm pulling data from database and i would like to make {{$data->title}} clickable.
“This for a new server. In the past, I’ve tried on existing servers.”
#foreach($ress as $key => $data)
{{$data->title}}
#endforeach
Define a route in your routes.php and call it in your template
{{ $data->title }}
This is the best way to do it. You can change the variable names to the ones you have used in your code.
Route.php
Route::get('/show/{id}','DataController#show')->name('showdata);
Link on say dataindex.blade.php
#foreach($datas as $data)
{{ $data->title }}
#endforeach
DataController.php
public function show(){
$datas = DB::table('table_name')->get();
return view('dataindex', ['datas'=>$datas]);
}
I'm trying to make it so the Admin Panel of my website will auto spit out a drop down list for one to one relationship fields...
I know drop downs are made like this in blade templates...
{{ Form::select('list', ['Value' => 'Display']) }}
So I just need a way to add the Eloquent query to populate that array in that format... Any ideas? Is there an out of the box way of doing this? I can't seem to find documentation on it in the Laravel Forms & one to one documentation...
I would imagine I could do something like this, and through the magic of Laravel, it would work...
{{ Form::select('list', MyModel::where('enabled', true)->pluck('title', 'id')) }}
Right now I do this by passing in the list to the view, but I'm wondering if there is a native way to do it...
public function get_option_list()
{
return MyModel::where('enabled', true)->pluck('title', 'id');
}
public function create()
{
return view('admin.myview.create')->with('list', $this->get_option_list());
}
and in blade...
{{ Form::select('list_id', $list) }}
I use Laravel 5.3.
You are doing it right except that you missed it here
public function get_option_list()
{
return MyModel::where('enabled', true)->pluck('title', 'id')->toArray();
}
Everything remains the same
public function create()
{
return view('admin.myview.create')->with('list', $this->get_option_list());
}
And your blade
{{ Form::select('list_id', $list) }}
I'm trying to make a delete button on a post from a user that is logged in. The logged in user can see other peoples posts, and only delete his own. I already managed that the delete button only appears on his own posts. I think the problem is in the route in the view..
Controller:
public function destroy($id)
{
$post = Post::find($id);
$post->delete;
return view('/home', [
'posts' => $post
]);
}
view:
#if ($post->checkUser(Auth::user()))
<form action="{{ route('posts.destroy, $post') }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button>Delete</button>
</form>
#endif
Route:
Route::resource('posts', 'PostController');
Change $post->delete; to $post->delete(); because delete() method is a function. And if you use resource you should send request with delete method for call destroy method. but you can not do it from form because for support only post and get, another solution you can use hidden input and send with post method, <input name="_method" type="hidden" value="DELETE">
You need to specify post_id and user_id both.
public function destroy($id)
{
$user_id = Auth::user();
$post = Post::where('post_id', $id)->where('user_id',$user_id)-get();
$post->delete();
// do your rest of code
}
Change post_id and user_id name according to your table field name.
I think you should edit the route in your view to look like this,
route('posts.destroy, ['id' => $post->id]'), if that doesn't work check your named route again by doing php artisan route:list, and also try to post the full error for better understanding.
Using Laravel 5 I m trying to delete a single record within a controller, here is my code:
public function destroy($id)
{
$employee = Employee::find($id);
$employee->delete();
return Redirect::route('noorsi.employee.index');
}
My view page code is:
<td>Delete</td>
My route is:
Route::delete(employee.'/{id}', array('as' => 'noorsi.employee.destroy','uses' => Employeecontroller.'#destroy'));
That did not work.
How do I fix the implementation ?
From the official Laravel 5 documentation:
Delete an existing Model
$user = User::find(1);
$user->delete();
Deleting An Existing Model By Key
User::destroy(1);
User::destroy([1, 2, 3]);
User::destroy(1, 2, 3);
In every cases, the number between brackets represents the object ID, but you may also run a delete query on a set of models:
$affectedRows = User::where('votes', '>', 100)->delete();
http://laravel.com/docs/5.0/eloquent#insert-update-delete
So the Laravel's way of deleting using the destroy function is
<form action="{{ url('employee' , $employee->id ) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button>Delete Employee</button>
</form>
You can find an example here http://laravel.com/docs/5.1/quickstart-intermediate#adding-the-delete-button
And your route should look something like this
Route::delete('employee/{id}', array('as' => 'employee.destroy','uses' => 'Employeecontroller#destroy'));
It works with eg:Route::resource('employee', 'EmployeeController'); and it should also work with how you set up your destroy route.
Obviously you have a bad routing problem. You're trying to use a 'get' verb to reach a route defined with a 'delete' verb.
If you want to use an anchor to delete a record, you should add this route:
Route::get('/employee/{id}/destroy', ['uses' => 'EmployeeController#destroy']);
or keep using a delete verb, but then you need to use a form (not an anchor) with a parameter called _method and value 'delete' stating that you're using a 'delete' verb.
Route::get('/showcon/{del_id}/delete','MainController#deletemsg');
public function deletemsg($del_id){
$mail=Mail::find($del_id);
$mail->delete($mail->id);
return redirect()->back();
}
del