How to add model variable to another model in Laravel - php

My problem is this I have $collaborator (model) variable in my view. In the view there is form related with $permission model. Now I need add $permission to My view file. How can I do it my blade view is this
I need print status value of the permission table on this blade file. That I need get $permission here.
My blade file is
#if($collaborators) // I need add $permission here
#foreach( $collaborators as $collaborator)
<div>
<div>
<span>
<img src="{{ $collaborator->user()->first()->getAvatarUrl() }}" />
{{ $collaborator->user()->first()->username}}
{{ $collaborator->user()->first()->id}}
</span>
</div>
<button class="btn btn-sm btn-danger delete" style="margin-top:5px;padding:4px;width:35px;"
data-action="/projects/{{ $project->id }}/collaborators/{{ $collaborator->collaborator_id }}"
data-token="{{csrf_token()}}">
<i class="fa fa-trash-o"></i>
</button>
<!-- permission start -->
<form class="form-vertical" role="form" method="post" action="{{ route('projects.collaborators.permission', $project->id,$collaborator->id) }}">
<!--<div id="cid" name="cid">{{ $collaborator->user()->first()->id}}</div>-->
<input type="hidden" id="cid" name="cid" value="{{ $collaborator->user()->first()->id }}" />
<div class="form-group{{ $errors->has('status') ? ' has-error' : '' }}">
<label for="status" class="control-label">Choose Permission</label>
<select name="status" id="status">
<option value="">Choose a status</option>
<option value="3">View Only</option>
<option value="2">Edit Tasks</option>
<option value="1">Admin</option>
</select>
#if ($errors->has('status'))
<span class="help-block">{{ $errors->first('status') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Create</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token()

Related

I have a error to pass my datta from blade file to Controller

This is my code in blade file :
<form action="{{ route('user.delete') }}" method="Delete" class="row g-3">
#csrf
<div class="col-auto">
<select class="form-select" aria-label="Default select example">
<option selected>Select User</option>
#foreach($users as $user)
<option value="1">{{$user->user_login}}</option>
#endforeach
</select>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-success btn-lg mb-1">Delete User</button>
</div>
</form>
And this is my fuction in Controller file :
public function delete(Request $request)
{
$username = $request->get('user_login');
$user = get_user_by( 'user_login', $username );
wp_delete_user( $user->ID );
return view('wp.user');
}
How to take a value of Selected User in blade file to pass in my Function?
<form action="{{ route('user.delete') }}" method="POST" class="row g-3">
#csrf
#method('DELETE')
<div class="col-auto">
<select class="form-select" aria-label="Default select example" name="user">
<option selected>Select User</option>
#foreach($users as $user)
<option value="1">{{$user->user_login}}</option>
#endforeach
</select>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-success btn-lg mb-1">Delete User</button>
</div>
</form>
You are missing the name attribute name="user", without name attribute it can't send the value on your case.

add Product size to cart (laravel shoppingcart)

I have followed a tutorial to create a laravel shopping cart https://www.youtube.com/watch?v=Jzi6aLKVw-A&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR&index=3 and composer https://github.com/hardevine/LaravelShoppingcart
I want make button select size product
CartController.php
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
Cart::add($request->id, $request->name, 1, $request->price, $request->data)->associate('App\Product');
return redirect()->route('cart.index')->with('success_message', 'Item was added to your cart!');
}
Product.blade.php
<h1 class="topic">{{ $product->name}}</h1>
<h4>{{ $product->price }}</h4>
<div class="size-b">
<div class="title">
<h5>Trousers</h5>
</div>
<form action="{{ route('cart.store') }}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $product->id }}" >
<input type="hidden" name="name" value="{{ $product->name }}" >
<input type="hidden" name="price" value="{{ $product->price }}" >
<div class="btn-group" role="group" aria-label="Basic">
<button> <input type="checkbox" name="data" value="S" class="btn btn-secondary"><span>S</span></button>
<button><input type="checkbox" name="data" value="M" class="btn btn-secondary">M</button>
<button><input type="checkbox" name="data" value="L" class="btn btn-secondary">L</button>
<button> <input type="checkbox" name="data" value="XL" class="btn btn-secondary">XL</button>
</div>
<button type="submit" class="btn btn-secondary">ซื้อสินค้า</button>
</form>
cart.blade.php
#if (Cart::count() >0 )
<h4>{{ Cart::count() }}Item(s) in cart</h4>
<div class="table-cart-destop">
#foreach (Cart::content() as $item)
<div class="row">
<div class="col-lg">
<div class="remove">
<form action="{{ route('cart.destroy', $item->rowId) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-secondary">X</button>
</form>
</div>
</div>
<div class="col-lg-2">
<div class="image-border">
<img src="{{ $item->model->photo1}}" class="img-cart" alt="product-cart">
</div>
</div>
<div class="col-lg-5">
<div class="content">
<h4>{{ $item->model->name}}</h4>
<p>{{ $item->model->details}}</p>
</div>
</div>
<div class="col-lg">
<p class="size">size {{ $item->model->data }} </p>
</div>
<div class="col-lg">
<div class="quality">
<form action="">
<div class="form-row align-items-center">
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
<option selected>1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="3">5</option>
</select>
</div>
</div>
</form>
</div>
</div>
<div class="col-lg">
<p class="size">{{ $item->data}}฿</p>
</div>
</div>
#endforeach
<div class="tatal">
<p>ยอดชำระเงินทั้งหมด</p>
<p class="price-tatal">{{ Cart::subtotal() }}฿</p>
</div>
<div class="buy-now">
<a href="{{ route('checkout.index') }}">
<button type="submit" class=" btn btn-secondary">สั่งซื้อสินค้า</button>
</a>
</div>
I try dd($request->all());
run php artisan serve --> size product (don't show)

Php post Function call after every x minutes

i have a function which sync api, i have to manually trigger the function from the front end by passing some parameters .
Can someone help me with script or a way to automatically sync the function after some specific time interval.
Code for frontend is below
<form
role="form"
method="POST"
action="{{ url('/admin/apisync/show') }}">
{{ csrf_field() }}
#if( ! $apis->isEmpty() )
<fieldset class="scheduler-border">
<div class="form-group{{ $errors->has('profit_percentage') ? ' has-
error' : '' }}">
<input type="text"
class="form-control"
data-validation="number"
placeholder="Profit Percentage"
data-validation-allowing="float"
id="profit_percentage"
value="{{ old('profit_percentage') }}"
name="profit_percentage">
#if ($errors->has('profit_percentage'))
<span class="help-block">
<strong>{{ $errors->first('profit_percentage') }}
</strong>
</span>
#endif
</div>
<div class="form-group">
<label for="sync_action" class="control-label">Sync Actions</label>
<label class="checkbox-inline">
<input type="checkbox"
style="margin: 0; margin-left: -20px"
name="sync_action[]"
checked
value="remove"> Remove
</label> <span class="fas fa-info-circle" data-
toggle="popover" data-trigger="hover" data-content="This will remove packages which exist
at your panel but have been removed at host"></span>
</div>
<div class="form-group" style="float:right">
<button type="submit" id="btn-proceed" class="btn btn-primary">Sync
Packages</button>
</div>
</fieldset>
#else
<span class="help-block">
<strong>No API is configured. Please update your API
configurations.</strong>
</span>
#endif
</form>

Passing dates from form into blade view

I have a shopping cart that has a pick-up and return date option that needs to be selected. I cannot get either of them to be echoed into the cart view when they are selected. I am using the Laravel Shopping Cart library to build the cart, which has an array for the extra options. I have passed the values into there to pass into the view, but it doesn't seem to work.
Here is the form mark:
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">Pick Up Date</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1of2" style="float: left;">
<label class="label" for="location" style="float:left;">Pick Up Location</label>
<select name="location" id="location" class="location">
<option>please choose</option>
<option value="bakersfield">Bakersfield</option>
<option value="chico">Chico</option>
<option value="fresno">Fresno</option>
<option value="hayward">Hayward</option>
<option value="manteca">Manteca</option>
<option value="oakley">Oakley</option>
<option value="redwood_city">Redwood City</option>
<option value="sacramento">Sacramento</option>
<option value="salinas">Salinas</option>
<option value="san_jose">San Jose</option>
<option value="san_jose_fusion">San Jose Fusion</option>
<option value="santa_rosa">Santa Rosa</option>
</select>
</div>
</div>
</fieldset>
<input type="hidden" name="id" value="{{ $rental->id }}">
<input type="hidden" name="title" value="{{ $rental->title }}">
<input type="hidden" name="pickupDate" value="{{ $rental->pickupDate }}">
<input type="hidden" name="returnDate" value="returnDate">
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Add to Cart</button>
</div>
</div>
</form>
Here is the cart view:
<article>
#if(session()->has('success_message'))
<div class="alert alert-success">
{{ session()->get('success_message') }}
</div>
#endif
<h1>Shopping Cart</h1>
#if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Cart::count() > 0)
<h2>{{ Cart::count() }} item(s) in Shopping Cart</h2>
<div>
<div>
#foreach (Cart::content() as $item)
<fieldset>
<article class="js-cart-product">
<p class="prod-title">Name: {{$item->model->name}} </p>
<p class="pu-date">Pick up date: {{ ($item->options->has('pickupDate') ? $row->options->pickup : '') }} </p>
<p class="rtn-date">Return Date: {{ ($item->options->has('returnDate') ? $row->options->pickup : '') }}</p>
<p class="loc">Location: {{$item->location}}</p>
<form action="{{ route('cart.destroy', $item->rowId)}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Delete Item</button>
</div>
</div>
<!-- <div class="cart__footer">
<p class="cart__text">
<a class="button" href="#" title="Buy products">
Check Out
</a>
</p>
</div> -->
</form>
</article>
</fieldset>
#endforeach
</div>
</div>
#else
<h3>No items in Cart!</h3>
Return to Rental Equipment
#endif
</div>
</article>
And here is the create item part of the controller:
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
$this->validate($request, array(
'location'=>'required',
));
Cart::add($request->id, $request->title, 1, $request->location, $options = ['pickup' => 'pickupDate', 'returnDate' => 'returnDate'])
->associate('App\Rental');
Session::flash('success', 'The item was successfully save!');
return redirect()->route('cart.index');
}
From your form blade
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">
Pick Up Date
</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
then in the bottom somewhere of your master/app blade you put
<script>
$(function() {
$("#pickupDate" ).datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
<script type="text/javascript">
$(function() {
$("#returnDate").datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
be sure to include these necessary files jquery.min.js, and if you use bootstrap then you need bootstrap.min.js, bootstrap-datepicker.min.js
Please let me know if it works

How to display my Laravel blade file items according to if-else conditions?

I need to display some different items according to an authenticated user id in Laravel blade file. This is the first condition that I need to display:
#foreach ($task->comments as $comment)
#if(auth()->user()->id == $task->user_id)
<div>
<div><i class="fa fa-check-square-o"></i>
<span>{{ $comment->comments }} by
<span style="font-style: italic;color: #09f;">
{{ ($comment->user()->first()->username === auth()->user()->username) ? 'You' : $comment->user()->first()->username }}
</span>
</span></div>
Edit
<button class="btn btn-danger delete pull-right"
data-action="/projects/{{ $task->project->id }}/comments/{{ $comment->id }}"
data-token="{{csrf_token()}}">
<i class="fa fa-trash-o"></i>Delete
</button>
</div>
<hr/>
#endif
#endforeach
<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', ['projectId'=> $project->id, 'taskId'=>$task->id])}}">
<div class="form-group{{ $errors->has('comments') ? ' has-error' : '' }}">
<textarea name="comments" class="form-control" style="width:80%;" id="comment" rows="5" cols="5"></textarea>
#if ($errors->has('comments'))
<span class="help-block">{{ $errors->first('comments') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Comment</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
and if the condition above doesn't meet, I need to display following conditions:
#else
<div><i class="fa fa-check-square-o"></i>
<span>{{ $comment->comments }} by
<span style="font-style: italic;color: #09f;">
{{ ($comment->user()->first()->username === auth()->user()->username) ? 'You' : $comment->user()->first()->username }}
</span>
</span></div>
<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', ['projectId'=> $project->id, 'taskId'=>$task->id])}}">
<div class="form-group{{ $errors->has('comments') ? ' has-error' : '' }}">
<textarea name="comments" class="form-control" style="width:80%;" id="comment" rows="5" cols="5"></textarea>
#if ($errors->has('comments'))
<span class="help-block">{{ $errors->first('comments') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Comment</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
How can I do this?
Try this i have edited your code
#foreach ($task->comments as $comment)
#if(auth()->user()->id == $task->user_id)
<div>
<div><i class="fa fa-check-square-o"></i>
<span>{{ $comment->comments }} by
<span style="font-style: italic;color: #09f;">
{{ ($comment->user()->first()->username === auth()->user()->username) ? 'You' : $comment->user()->first()->username }}
</span>
</span></div>
Edit
<button class="btn btn-danger delete pull-right"
data-action="/projects/{{ $task->project->id }}/comments/{{ $comment->id }}"
data-token="{{csrf_token()}}">
<i class="fa fa-trash-o"></i>Delete
</button>
</div>
<hr/>
#else
<div><i class="fa fa-check-square-o"></i>
<span>{{ $comment->comments }} by
<span style="font-style: italic;color: #09f;">
{{ ($comment->user()->first()->username === auth()->user()->username) ? 'You' : $comment->user()->first()->username }}
</span>
</span></div>
#endif
#endforeach
<form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', ['projectId'=> $project->id, 'taskId'=>$task->id])}}">
<div class="form-group{{ $errors->has('comments') ? ' has-error' : '' }}">
<textarea name="comments" class="form-control" style="width:80%;" id="comment" rows="5" cols="5"></textarea>
#if ($errors->has('comments'))
<span class="help-block">{{ $errors->first('comments') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Comment</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>

Categories