我是laravel的初学者,我按照教程创建了一个简单的小游戏
经过一些操作,我得到了不错的结果,并且看到了一些奇怪的东西。 当我使用:: find方法通过id获取数据时,返回的结果与数据库结果并不完全相同。
correct data (look at the line where id = 4)
Data after a click on the edit button
您可以看到第二个输入缺少一部分,而第三个输入则有一个额外的“ /”。
somoene知道如何纠正这种行为吗?
这是第一张图片中的index.blade.php页面
@extends('layout')
@section('content')
<style>
.uper {
margin-top: 40px;
}
</style>
<div class="uper">
@if(session()->get('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div><br />
@endif
<table class="table table-striped">
<thead>
<tr>
<td>ID</td>
<td> nom</td>
<td> Price</td>
<td> prenom</td>
<td> adresse</td>
<td> collectivite</td>
<td> telephone</td>
<td> mail</td>
<td> ref</td>
<td colspan="2">Action</td>
</tr>
</thead>
<tbody>
@foreach($clients as $client)
<tr>
<td>{{$client->id_client}}</td>
<td>{{$client->nom_client}}</td>
<td>{{$client->prenom_client}}</td>
<td>{{$client->adresse_client}}</td>
<td>{{$client->nom_collectivite}}</td>
<td>{{$client->telephone_client}}</td>
<td>{{$client->mail_client}}</td>
<td>{{$client->ref_client}}</td>
<td><a href="{{ route('clients.edit',$client->id_client)}}" class="btn btn-primary">Edit</a></td>
<td>
<form action="{{ route('clients.destroy', $client->id_client)}}" method="post">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
<div>
@endsection
这是第二张图片中的edit.blade.php页面
@extends('layout')
@section('content')
<style>
.uper {
margin-top: 40px;
}
</style>
<div>sdsq</div>
<div class="card uper">
<div class="card-header">
Edit client
</div>
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br />
@endif
<form method="post" action="{{ route('clients.update', $client->id_client) }}">
@method('PATCH')
@csrf
<div class="form-group">
<label for="nom">nom client:</label>
<input type="text" class="form-control" name="nom_client" value={{$client->nom_client}} />
</div>
<div class="form-group">
<label for="prenom">prenom client:</label>
<input type="text" class="form-control" name="prenom_client" value={{$client->prenom_client}} />
</div>
<div class="form-group">
<label for="adresse">adresse client:</label>
<input type="text" class="form-control" name="adresse_client" value={{$client->adresse_client}}/>
</div>
<div class="form-group">
<label for="collectivite">collectivite client:</label>
<input type="text" class="form-control" name="nom_collectivite" value={{$client->nom_collectivite}} />
</div>
<div class="form-group">
<label for="telephone">telephone client:</label>
<input type="text" class="form-control" name="telephone_client" value={{$client->telephone_client}} />
</div>
<div class="form-group">
<label for="mail">mail client:</label>
<input type="text" class="form-control" name="mail_client" value={{$client->mail_client}} />
</div>
<div class="form-group">
<label for="ref">ref client:</label>
<input type="text" class="form-control" name="ref_client" value={{$client->ref_client}} />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
@endsection
这是我的ClientController.php控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\client;
class ClientController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$clients = client::all();
return view('clients.index', compact('clients'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('clients.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'nom_client'=>'required',
'prenom_client'=>'required',
'adresse_client'=>'required',
'nom_collectivite'=>'required',
'telephone_client'=>'required',
'mail_client'=>'required',
'ref_client'=>'required'
]);
$client = new client([
'nom_client' => $request->get('nom_client'),
'prenom_client'=> $request->get('prenom_client'),
'adresse_client'=> $request->get('adresse_client'),
'nom_collectivite' => $request->get('nom_collectivite'),
'telephone_client'=> $request->get('telephone_client'),
'mail_client'=> $request->get('mail_client'),
'ref_client'=> $request->get('ref_client')
]);
$client->save();
return redirect('/clients')->with('success', 'Stock has been added');
}
/**
* Display the specified resource.
*
* @param int $id_client
* @return \Illuminate\Http\Response
*/
public function show($id_client)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id_client
* @return \Illuminate\Http\Response
*/
public function edit($id_client)
{
$client = client::find($id_client);
return view('clients.edit', compact('client'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id_client
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id_client)
{
$request->validate([
'nom_client'=>'required',
'prenom_client'=>'required',
'adresse_client'=>'required',
'nom_collectivite'=>'required',
'telephone_client'=>'required',
'mail_client'=>'required',
'ref_client'=>'required'
]);
$client = client::find($id_client);
$client->nom_client = $request->get('nom_client');
$client->prenom_client = $request->get('prenom_client');
$client->adresse_client = $request->get('adresse_client');
$client->nom_collectivite = $request->get('nom_collectivite');
$client->telephone_client = $request->get('telephone_client');
$client->mail_client = $request->get('mail_client');
$client->ref_client = $request->get('ref_client');
$client->save();
return redirect('/clients')->with('success', 'Stock has been updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id_client
* @return \Illuminate\Http\Response
*/
public function destroy($id_client)
{
$client = client::find($id_client);
$client->delete();
return redirect('/clients')->with('success', 'Stock has been deleted Successfully');
}
}
这是我的client.php类
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class client extends Model
{
public $timestamps = false;
protected $primaryKey = 'id_client';
protected $fillable = [
'nom_client',
'prenom_client',
'adresse_client',
'nom_collectivite',
'telephone_client',
'mail_client',
'ref_client'
];
}
答案 0 :(得分:1)
您的代码将生成给定的html,因为您没有引用该值:
<input type="text" class="form-control" name="adresse_client" value=s/>
您需要引用value="{{$client->adresse_client}}"
<input type="text" class="form-control" name="adresse_client" value="s"/>
答案 1 :(得分:1)
关于我想补充的一点是,表顺序不正确
<table class="table table-striped">
<thead>
<tr>
<td>ID</td>
<td> nom</td>
<td> Price</td>
<td> prenom</td>
<td> adresse</td>
<td> collectivite</td>
<td> telephone</td>
<td> mail</td>
<td> ref</td>
<td colspan="2">Action</td>
</tr>
</thead>
<tbody>
@foreach($clients as $client)
<tr>
<td>{{$client->id_client}}</td> //ID
<td>{{$client->nom_client}}</td> //nom
//---------------------------------
// PRICE IS MISSING SO either remove from table or pass it from controller
//---------------------------------
<td>{{$client->prenom_client}}</td> // prenom
<td>{{$client->adresse_client}}</td> //adresse
<td>{{$client->nom_collectivite}}</td> //collectivite
<td>{{$client->telephone_client}}</td> //telephone
<td>{{$client->mail_client}}</td> // mail
// ............
在edit.blade.php中,请使用@cbaconnier
提到的所有值添加引号。