When I click the search button, this shows.
error image
It shows Error Exception for (Cannot use object of type Illuminate\Database\MySqlConnection as array )
<div class='container'>
<div class="row" >
<div class="col-md-12">
<br />
<h3 align="center">Data Pesakit</h3>
<br />
#if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
#endif
<div align="right">
Add
<br />
<br />
</div>
<div class="col-md-12">
<form action="/search" method="get" class="d-none d-md-inline-block form-inline ml-auto mr-0 mr-md-3 my-2 my-md-0">
<div class="form-group">
<input class="form-control" type="search" name="search" />
<span class="form-group-btn">
<button class="btn btn-primary" type="submit">Search</button>
</span>
</div>
</form>
</div>
<table class="table table-bordered">
<tr>
<th>Nama Pertama</th>
<th>Nama Akhir</th>
<th>Kad Pengenalan</th>
<th>No Telefon</th>
<th>Alamat Rumah</th>
<th>Alamat Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
#foreach($pesakit as $row)
<tr>
<td>{{$row['namaPertama']}}</td>
<td>{{$row['namaAkhir']}}</td>
<td>{{$row['kadPengenalan']}}</td>
<td>{{$row['noTelefon']}}</td>
<td>{{$row['alamatRumah']}}</td>
<td>{{$row['email']}}</td>
<td>Edit</td>
<td><form method="post" class="delete_form" action="{{action('PesakitController#destroy',$row['id'])}}">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE" />
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$('.delete_form').on('submit', function(){
if(confirm("Are you sure you want to delete it?"))
{
return true;
}
else
{
return false;
}
});
});
</script>
#endsection
This is code for views
class PesakitController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$pesakit = Pesakit::all()->toArray();
return view('pesakit.index', compact('pesakit'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('pesakit.create');
}
public function search(Request $request)
{
$search = $request->get('search');
$pesakit = DB::table('pesakit')
->where('namaPertama', 'like', '%'.$search. '%');
return view('pesakit.index', ['pesakit'=>$pesakit]);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'namaPertama' => 'required',
'namaAkhir' => 'required',
'kadPengenalan' => 'required',
'noTelefon' => 'required',
'alamatRumah' => 'required',
'email' => 'required'
]);
$pesakit = new Pesakit([
'namaPertama' => $request->get('namaPertama'),
'namaAkhir' => $request->get('namaAkhir'),
'kadPengenalan' => $request->get('kadPengenalan'),
'noTelefon' => $request->get('noTelefon'),
'alamatRumah' => $request->get('alamatRumah'),
'email' => $request->get('email')
]);
$pesakit->save();
return redirect()->route('pesakit.create')->with('success',
'Data Berjaya Disimpan');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$pesakit = Pesakit::find($id);
return view('pesakit.edit', compact('pesakit', 'id'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'noTelefon' => 'required',
'alamatRumah' => 'required'
]);
$pesakit = Pesakit::find($id);
$pesakit->noTelefon = $request->get('noTelefon');
$pesakit->alamatRumah = $request->get('alamatRumah');
$pesakit->save();
return redirect()->route('pesakit.index')->with('success', 'Data Dikemas kini');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$pesakit = Pesakit::find($id);
$pesakit->delete();
return redirect()->route('pesakit.index')->with('success', 'Data Deleted');
}
}
This is the controller
Route::get('/', function () {
return view('utama');
});
Route::resource('pesakit', 'PesakitController');
Route::resource('petugas', 'PetugasController');
Route::resource('pendaftaran', 'PendaftaranController');
Route::get('/search','PesakitController#search');
This is the Route
I try to look for simple error such as ; or misspell or something but i cannot find it. It says some array problem but does not all crud is array??
add get()
public function search(Request $request)
{
$search = $request->get('search');
$pesakit = DB::table('pesakit')
->where('namaPertama', 'like', '%'.$search. '%')->get();
return view('pesakit.index', ['pesakit'=>$pesakit]);
}
Related
I stored the User id in Stock table from session user with each row of data. Now i try to retrive the data for particlar user that's loggin at the time. Only which data show in table which row match the user_id with auth user_id.
THis is my index page
#extends('layouts.app')
#section('content')
#if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
#endif
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4 class="card-title"> Stock
<div align="right">
Add
</div></h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead class=" text-primary">
<th>Product Name</th>
<th>Product code</th>
<th>deatils</th>
<th>Price</th>
<th>Cost</th>
<th>Quantity</th>
<th></th>
</thead>
<tbody>
#foreach($stocks as $row Auth::user()->user_id == user_if)
<tr>
<td>{{$row['product_name']}}</td>
<td>{{$row['product_code']}}</td>
<td>{{$row['details']}}</td>
<td>{{$row['price']}}</td>
<td>{{$row['cost']}}</td>
<td>{{$row['quntity']}}</td>
<td ></i>
</td>
<td>
<form id="my_form" method="post" class="delete_form" action="{{action('StockController#destroy', $row['id'])}}">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE" />
<i class="fa fa-trash"></i>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
THis is my controller file which i try to display data of particular user which is loggin at the time
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Stock;
use Auth;
class StockController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$stocks = Stock::all()->toArray();
return view('stock.index', compact('stocks'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('stock.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'user_id' =>'required',
'product_name' => 'required',
'product_code' => 'required',
'details' => 'required',
'price' => 'required',
'cost' => 'required',
'quntity' => 'required'
]);
$stock = new Stock([
'user_id' => $request->get('user_id'),
'product_name' => $request->get('product_name'),
'product_code' => $request->get('product_code'),
'details' => $request->get('details'),
'price' => $request->get('price'),
'cost' => $request->get('cost'),
'quntity' => $request->get('quntity')
]);
$stock->save();
return redirect()->route('stock.index')->with('success', 'Data Added');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$stock = Stock::find($id);
return view('stock.edit', compact('stock', 'id'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'product_name' => 'required',
'product_code' => 'required',
'details' => 'required',
'price' => 'required',
'cost' => 'required',
'quntity' => 'required'
]);
$stock = Stock::find($id);
$stock->product_name = $request->get('product_name');
$stock->product_code = $request->get('product_code');
$stock->details = $request->get('details');
$stock->price = $request->get('price');
$stock->cost = $request->get('cost');
$stock->quntity = $request->get('quntity');
$stock->save();
return redirect()->route('stock.index')->with('success', 'Data Updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$stock = Stock::find($id);
$stock->delete();
return redirect()->route('stock.index')->with('success', 'Data Deleted');
}
}
there are 2 ways. first is like #Keepon said: create a relationship between the user and the stock table
2:
$user = Auth::user();
$stocks = Stock::where('user_id','=',$user->id)->get();
Please change you index function
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$stocks = Stock::where('user_id', Auth::id())->get();
return view('stock.index', compact('stocks'));
}
And than in your blade
#foreach($stocks as $stock)
<tr>
<td>{{$stock->product_name}}</td>
<td>{{$stock->product_code}}</td>
<td>{{$stock->details}}</td>
<td>{{$stock->price}}</td>
<td>{{$stock->cost}}</td>
<td>{{$stock->quntity}}</td>
<td ></i>
</td>
<td>
<form id="my_form" method="post" class="delete_form" action="{{action('StockController#destroy', $stock->id)}}">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE" />
<i class="fa fa-trash"></i>
</form>
</td>
</tr>
#endforeach
The best practice is to make a MODEL RELATIONSHIP.
To make a user and stocks relationship you need to determine what relationship would you build.
In this case I will build a relationship base on what I under stand a One To Many relationship
DATABASE
Stocks Migration
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
...
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
It should be look like the above code.
Then in your model you can connect the relationship of user and stocks
MODEL
User Model
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function stocks() {
return $this->hasMany(Stock::class);
}
}
Stock Model
class Stock extends Model
{
protected $guarded = [];
public function user() {
return $this->belongsTo(User::class);
}
}
Then you can query on your controller the user stocks.
CONTROLLER
Stocks Controller
public function index() {
$stocks = auth()->user()->stocks;
return view('index', compact('stocks'));
}
If you want more example of eloquent relationship you can check this tutorial by Victor
add this function in User Model
public function stocks() {
return $this->hasMany(Stock::class);
}
in Stocks Controller
public function index()
{
$stocks = auth()->user()->stocks;
return view('stock.index', compact('stocks'));
}
in index.blade.php
#foreach($stocks as $row)
<tr>
<td>{{$row['product_name']}}</td>
<td>{{$row['product_code']}}</td>
<td>{{$row['details']}}</td>
<td>{{$row['price']}}</td>
<td>{{$row['cost']}}</td>
<td>{{$row['quntity']}}</td>
<td ></i>
</td>
<td>
<form id="my_form" method="post" class="delete_form" action="{{action('StockController#destroy', $row['id'])}}">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE" />
<i class="fa fa-trash"></i>
</form>
</td>
#endforeach
I have a previous form in which i created a user and technician. the data is well filled in the tables of the database. Now the form redirects me to another form in which the technician id is needed. I'm working to make the technician id in the new form auto filled from the previous form. I hope my question is clear and I which you could help me. I put my code and the form I want to be auto-filled.Hope you could help me.
Model1
public function tarificationtache()
{
return $this->belongsToMany('App\tarificationtache','technicien_tarificationtache','technicien_id','tarificationtache_id');
}
Model2
public function techniciens()
{
return $this->belongsToMany('App\technicien','technicien_tarificationtache','tarificationtache_id','technicien_id');
}
technicien.Controller
public function store(Request $request)
{
$user = new user();
$user->nom = $request->input('nom');
$user->prenom = $request->input('prenom');
$user->tel = $request->input('tel');
$user->mobil = $request->input('mobil');
$user->role = $request->input('role');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->save();
$technicien = new technicien();
$technicien->user_id = $user->id;
$technicien->actif = $request->input('actif');
$technicien->moyenne_avis = $request->input('moyenne_avis');
$technicien->save();
$technicien->zoneintervention()->attach($request->zoneintervention_id);
$technicien->metier()->attach($request->metier_id);
return redirect('tarification/create');
}
tarification.controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\tarificationtache;
use App\technicien;
use App\tache;
class TarificationController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$Listtache=tache::orderBy('libelle_tache')->get();
$Listtarification=tarificationtache::all();
return view('tarification.index',
['tarification'=>$Listtarification]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$techniciens = technicien::orderBy('id','desc')->get();
$taches = Tache::orderBy('id', 'desc')->get();
return view('tarification.create')->with('taches', $taches)->with('techniciens', $techniciens);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$tarification = new tarificationtache();
$tarification ->tache_id = $request->input('tache_id');
$tarification ->Tarif =$request->input('Tarif');
$tarification->save();
$tarification->techniciens()->attach($request->technicien_id);
return redirect('tarification');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$tache=Tache::find($id);
return view('tache.edit',['libelle_tache'=>$tache],
['Tarif'=>$tache],['metier_id'=>$tache]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$tache =Tache::find($id);
$tache->delete();
return redirect('tache');
}
}
View
#extends('Layouts/app')
#section('content')
#if(count($errors))
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors ->all() as $message)
<li>{{$message}}</li>
#endforeach
</ul>
</div>
#endif
<div class="container">
<div class="row"></div>
<div class="col-md-12">
<form action=" {{url ('tarification') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="technicien">Technicien</label>
<select name="technicien_id" id="technicien" class="form-control" >
#foreach($techniciens as $techniciens)
<option value="{{ $techniciens->id }}">
{{$techniciens->id}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="Tache">Libelle Tache</label>
<select name="tache_id" id="Tache" class="form-control">
#foreach($taches as $tache)
<option value="{{ $tache->id }}">
{{$tache->libelle_tache}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Tarif</label>
<input type="text" name ="Tarif" class="form-control"value="{{old('tarif')}}">
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
<script>
#endsection
technicien.controller
public function store(Request $request)
{
$user = new user();
$user->nom = $request->input('nom');
$user->prenom = $request->input('prenom');
$user->tel = $request->input('tel');
$user->mobil = $request->input('mobil');
$user->role = $request->input('role');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->save();
$technicien = new technicien();
$technicien->user_id = $user->id;
$technicien->actif = $request->input('actif');
$technicien->moyenne_avis = $request->input('moyenne_avis');
$technicien->save();
$technicien->zoneintervention()->attach($request->zoneintervention_id);
$technicien_id = $technicien->id;
return redirect('tarification/create')->with(‘technicien_id’, $technicien_id);
}
tarification.view
<div class="form-group">
<label for="technicien">Technicien</label>
<select name="technicien_id" id="technicien"
class="form-control" >
#foreach($techniciens as $techniciens)
<option value="{{ $techniciens->id }}">
{{ $technicien_id }}
</option>
#endforeach
</select>
</div>
here is my new code i did exactly as you told me
You have to pass that value to the view.
$technician_id = $user->id;
return view(‘tarification/create’)->with(‘technician_id’, $technician_id);
Now you can use this value in your view.
<div class="form-group">
<label for="technicien">Technicien</label>
<input type="text" value={{$technician_id}} >
</div>
I would like to make a multiple selection through the combobox and insert them into the database. the idea is to make an array and then make his appeal.
with what I've made, a single choice adds to the base. Would anyone have any idea how to insert all zones selected into my database.Thanks
Model 1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class zoneintervention extends Model
{
public function techniciens()
{
return $this-
belongsToMany('App\technicien','technicien_zone','zoneintervention_id','technicien_id');
}
}
Model 2
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class technicien extends Model
{
public function zoneintervention()
{
return $this-
belongsToMany('App\zoneintervention','technicien_zone','technicien_id','z
oneintervention_id');
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
Controler
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\technicien;
use App\zoneintervention;
class TechnicienController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$Listzone=zoneintervention::orderBy('code_postal')->get();
$Listtechnicien=technicien::all();
return view('technicien.index',['technicien'=>$Listtechnicien]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$zoneintervention = Zoneintervention::orderBy('id', 'desc')-
>get();
return view('technicien.create')->with('zoneintervention',
$zoneintervention);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$technicien = new technicien();
$technicien ->actif =$request->input('actif');
$technicien ->moyenne_avis =$request->input('moyenne_avis');
$technicien = new technicien();
$technicien->actif = $request->input('actif');
$technicien->moyenne_avis = $request->input('moyenne_avis');
$technicien->save();
$technicien->zoneintervention()->attach($request-
>zoneintervention_id);
return redirect('technicien');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
create
#extends('Layouts/app')
#section('content')
#if(count($errors))
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors ->all() as $message)
<li>{{$message}}</li>
#endforeach
</ul>
</div>
#endif
<div class="container">
<div class="row"></div>
<div class="col-md-12">
<form action=" {{url ('technicien') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="zoneintervention">zoneintervention</label>
<select multiple name="zoneintervention_id"
id="zoneintervention" class="form-control" >
#foreach($zoneintervention as
$zoneintervention)
<option value="{{ $zoneintervention->id }}">
{{$zoneintervention->code_postal}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Moyenne Avis</label>
<input type="text" name ="moyenne_avis" class="form-
control"value="{{old('moyenne_avis')}}">
</div>
<div class="form-group">
<label for="">Etat</label>
<input type="text" name ="actif" class="form-
control"value="{{old('actif')}}">
</div>
<div class="form-group">
<input type="submit" value = "enregistrer"
class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
In your blade file you can do:
...
<select multiple name="zoneintervention_id[]" id="zoneintervention" class="form-control" >
...
In your controller,
public function store(Request $request)
{
//This will give you an array of all the selected item
dd($request['zoneintervention_id']);
}
I have two tables, a table "métier" and a table "tâche" with one to many connection, a "métier" has several "tâche". In my form "ajouter tâches" I have a combobox from which I choose the "métier" associated with the "tâche".
now i just want to fill the combo from the table metier with libelle_metier an then add the id of libelle_metier that i chosen to the table tache and then add it to metier_id.Any help ?
Model metier
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class metier extends Model
{
public function metier()
{
return $this->hasMany(Tache::class);
}
}
Model metier
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tache extends Model
{
public function metier()
{
return $this->belongsTo(Metier::class);
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
tache.blade.php
#extends('Layouts/app')
#section('content')
#if(count($errors))
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors ->all() as $message)
<li>{{$message}}</li>
#endforeach
</ul>
</div>
#endif
<div class="container">
<div class="row"></div>
<div class="col-md-12">
<form action=" {{url ('tache') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="">Libelle Tache</label>
<input type="text" name ="libelle_tache"
class="form-control"value="{{old('libelle_tache')}}">
</div>
<div class="form-group">
<label for="">Tarif</label>
<input type="text" name ="Tarif" class="form-
control"value="{{old('tarif')}}">
</div>
<div class="form-group">
<label for="metier">metier</label>
<select name="metier_id" id="metier" class="form-
control">
#foreach($metiers as $metier)
<option value="{{$metier}}">
{{$metier->libelle_metier}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<input type="submit" value = "enregistrer"
class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
<script>
#endsection
tache controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tache;
use App\Metier;
class TacheController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$Listmetier=metier::orderBy('libelle_metier')->get();
$Listtache=tache::all();
return view('tache.index',['tache'=>$Listtache]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//$metiers = Costcenter::lists('libelle_metier', 'id');
$metiers = Metier::orderBy('id', 'desc')->get();
return view('tache.create')->with('metiers', $metiers);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$tache = new Tache();
$tache ->libelle_tache =$request->input('libelle_tache');
$tache ->Tarif =$request->input('Tarif');
$tache ->metier_id = $request->input('metier_id');
$tache->save();
return redirect('tache');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$tache=Tache::find($id);
return view('tache.edit',['libelle_tache'=>$metier],
['Tarif'=>$tache]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$tache =Tache::find($id);
$tache->delete();
return redirect('tache');
}
}
If I understood you correctly you want to pass metier id where you loop over metiers you need to change
tache.blade.php
...
<option value="{{ $metier->id }}">
...
Hello guys i have 3 eloquent tables members, pledge and campaign and they are have a relationship amongst each other namely:
for member model
protected $table = "members";
public function loans()
{
return $this->hasMany(Loan::class, 'borrower_id', 'id');
}
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function pledges()
{
return $this->hasMany(Pledge::class, 'member_id', 'id');
}
for campaign
protected $table = "campaigns";
public function pledges()
{
return $this->hasMany(Pledge::class, 'campaign_id', 'id')->orderBy('created_at', 'desc');
}
}
then for pledge model
protected $table = "pledges";
public function campaign()
{
return $this->hasOne(Campaign::class, 'id', 'campaign_id');
}
public function member()
{
return $this->hasOne(Member::class, 'id', 'member_id');
}
}
IN the plegde.create.blade
<div class="control-group">
<label class="control-label">Member</label>
<div class="controls">
<select>
#foreach ($member as $members)
<option>{{ $members->first_name }}</option>
#endforeach
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Campaign</label>
<div class="controls">
<select>
#foreach ($campaign as $campaigns)
<option>{{ $campaigns->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Amount</label>
<div class="controls">
<input type="number" name="amount" id="required" value="{{ old('amount') }}">
#if ($errors->has('amount'))
<p class="alert alert-danger">{{ $errors->first('amount') }}
#endif
</div>
</div>
<div class="control-group">
<label class="control-label">Date (mm-dd)</label>
<div class="controls">
<div data-date="2012-11-02" class="input-append date datepicker">
<input type="text" name="date" value="2012-11-02" data-date-format="yyyy-mm-dd" class="span11" value="{{ old('date') }}">
#if ($errors->has('date'))
<p class="alert alert-danger">{{ $errors->first('date') }}
#endif
<span class="add-on"><i class="icon-th"></i></span> </div>
</div>
</div>
<div class="control-group">
<label class="control-label">Description</label>
<div class="controls">
<input type="text" name="notes" id="required" value="{{ old('notes') }}">
#if ($errors->has('notes'))
<p class="alert alert-danger">{{ $errors->first('notes') }}
#endif
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn btn-success">
</div>
</form>
then in my pledge.index.blade.php i have this but it is not showing proerly this is the code
<div class="widget-content nopadding">
<table class="table table-bordered data-table">
<thead>
<tr>
<th>#ID</th>
<th>Campaign</th>
<th>Member</th>
<th>Amount</th>
<th>Date</th>
<th>Notes</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
#foreach ($member as $members)
<td>{{ $members->first_name }}</td>
#endforeach
#foreach ($campaign as $campaigns)
<td>{{ $campaigns->name }}</td>
#endforeach
#foreach ($pledge as $pledges)
<td>{{ $pledges->amount }}</td>
#endforeach
#foreach ($pledge as $pledges)
<td>{{ $pledges->date }}</td>
#endforeach
</tr>
</tbody>
</table>
</div>
Any help would be appreciated.
These are the controllers
Campaign Controller
use App\Campaign;
use Illuminate\Http\Request;
class CampaignController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$campaign = Campaign::all();
return view('campaign.index', compact('campaign'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('campaign.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name'=>'required',
'goal'=> 'required',
]);
// return $request->all();
$campaign = new Campaign;
$campaign->name = $request->name;
$campaign->goal = $request->goal;
$campaign->notes= $request->notes;
$campaign->save();
return redirect (route('campaign.index'));
}
PLedge Controller
use App\Campaign;
use App\Member;
use App\Pledge;
use Illuminate\Http\Request;
class Pledgecontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$member= Member::all();
$campaign= Campaign::all();
$pledge= Pledge::all();
return view ('pledges.index', compact ('member', 'campaign', 'pledge'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$member= Member::all();
$campaign= Campaign::all();
$pledge= Pledge::all();
return view ('pledges.create', compact ('member', 'campaign', 'pledge'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$pledge= new Pledge;
$pledge->member_id = $request->member_id;
$pledge->campaign_id = $request->campaign_id;
$pledge->amount = $request->amount;
$pledge->date = $request->date;
$pledge->notes = $request->notes;
$pledge->save();
return redirect (route('pledge.index'));
// return $request->all();
}
Member Controller
use App\Member;
use Illuminate\Http\Request;
class MemberController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$member = Member::all();
return view('member.index', compact('member'));
// return view ('member.data');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view ('member.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'first_name'=> 'required',
'middle_name'=>'required',
'last_name'=>'required',
'gender'=> 'required',
'marital_status'=> 'required',
'status'=> 'required',
'mobile_phone'=> 'required',
'address'=> 'required',
'email'=> 'required',
'dob'=> 'required',
'photo'=> 'required | image|mimes:jpeg,png,jpg,gif,svg|max:700',
]);
if ($request->hasFile('photo')) {
// $request->photo->store('public');
$imageName= $request->photo->store('public');
}
$member = new Member;
$member->first_name = $request->first_name;
$member->middle_name = $request->middle_name;
// $member->photo = $request->photo;
$member->photo = $imageName;
$member->last_name = $request->last_name;
$member->gender = $request->gender;
$member->marital_status = $request->marital_status;
$member->status = $request->status;
$member->mobile_phone = $request->mobile_phone;
$member->address = $request->address;
$member->email = $request->email;
$member->dob = $request->dob;
$member->save();
return redirect (route('member.index'));
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$member = Member::where('id', $id)->get();
return view ('member.edit', compact('member'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'first_name'=> 'required',
'middle_name'=>'required',
'last_name'=>'required',
'gender'=> 'required',
'marital_status'=> 'required',
'status'=> 'required',
'mobile_phone'=> 'required',
'address'=> 'required',
'email'=> 'required',
'dob'=> 'required',
'photo'=> 'required | image|mimes:jpeg,png,jpg,gif,svg|max:700',
]);
if ($request->hasFile('photo')) {
// $request->photo->store('public');
$imageName= $request->photo->store('public');
}
$member = Member::find($id);
$member->first_name = $request->first_name;
$member->middle_name = $request->middle_name;
// $member->photo = $request->photo;
$member->photo = $imageName;
$member->last_name = $request->last_name;
$member->gender = $request->gender;
$member->marital_status = $request->marital_status;
$member->status = $request->status;
$member->mobile_phone = $request->mobile_phone;
$member->address = $request->address;
$member->email = $request->email;
$member->dob = $request->dob;
$member->save();
return redirect (route('member.index'));
// return $request->all();
}
This is not a good solution. Same table, different models, different foreachs.
You can make just one foreach and function. Like this:
public function memberFunction()
{
return $this->hasOne('App\Member');
}
//
public function campaignFunction()
{
return $this->hasOne('App\Campaign');
}
We made the connection as a function. Now inside the foreach use the function with [' '] tags.
#foreach ($example as $examples)
<td>{{ $examples->memberFunction['firstname'] }}</td>
<td> {{ $examples->campaignFunction['name'] }} </td>
#endforeach
you can continue like this.