trying to return items data from invoice - laravel

i need to manage invoice items.
here is my code below
in my model i've got this
public function items()
{
return $this->hasMany(invoice_product::class);
}
in the invoice_product model
public function products(){
return $this->belongsTo(prodStock::class);
}
and my controller below is
public function show(invoice $invoice, $id)
{
$invoice = Invoice::with('items.products')->findOrFail($id);
return view('pages.editInvoice', compact('invoice'));
}

Since we are passing variables using compact in the controller, all we have to do is get the view
controller
public function show(invoice $invoice, $id)
{
$invoice = Invoice::with('items.products')->findOrFail($id);
return view('pages.editInvoice', compact('invoice'));
}
view
<select>
#foreach($invoice as $in)
<option value="{{ $in->ColumnName }}">{{ $in->name }}</option>
#endforeach
</select>
If you do like this, you will be able to select in the drop down select

Related

how can i handle the relations beetween products attributes and values?

i'm working on a Laravel/Livewire project
there are some products and services in this platform that user can order them.
the price of these products and services are changeable by their attributes . like size,color,quality and....
and i made a Many To Many relation between products and attributes.
but a can't handle it in my view where user should select that attributes before ordering
and my for each loop return wrong data . and i get this error :
Trying to get property 'pivot' of non-object .
my migration :
public function up()
{
Schema::create('attr_product', function (Blueprint $table) {
$table->foreignId('attr_id')->constrained();
$table->foreignId('product_id')->constrained();
$table->string('value')->nullable();
$table->string('price')->nullable();
$table->timestamps();
});
}
product model :
public function attr(){
return $this->belongsToMany(Attr::class)->withPivot(['value','price'])->withTimestamps();
}
attr model:
public function product(){
return $this->belongsToMany(Product::class)->withPivot(['value','price'])->withTimestamps();
}
my controller :
class SingleProduct extends Component
{
public $product;
public function mount($id){
$this->product=Product::with('attr')->findOrFail($id);
}
public function render()
{
return view('livewire.front.product.single-product')->extends('layouts.Default')->section('content');
}
}
my loop in blade :
#foreach($product->attr as $attr)
<div class="col-lg-4 col-sm-6 mt-3">
<h6 class="mb-2 text-black">{{$attr->title}}</h6>
<select class="custom-select shadow-none">
#foreach($attr as $av)
<option value="{{$av->pivot->price}}">{{$av->pivot->value}}</option>
#endforeach
</select>
</div>
#endforeach
before #foreach add
#php
dd($product)
#endphp
You can troubleshoot your $product Model and relations
The right syntax for withPivot should be parameters separated by comma, try this:
product model
public function attr(){
return $this->belongsToMany(Attr::class)
->withPivot('value','price')
->withTimestamps();
}
attr model:
public function product(){
return $this->belongsToMany(Product::class)
->withPivot('value','price')
->withTimestamps();
}

show the data in dropdown fetched from database using laravel 8

i have to show the category values in dropdown in product form,the given code is from my view,the error is undefined $categories.this is my first code in laravel i dont know how to make changings in other files.which variable is used in foreach?or i have to create new function in ProductController?
<form action="/upload_product" method="post">
#csrf
<label>Choose Categories</label>
<select name="category_id" id="category" class="category">
<option disable selected>--select category--</option>
#foreach($categories as $item)
<option value="{{ $item->id }}">{{ $item->name}}</option>
#endforeach
</select>
<input type="text" name="name" placeholder="name">
<input type="number" name="sale_price" placeholder="sale_price">
</form>
Model Product.php
class Product extends Model
{
use HasFactory;
protected $table = 'products';
public $timestamps = true;
public function category(){
return $this->belongsTo('App\Models\Category');
}
}
Model Category.php
class Category extends Model
{
use HasFactory;
public $fillable = [ 'name' ];
protected $dates = [ 'deleted_at' ];
public $timestamps = true;
public function products (){
return $this->hasMany('App\Models\Product');
}
}
ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\Category;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$products = Product::all();
return view('products/index', ['products'=>$products]);
}
public function view()
{
$products = Product::with('category')->get();
$categories = Category::with('products')->get();
return view ('product.view')-> with([
'products' => $products,
'categories' => $categories,
]);
}
You have to use like below
public function index(){
$products = Product::all();
$categories = Category::with('products')->get();
return view('products.index', compact('products','categories'));
}
You don't have $categories in your index file.
Based on the index method, you're sendig just products:
public function index()
{
$products = Product::all();
return view('products/index', ['products'=>$products]);
}
So add categories too.
public function index()
{
$products = Product::get();
return view('products/index', ['products'=>$products]);
}
you can do this in three ways
public function index(){
$products = Product::all();
$categories = Category::with('products')->get();
return view('products.index', compact('products','categories'));
}
public function index(){
$products = Product::all();
$categories = Category::with('products')->get();
return view('products.index')->with(['products'=>$products,'categories'=>$categories]));
}
public function index(){
$data['products'] = Product::all();
$data['categories'] = Category::with('products')->get();
return view('products.index',$data);
}

How can I get video of specific model?

I want to display videos for each model in my table, where to define they are connected by their ids?
In my controller:
public function index()
{
$model_video = NewModelVideos::all();
return view('admin.model_new_videos.index')
->with('models', $models)
->with('model_video', $model_video);
}
in my Model:
class NewModelVideos extends Model
{
public function model()
{
return $this->belongsTo('App\Models\NewModels\NewModel', 'model_id');
}
}
class NewModel extends Model
{
public function videos()
{
return $this->hasMany('App\Models\NewModels\NewModelVideos', 'model_id', 'id');
}
}
and View:
#foreach($model_video as $model)
{{ $model->video }}
#endforeach
You should update your code like this:
public function index()
{
$model_video = NewModelVideos::all();
$models = NewModel::all();
return view('admin.model_new_videos.index')
->with('models', $models)
->with('model_video', $model_video);
}
and blade:
#foreach($models as $model)
{{ $model->videos }} //get videos
#endforeach
#foreach($model_video as $video)
{{ $video->model }} //get model
#endforeach

How to Insert id from a Name in Laravel 5.3 Combobox

I want to insert the id of the supplier by choosing the name of the supplier from a combo box.
View
<label for="supplier">Supplier</label>
<input list="supplier" name="supplier" placeholder="Select Supplier" class="form-control">
#foreach($suppliers as $key=>$value)
<datalist id="supplier">
<option value="{{$key}}">{{$value}}
</datalist>
#endforeach
Controller
<?php
public function store(Request $request, User $user)
{
$user = Auth::user();
$product = new Product;
$request->user()->products()->create($request->all());
}
Model
protected $fillable = ['name', 'qty', 'bprice', 'sprice', 'edate'];
public function user()
{
return $this->belongsTo(User::class, user_id);
}
use this
public function store(Request $request, User $user)
{
$user = Auth::user();
$product = new Product;
$product->supplier_id = $request->supplier;
$product->price = $request->price; // if u hv it
. // finish the rest then
$user->products()->save($product);
// finish the rest
}

Laravel 5.3 How to use multiple controllers in one view

I'm working with a view that at first displays all products, and on the sidebar, users can see a list of categories. My aim is when users press on any categories, it will display products of that category only. However, since there are 2 controllers is interacting with this view and send the same data, one is not running(CategoriesController). It does not show any error, just when I click on the link of each of the categories, it does not reload the page.
Here is my ProductsController:
class ProductsController extends Controller
{
// display all products and categories
public function index() {
$products = Product::all();
$categories = Category::all();
return view('frontend.product', compact('products','categories'));
}
And my CategoriesController:
class CategoriesController extends Controller
{
public function showProducts($category_id) {
$products = Category::find($category_id)->products;
return view('frontend.product', compact('products'));
}
Here is my view:
// Products part
#foreach($products as $product)
{{ $product->name }}
#endforeach
//Categories part
#foreach($categories as $category)
{{ $category->name }}
#endforeach
And route:
Route::get('/products', [ 'as' => 'products', 'uses' => 'frontend\ProductsController#index']);
Route::get('/categories/{category_id}', ['as' => 'categories', 'uses' => 'backend\CategoriesController#showProducts']);
Just include all of the categories in your showProducts function, and omit the current category:
public function showProducts($category_id)
{
$categories = Category::whereNotIn('id', $category_id)->get();
$products = Category::find($category_id)->products;
return view('frontend.product', compact('products', 'categories'));
}
Now there will be no discrepancies between the variables that are being used.
If I have not misunderstood, I thought you can use #inject in blade. For example:
namespace App\Presenters;
use App\User;
class UserPresenter
{
/**
* 是否顯示email
* #param User $user
* #return string
*/
public function showEmail(User $user)
{
if ($user->show_email == 'Y')
return '<h2>' . $user->email . '</h2>';
else
return '';
}
}
and
<div>
#inject('userPresenter', 'MyBlog\Presenters\UserPresenter')
#foreach($users as $user)
<div>
{!! $userPresenter->showEmail($user) !!}
</div>
</div>

Resources