I am working on laravel 5.2.I want to display those members who belongs to that particular group which is open at this time. Actually, i am getting all the members which i have stored in my database but, i only want to access or display only those members who belongs to a particular on which i am currently accessing. I am getting an error: Method groups does not exist. which is shown below:
My controller:
public function members($id){
$dashes=Grouptable::findorFail($id);
$members=Member::all();
return view('members' , ['dashes'=>$dashes,'members'=>$members]);
}
public function dashboard($id){
$dashes=Grouptable::findorFail($id);
return view('dashboard' , ['dashes'=>$dashes]);
}
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member);
return redirect()->back();
}
My view:
<body>
<div class="row">
<div class="col-lg-3 col-lg-offset-1">
<img src="images/ImgResponsive_Placeholder.png"
class="img-circle img- responsive" alt="Placeholder image"> </div>
<div class="col-lg-7">
<h1 style="color:black;">{{ $dashes->name }}</h1></div>
<br />
</div>
<div class="row">
<div class="col-lg-3">
<button class="btn btn-success" onclick="myFunction()">
Add Members + </button>
<div>
<form id="demo" style="display:none;" method="post"
action="{{ route('addmember') }}">
<input class="form-control" type="text" name="addmember">
<button class="btn btn-primary" type="submit">Add</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
<div class="col-lg-7 col-lg-offset-0">
<div class="panel panel-default">
<div id="grp" class="panel-heading">
<h3 id="grouptitle" class="panel-title">Group Members</h3>
</div>
<div id="zx" class="panel-content">
<div class="row">
#foreach($members as $member)
<section class="col-md-6">
<div class="row">
<section class="col-md-offset-1 col-md-3 col-xs-offset-1
col-xs-4">
<img id="imagesize" src="images/g.jpg" class="img-circle"/>
</section>
<section class="col-md-offset-1 col-md-7 col-xs-7">
<section class="col-md-12">
<h5 id="friendname">{{$member->members}}</h5>
</section>
</section>
</div>
</section>
</div>
</section>
#endforeach
</div>
<div id="mn" class="panel-footer"><a id="seemr1"
href="#.html">See More</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
My routes:
Route::get('/members/{id}',[
'uses'=>'GroupController#members',
'as'=>'members'
]);
Route::get('/dashboard/{id}',[
'uses'=>'GroupController#dashboard',
'as'=>'dashboard'
]);
Route::post('/memeber/add',[
'uses'=>'GroupController#addmembers',
'as'=>'addmember'
]);
My modals:
Grouptable:
public function members(){
return $this->hasMany('App\Member');
}
Member:
public function groups(){
return $this->hasMany('App\Grouptable');
}
The way you are defining the relationship is wrong. I think this is OneToMany relationship. Like A Group has many users. Then you must define it like this
MODELS
Grouptable:
public function members(){
return $this->hasMany('App\Member','group_id'); // group_id represents the name of the foreign key. It is not neccessary if your foreign key is Grouptable_id, because Laravel automatically guess it unless the foreign key name is explicitly provided
}
Member:
public function groups(){
return $this->belongsTo('App\Grouptable','group_id');
}
To retrieve all members of a particular group say groupid 1:
$members = \App\Gouptable::find(1)->members;
CONTROLLER
Something seems to be wrong in your addmembers() function:
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member); //what is this groups()?
//You need to do something like this
$groupid = $request['groupid']; // send groupid as parameter from form
Grouptable::find($groupid)->members()->save($member);
return redirect()->back();
}
Related
So, i tried laravel and i got stuck when i want to get URL value as a value for my database. Here is the code in my controller.
public function store(Request $request, $shoe_id)
{
//Insert Data
$cart = new Cart;
$cart->user_id = Auth::user()->id;
$cart->shoe_id = $shoe_id;
$cart->quantity = $request->quan;
$cart->save();
return redirect()->back();
}
and here is the problem i've encountered:
it says that the {shoe_id} is string where i wanted to be an integer from the URL
Here what my website looks like:
My blade
Here is my web.php:
Route::get('/','homeController#main')->name('home');
Route::get('/home','homeController#main')->name('home');
Route::get('/detail/{shoe_id}', 'homeController#detail');
Route::get('/cart/{shoe_id}','CartController#show');
Route::post('/cart/{shoe_id}','CartController#store');
Here is my blade:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div style="text-align:center;">
<img src="{{ asset("$cart->shoe_image") }}" alt="" style=" width:350px; height:350px;">
<div class="card-header">{{$cart->shoe_name}}</div>
<div class="card-header">${{$cart->shoe_price}}</div>
<div class="card-header">{{$cart->shoe_description}}</div>
</div>
<div class="card-header form-group">
<form method="POST" action="/cart/{shoe_id}" enctype="multipart/form-data">
#csrf
<div class="form-row">
<div class="col-md-2">
<label for="quantity">Quantity:</label>
</div>
<div>
<input class = "form-control" type="text" id="quantity" name="quan"><br><br>
</div>
<button class="btn btn-primary" type="submit" name="addcart">Add to Cart</button>
</div>
</form>
{{-- <a class="btn btn-primary" href="#">Add to Cart</a> --}}
</div>
</div>
</div>
</div>
</div>
#endsection
what i want to do: input the inputted data to database. My database has table called cart with attribute of id, user_id, shoe_id.
In your form action you should provide the actual value for the shoe_id then it will work as expected. action="/cart/{{ $cart->shoe_id }}"
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div style="text-align:center;">
<img src="{{ asset("$cart->shoe_image") }}" alt="" style=" width:350px; height:350px;">
<div class="card-header">{{$cart->shoe_name}}</div>
<div class="card-header">${{$cart->shoe_price}}</div>
<div class="card-header">{{$cart->shoe_description}}</div>
</div>
<div class="card-header form-group">
<form method="POST" action="/cart/{{ $cart->shoe_id }}" enctype="multipart/form-data">
#csrf
<div class="form-row">
<div class="col-md-2">
<label for="quantity">Quantity:</label>
</div>
<div>
<input class = "form-control" type="text" id="quantity" name="quan"><br><br>
</div>
<button class="btn btn-primary" type="submit" name="addcart">Add to Cart</button>
</div>
</form>
{{-- <a class="btn btn-primary" href="#">Add to Cart</a> --}}
</div>
</div>
</div>
</div>
</div>
#endsection
I'm building a micro ecommerce in laravel, and I've a problem with detaching an element added in the cart. I'll explain: For example, if I add 4 elements in the cart, when I click "remove element" on the last element I've added in the cart, the last element is not deleted, but vice versa is deleted the first one I've added.
This is my Controller file in laravel:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Computer;
use App\Smartphone;
class FrontController extends Controller
{
public function cart()
{
$computers = Computer::all();
$smartphones = Smartphone::all();
return view('cart',compact('computers', 'smartphones'));
}
public function computers()
{
$computers = Computer::all();
return view('computers',compact('computers'));
}
public function smartphones()
{
$smartphones = Smartphone::all();
return view('smartphones',compact('smartphones'));
}
public function addpc($id)
{
$user = Auth::user();
if(!$user)
return redirect('/login');
$user->computers()->attach($id);
return redirect(route('cart'));
}
public function removepc($id2)
{
$user = Auth::user();
if(!$user)
return redirect('/cart');
$user->computers()->detach($id2);
return redirect(route('cart'));
}
public function addsmartphone($id3)
{
$user = Auth::user();
if(!$user)
return redirect('/login');
$user->smartphones()->attach($id3);
return redirect(route('cart'));
}
public function removesmartphone($id4)
{
$user = Auth::user();
if(!$user)
return redirect('/cart');
$user->smartphones()->detach($id4);
return redirect(route('cart'));
}
}
This is my web.php file:
Route::get('/computers', 'FrontController#computers')->name('computers');
Route::post('/computers{id}/addpc','FrontController#addpc')->name('computers.addpc');
Route::post('/computers{id2}/removepc','FrontController#removepc')->name('computers.removepc');
Route::get('/smartphones', 'FrontController#smartphones')->name('smartphones');
Route::post('/smartphones{id3}/addsmartphone','FrontController#addsmartphone')->name('smartphones.addsmartphone');
Route::post('/smartphones{id4}/removesmartphone','FrontController#removesmartphone')->name('smartphones.removesmartphone');
This is my smartphones.blade.php file:
#extends('layouts.app')
#section('content')
<main class="container">
<section class="row justify-content-center">
#foreach($smartphones as $smartphone)
<div class="col-12 col-md-5 my-5">
<h4 class="card-title text-center">{{$smartphone->name}}</h4>
<h6>{{$smartphone->description}}</h6>
<form action="{{route('smartphones.addsmartphone',['id3'=>$smartphone->id])}}" method="POST">
#csrf
<button class="btn btn-success my-1" type="submit">Add to cart!</button>
</form>
</div>
</div>
#endforeach
</section>
</main>
#endsection
This is my computers.blade.php file:
#extends('layouts.app')
#section('content')
<main class="container">
<section class="row justify-content-center">
#foreach($computers as $computer)
<div class="col-12 col-md-5 my-5">
<h4 class="card-title text-center">{{$computer->name}}</h4>
<h6>{{$computer->description}}</h6>
<form action="{{route('computers.addpc',['id'=>$computer->id])}}" method="POST">
#csrf
<button class="btn btn-success my-1" type="submit">Add to cart!</button>
</form>
</div>
</div>
#endforeach
</section>
</main>
#endsection
And this is my cart.blade.php file:
#extends('layouts.app')
#section('content')
<main class="container">
<section class="row justify-content-center">
#php
$total = 0;
#endphp
#foreach(Auth::user()->computers()->get() as $computer)
<div class="col-12 col-md-4">
<h2 class="text-center">{{$computer->name}}</h2>
<form action="{{route('computers.removepc',['id2'=>$computer->id])}}" method="POST">
#csrf
<center><button class="btn btn-danger mb-5" type="submit">Remove</button></center>
<p class="text-center">Price: {{$computer->price}}</p>
#php
$total+= $computer->price
#endphp
</div>
#endforeach
</section>
</main>
<main class="container">
<section class="row justify-content-center">
#foreach(Auth::user()->smartphones()->get() as $smartphone)
<div class="col-12 col-md-4">
<h2 class="text-center">{{$smartphone->name}}</h2>
<form action="{{route('smartphones.removesmartphone',['id4'=>$smartphone->id])}}" method="POST">
#csrf
<center><button class="btn btn-danger mb-5" type="submit">Remove</button></center>
<p class="text-center">Price: {{$smartphone->price}}</p>
#php
$total+= $smartphone->price
#endphp
</div>
#endforeach
</section>
</main>
<main class="container">
<section class="row justify-content-center">
<h1>Total: {{$total}} €</h1>
</section>
</main>
#endsection
I hope I explained myself, thanks for help!
You need to close your forms , for every category there, inside the loop.
I succeeded the step "create" barring the overview.
My recordings are saved on PhpMyAdmin but not on my page index.blade.php I don't understand why ? I made several refreshments even so.
Here is my file AdminController.php
class AdminController extends Controller
{
public function index(){
/*$devis = Devis::all()->sortByDesc("created_at");
return view('admin.home', compact('devis'));*/
$students = Student::first()->paginate(5);
return view('student.index', compact('students'))
->with('i', (request()->input('page',1)-1)*5);
}
public function create()
{
return view('student.create');
}
public function store(Request $request)
{
$request->validate([
'firstname' => 'required',
'lastname' => 'required'
]);
Student::create($request->all());
return redirect()->route('student.index')
->with('success', 'save');
}
}
In my folder VIEWS with another folder which is Student which has 2 files:
Index.blade.php
#extends('student.template.master')
#section('left_menu')
#include('student.template.left_menu', array('current' => 'home'))
#endsection
#section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>List </h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('student.create') }}">Create</a>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
#foreach($students as $item)
<tr>
<td> {{$item->firstname}}</td>
<td> {{$item->lastname}} </td>
<td> <span class="left ion-close"></span></td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
</div>
</div>
#endsection
And create.blade.php
#extends('student.template.master')
#section('left_menu')
#include('student.template.left_menu', array('current' => 'home'))
#endsection
#section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>Devis</h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<form class="panel-body" action="{{route('student.store')}}" method="POST">
#csrf
<fieldset class="form-group">
<label for="form-group-input-1">Firstname</label>
<input type="text" name="firstname" class="form-control" id="form-group-input-1">
</fieldset>
<fieldset class="form-group">
<label for="form-group-input-1">Lastname</label>
<input type="text" name="lastname" class="form-control" id="form-group-input-1" >
</fieldset>
Back
<button type="submit" class="btn btn-sm btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Do you have an idea please ? I am really stuck.
first() returns an instance of the class Student, you can't paginate on it. change
public function index(){
/*$devis = Devis::all()->sortByDesc("created_at");
return view('admin.home', compact('devis'));*/
$students = Student::first()->paginate(5);
return view('student.index', compact('students'))
->with('i', (request()->input('page',1)-1)*5);
}
to
public function index(){
/*$devis = Devis::all()->sortByDesc("created_at");
return view('admin.home', compact('devis'));*/
$students = Student::paginate(5);
return view('student.index', compact('students'))
->with('i', (request()->input('page',1)-1)*5);
}
In Laravel, when you use paginate(), it also comes with links() function. So all you needed to do was to pass the variable in the controller, as below:
public function index(){
$students = Student::get()->paginate(5);
return view('student.index', compact('students'));
}
Now, in index.blade.php, just reference the links() function:
{{ $students->links() }}
In an html page, I would like to recover the value of an amount and can change the value only in my admin panel.
In the file navbar.blade.php I have 250 which is the amount to edit for the example.
<div class="header-widgets hidden-xs" style="padding:0px;padding-top: 60px;">
<div id="text-3" class="widget widget_text">
<div class="textwidget">
<div class="info-icon">
<img src="/img/time.png">
<span style="font-size: 22px;color: #0d3863;font-weight: bold;"> 250</span>
</div>
</div>
</div>
</div>
I just seek to edit the amount (250) ...
In my Controller named tarrifcontroller I have that.
public function edit(Tariff $tariff)
{
return view('admin.tariffs.edit', compact('tariff'));
}
public function update(Request $request, Tariff $tariff)
{
$tariff->valeur = strip_tags($request->input('amount'));
$tariff->save();
return redirect('/tariffs');
}
In my edit.blade.php I have that
#section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>Tarif {{$tariff->id}} </h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<form class="panel-body" action="/tariff/edit/{{$tariff->id}}" method="POST">
#csrf
<fieldset class="form-group">
<label for="form-group-input-1">Amount</label>
<input type="text" name="amount" class="form-control" id="form-group-input-1" value="{{$tariff->amount}}">
</fieldset>
<button type="submit" class="btn btn-primary pull-right">MAJ</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
My problem is now in navbar.blade.php how should I do that 250 can interact with my edit / update function?
<span style="font-size: 22px;color: #0d3863;font-weight: bold;"> 250</span>
Thank you
I don't have experience with laravel but i guess like any other php framework you can echo variable from controller to view
<span style="font-size: 22px;color: #0d3863;font-weight: bold;"> <?php echo $variable ?></span>
after some googling: You can pass data to the view using the with method.
return View::make('blog')->with('posts', $posts);
I am working on laravel 5.2.I want to display those members who belongs to that particular group which is open at this time. Actually, i am getting all the members which i have stored in my database but, i only want to access or display only those members who belongs to a particular on which i am currently accessing.
I am getting an error: Method groups does not exist. which is shown below:
My controller:
public function members($id){
$dashes=Grouptable::findorFail($id);
$members=Member::all();
return view('members' , ['dashes'=>$dashes,'members'=>$members]);
}
public function dashboard($id){
$dashes=Grouptable::findorFail($id);
return view('dashboard' , ['dashes'=>$dashes]);
}
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member);
return redirect()->back();
}
My view:
<body>
<div class="row">
<div class="col-lg-3 col-lg-offset-1">
<img src="images/ImgResponsive_Placeholder.png"
class="img-circle img- responsive" alt="Placeholder image"> </div>
<div class="col-lg-7">
<h1 style="color:black;">{{ $dashes->name }}</h1></div>
<br />
</div>
<div class="row">
<div class="col-lg-3">
<button class="btn btn-success" onclick="myFunction()">
Add Members + </button>
<div>
<form id="demo" style="display:none;" method="post"
action="{{ route('addmember') }}">
<input class="form-control" type="text" name="addmember">
<button class="btn btn-primary" type="submit">Add</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
<div class="col-lg-7 col-lg-offset-0">
<div class="panel panel-default">
<div id="grp" class="panel-heading">
<h3 id="grouptitle" class="panel-title">Group Members</h3>
</div>
<div id="zx" class="panel-content">
<div class="row">
#foreach($members as $member)
<section class="col-md-6">
<div class="row">
<section class="col-md-offset-1 col-md-3 col-xs-offset-1 col-xs-4">
<img id="imagesize" src="images/g.jpg" class="img-circle"/>
</section>
<section class="col-md-offset-1 col-md-7 col-xs-7">
<section class="col-md-12">
<h5 id="friendname">{{$member->members}}</h5>
</section>
<section class="col-md-12">
<button type="button" class="btn btn-sm btn-
default">Score</button>
</section>
</section>
</div>
<div class="row">
<section class="col-md-offset-9 col-md-3 col-xs-offset-6
col-xs-4">
<div class="btn-group">
<button id="btnclr1" type="button" class="btn btn-block
btn-warning dropdown-toggle" data-toggle="dropdown" aria-
expanded="false"><span class="caret"></span></button>
<ul id="bckdrp" class="dropdown-menu" role="menu">
<li role="presentation"><a id="drpmenu" href="#">Remove</a>
</li>
</ul>
</div>
</section>
</div>
<div class="row">
<section class="col-md-offset-1 col-md-10">
<hr>
</section>
</section>
#endforeach
</div>
<div id="mn" class="panel-footer"><a id="seemr1"
href="#.html">See More</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
My routes:
Route::get('/members/{id}',[
'uses'=>'GroupController#members',
'as'=>'members'
]);
Route::get('/dashboard/{id}',[
'uses'=>'GroupController#dashboard',
'as'=>'dashboard'
]);
Route::post('/memeber/add',[
'uses'=>'GroupController#addmembers',
'as'=>'addmember'
]);
My modals:
Grouptable:
public function members(){
return $this->hasMany('App\Member');
}
Member:
public function groups(){
return $this->belongsTo('App\Grouptable');
}
I do not quite understand the entire problem, but
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member);
return redirect()->back();
}
should look more like
public function addmembers(Request $request){
$member=new Member();
$member->propertyX = $request->get('propertyX');
$member->propertyY = $request->get('propertyY');
$member->groups()->attach($group); // FOR MANY-TO-MANY (N-N) RELATION
$member->groups()->associate($group); // FOR ONE-TO-MANY (1-N) RELATION
$member->save();
return redirect()->back();
}
Depending on your migrations you should choose attach() or associate()