Php laravel 5.0在数据库中删除和编辑
如何使用公共函数destroy and edit从Laravel 5.0中的数据库中删除或编辑内容 这是我的库,我想从数据库中删除或更新一些内容Php laravel 5.0在数据库中删除和编辑,php,html,laravel,Php,Html,Laravel,如何使用公共函数destroy and edit从Laravel 5.0中的数据库中删除或编辑内容 这是我的库,我想从数据库中删除或更新一些内容 @foreach ($allJobs as $job) <tr> <td><img src="{{$job->selected_icon}}" width="50" /> </td> <td>{{$job->jobtitle_de}}<
@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> <a href="/delete">
<button>Delete</button></a></td>
<!--<td> <button type="delete" name="button"></button>-->
<td>
</td>
</tr>
@endforeach
@foreach($all jobs作为$job)
所选图标}“width=”50“/>
{{$job->jobtitle_de}
{{$job->location}
所选图片}“width=”100“/>
{{$job->workinghours}
{{$job->grey_header_de}
{{$job->date}
@endforeach
现在我在互联网上找到了:
<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 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');
}
公共功能编辑($id){
//创建一个名为“$job”的变量,并使用函数“find()”传递到之前单击的$id中
$job=job::find($id);
//返回包含在编辑视图中找到的对象的视图
返回视图('jobs.edit'[
“作业”=>$job
]);
}
公共功能销毁($id){
//使用函数'find()`传递到之前单击的$id中,并使用delete方法删除作业
作业::查找($id)->删除();
//返回到包含作业的路径,可能是
return redirect()->route('jobs');
}
阅读文档我的控制器:
<?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');
}
在您的路线中:
Route::post('delete','CallyourController@delete');
Route::post('update','CallyourController@update');
我想这就是您想要做的。您将“编辑”和“删除”按钮链接到控制器中的一个函数,在该函数中加载编辑表单或删除选定的模型。这只是视图中的代码。您需要共享控制器中发生这些操作的代码我应该如何在控制器中设置公共功能?您需要为每个项目使用一个小表单,该表单执行删除请求。然后将其传递给负责删除etcit don'works的控制器方法。我的错误:2a7615e155aadcd67e784c19418651ba第16行中的FatalErrorException:语法错误,意外的'->'(T_OBJECT_运算符)请发布控制器
Route::post('delete','CallyourController@delete');
Route::post('update','CallyourController@update');