2017-06-08 2 views
-3

Wie kann ich Dinge aus meiner Datenbank in Laravel 5.0 mit der öffentlichen Funktion zerstören und bearbeiten?Laravel 5.0 löschen und in der Datenbank bearbeiten

Das ist meine Bibliothek, hier möchte ich etwas aus meiner Datenbank löschen oder aktualisieren

@foreach ($allJobs as $job) 
    <tr> 
     <td><img src="{{$job->selected_icon}}" width="50" /> </td> 
     <td>{{$job->jobtitle_de}}</td> 
     <td>{{$job->location}}</td> 
     <td><img src="{{$job->selected_image}}" width="100" /></td> 
     <td>{{$job->workinghours}}</td> 
     <td>{{$job->grey_header_de}}</td> 
     <td>{{$job->date}}</td> 
     <td> 
      <a href=""><button>Edit</button></a>&nbsp;<a href="/delete"> 
      <button>Delete</button></a></td> 
      <!--<td> <button type="delete" name="button"></button>--> 
     <td> 
     </td> 
    </tr> 
@endforeach 
+0

Sie verbinden das Bearbeiten und Löschen von Tasten auf eine Funktion tun wollen in Ihrem Controller, wo Sie ein Bearbeitungsformular laden oder das ausgewählte Modell löschen. – Jerodev

+0

Dies ist nur der Code für Ihre Ansichten. Sie müssen Code von Ihrem Controller teilen, wo diese Aktionen passieren – manian

+0

Wie sollte ich die öffentliche Funktion in meinem Controller machen? –

Antwort

0

jetzt fand ich diese im Internet:

<div class="library"> 
<table> 
@foreach ($allJobs as $job) 
<tr> 
    <td><img src="{{$job->selected_icon}}" width="50" /> </td> 
    <td>{{$job->jobtitle_de}}</td> 
    <td>{{$job->location}}</td> 
    <td><img src="{{$job->selected_image}}" width="100" /></td> 
    <td>{{$job->workinghours}}</td> 
    <td>{{$job->grey_header_de}}</td> 
    <td>{{$job->date}}</td> 
<td> 
    {{ Form::open(['method' => 'DELETE', 'route' => 'job.destroy', $job]- 
    >id]) }} 
     {{ Form::hidden('id', $job-id) }} 
     {{ Form::submit('delete', ['class' => 'library']) }} 
    {{Form::close}} 
    </td> 
</tr> 
@endforeach 

und dies ist mein Controller-

public function destroy($id) 
{ 
    $jobs = Job::findOrFail($id); 
    $jobs->delte(); 

    return redirect::route('/'); 
} 
0

In Ihrem Controller (ich nehme an, dass Sie haben erstellt), implementiert diese beiden Funktionen.

 
public function edit($id) { 
    // Create a var called `$job` and uses the function `find()` passing into the $id that you clicked before 

    $job = Job::find($id); 

    // Return a view with the object that you found into a edit view 
    return view('jobs.edit', [ 
     'job' => $job 
    ]); 
} 

public function destroy($id) { 
    // Use the function `find()` passing into the $id that you clicked before and that use the delete method to delete the job 
    Job::find($id)->delete(); 

    // Returning into a route that contains the jobs, probably 
    return redirect()->route('jobs'); 
} 

Lesen Sie die Dokumentation https://laravel.com/docs/5.4/controllers

+0

funktioniert nicht. Mein Fehler: FatalErrorException in 2a7615e155aadcd67e784c19418651ba Zeile 16: Syntaxfehler, unerwartet '->' (T_OBJECT_OPERATOR) –

+0

Poste bitte deinen Controller – wbail

0
my Controller: 

<?php namespace App\Http\Controllers; 

use App\Http\Requests; 
use App\Http\Controllers\Controller; 
use Request; 
use App\Jobs; 

class JobController extends Controller { 

/** 
* Display a listing of the resource. 
* 
* @return Response 
*/ 
public function index() 
{ 
    return view('library',['allJobs' => Jobs::all()]); 
} 

//my create function 
public function create() 
{ 
    return view('add'); 
} 
/** 
* Store a newly created resource in storage. 
* 
* @return Response 
*/ 
public function store(Request $request) 
{ 
    $job = new Jobs(); 

    $job->jobtitle_de = array_key_exists('jobtitle_de',$_POST) ? 
$_POST['jobtitle_de'] : ''; 
    $job->jobtitle_en = array_key_exists('jobtitle_en',$_POST) ? 
$_POST['jobt'] : ''; 


    if (array_key_exists('place', $_POST)) { 
     $places = $_POST['place']; 
     $placesString = ""; 
     foreach ($places as $p) { 
      $placesString .= $p.','; 
     } 
     $job->location = $placesString; 
    } 

    $job->workinghours = array_key_exists('workinghours',$_POST) ? 
    $_POST['workinghours'] : ''; 
    $job->workinghours_de = array_key_exists('workinghours',$_POST) ? 
    $_POST['workinghours'] : ''; 
    $job->selected_image= array_key_exists('selected_image',$_POST) ? 
    $_POST['selected_image'] : ''; 
    $job->grey_header_de = array_key_exists('grey_header_de',$_POST) ? 
    $_POST['grey_header_de'] : ''; 



    $job->selected_icon = array_key_exists('selected_icon',$_POST) ? 
    $_POST['selected_icon'] : ''; 
    $job->selected_icon = array_key_exists('selected_icon',$_POST) ? 
    $_POST['selected_icon'] : ''; 
    $job->selected_icon = array_key_exists('selected_icon',$_POST) ? 
    $_POST['selected_icon'] : ''; 
    $job->selected_icon = array_key_exists('selected_icon',$_POST) ? 
    $_POST['selected_icon'] : ''; 



    $job->date; 

    if (array_key_exists('date',$_POST) && !empty($_POST['date'])) { 
     $date = $_POST['date']; 
     $date = explode('/',$_POST['date']); 
     $newdate = $date[2]."-".$date[0]."-".$date[1]; 
     $job->date = $newdate; 
    } 

    $job->grey_header_de = $_POST['grey_header_de']; 

    if (array_key_exists('workinghours',$_POST) && $_POST['workinghours'] 
=== "full-time") { 
     $job->workinghours = $_POST['workinghours']; 
     $job->workinghours_de = "Vollzeit"; 
    } 
    if (array_key_exists('workinghours',$_POST) && $_POST['workinghours'] 
=== "part-time"){ 
     $job->workinghours = $_POST['workinghours']; 
     $job->workinghours_de = "Teilzeit"; 
    } 
    try { 
     $job->save(); 
    } 
    catch (Exceptions $e) { 
     echo $e->getMessage(); 
    } 

    return redirect()->action('[email protected]'); 
} 


//my edit function 
public function edit($id) 
{ 
    $job = Job::find($id); 

    return view('jobs.edit', [ 
     'job' => $job 
    ]); 
} 

//destroy function 
public function destroy($id) 
{ 
    Job::find($id)->delete(); 

    return redirect()->route('jobs'); 
} 
Verwandte Themen