我是Laravel框架的新粉丝。你能告诉我如何在我的post方法中输入并将其作为属性插入' cli'在我的表格' demandes'。
class DemandeController extends Controller
{
protected $demandeRepository;
protected $demandeGestion;
protected $compteRepository;
protected $nbrPerPage = 4;
public function search(SearchRequest $request)
{
$mc = $request->input('mc');
$demandes = $this->demandeRepository->search($mc);
$links = $demandes->render();
$etape = $request->input('etape');
return view('demandes.liste', compact('demandes', 'links'))->with('etape',$etape);
}
public function update(DemandeRequest $request, $id)
{
$user = Auth::user();
$inputs = $this->demandeGestion->get_inputs($request,$user);
$demande= $this->demandeRepository->update($id, $inputs);
return redirect('demande')->withOk("Le dossier " . $demande->objet . " a été modifié.");
//return redirect('demandes.liste')->withOk("Le dossier " . $request->input('objet') . " a été enregistrée.");
}
public function store(DemandeRequest $request)
{
$user = Auth::user();
$inputs = array_merge($request->all(), ['user_id' => $user->id]);
$demande = $this->demandeRepository->store($inputs);
return redirect('demande')->withOk("Le dossier " . $demande->objet . " a été créé.");
}
提前致谢:)
答案 0 :(得分:0)
我通常在处理数据时尝试使用Laravel Models。如果你能够,这就是我如何使用模型
Laravel Folder / app / Demande.php
use Illuminate\Database\Eloquent\Model;
class Demande extends Model {
}
Laravel Folder / app / Http / Controllers / DemandeController.php中的示例store
请求
<?php
namespace App\Http\Controllers;
// Models
use App\Demande;
use Auth;
use App\Http\Requests;
use Illuminate\Http\Request;
class DemandeController extends Controller
{
public function store(Request $request)
{
$demande = new Demande;
$demande->cli = $request->cli;
$demande->objet = $request->objet;
$demande->user_id = Auth::id();
$demande->some_other_attribute = $request->some_other_attribute;
$demande->save();
return redirect('demande')->withOk("Le dossier " . $demande->objet . " a été créé.");
}
}
如果这不能满足您的需求,我需要了解有关您的存储库的更多信息。
答案 1 :(得分:0)
非常感谢您的回复。所以,不幸的是它不起作用,因为我还在我的文件DemandeRepository中定义了函数store()。
public function store($inputs)
{
$demande = new $this->demande;
$this->save($demande, $inputs);
return $demande;
}
private function save(Demande $demande, Array $inputs)
{
$demande->objet = $inputs['objet'];
$demande->save();
}
我的模特:
class Demande extends Model
{
protected $fillable = ['objet','etude','user_id','role','mc','num_tf','val_exp','nom_exp','nom_gar'];
public function user()
{
return $this->belongsTo('App\User');
}
}
class User extends Authenticatable
{
use Notifiable;
use AdldapUserModelTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'username', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function demandes(){
return $this->hasMany('App\Demande');
}
}
我的档案DemandeRequest:
class DemandeRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'objet' => 'required|max:80',
];
}
}
public function update(DemandeRequest $request, $id)
{
$user = Auth::user();
$inputs = $this->demandeGestion->get_inputs($request,$user);
$demande= $this->demandeRepository->update($id, $inputs);
return redirect('demande')->withOk("Le dossier " . $demande->objet . " a été modifié.");
//return redirect('demandes.liste')->withOk("Le dossier " . $request->input('objet') . " a été enregistrée.");
}
我的班级DemandeGestion
class DemandeGestion
{
protected $demande;
public function __construct(Demande $demande)
{
$this->demande = $demande;
}
public function get_inputs(DemandeRequest $request, User $user){
if($user->role=='ETUDE'){
$inputs = array_merge($request->all(), ['user_id' => $user->id,'etat' => $user->role]);
}else{
$inputs = array_merge($request->all(), ['user_id' => $user->id]);
}
return $inputs;
}
}