laravel 5.0在数据库中删除和编辑

时间:2017-06-08 10:45:00

标签: php html laravel

如何使用公共功能销毁和编辑从Laravel 5.0中的数据库中删除或编辑内容?

这是我的图书馆,在这里我想从我的数据库中删除或更新某些内容

@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

4 个答案:

答案 0 :(得分:0)

现在我在互联网上找到了这个:

<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

     这是我的控制器

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

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

答案 1 :(得分:0)

在你的控制器中(我假设你已创建),实现这两个功能。

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');
}

阅读文档https://laravel.com/docs/5.4/controllers

答案 2 :(得分: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('JobController@index');
}


//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');
}

答案 3 :(得分:0)

在你的路线

Route::post('delete','CallyourController@delete');
Route::post('update','CallyourController@update');

我认为这就是你想要做的事情