Call to a member function store() on null - laravel

I'm just wondering why this doesn't work for my Item Controller however on other controller it worked.
Controller for Item
public function store(Request $request, $id)
{
$categories = Category::all();
$userId = Auth::user()->id;
$item = new Item;
$item->store_id = $userId;
$item->name = $request->name;
$item->description = $request->description;
$item->unit_price = $request->unit_price;
$path = $request->image->store('images', 'public');
$item->picture = $path;
$item->category_id = $request->category_id;
$item->is_available = 1;
$item->is_archived = 0;
$item->save();
return redirect('/store/index')->with('status', 'Item successfully added!');
}
HTML form for creating item data
<form method="post" action='{{ url("item/store/$stores->id") }}' enctype="mutlipart/form-data">
#csrf
<input placeholder="Item Name" type="text" name="name" value="{{ old('name') }}" required autocomplete="off">
<input placeholder="Item Description" type="text" name="description" value="{{ old('description') }}" required autocomplete="off">
<input placeholder="000.00" type="decimal" name="unit_price" value="{{ old('unit_price') }}" required autocomplete="off">
<input type="file" name="picture" required>
<label>Category</label>
<select class="form-control" name="category_id">
<option value selected disabled>Select Category</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
<button type="submit" class="btn btn-warning">Add</button>
</form>
Thank you for answering

There are no any input file named image your input file name is picture.
if you have a file field, and you try to check it with $request->has(‘file’), it will always return FALSE. For files there is a specific method :
$path = $request->image->store('images', 'public');
$item->picture = $path;
Change to
$path = "no_image.png";
if ($request->hasFile('picture')) {
// $path = $request->picture->store('images', 'public');
$file = $request->picture;
$fName = time() . $file->getClientOriginalName();
$file->move('images', $fName );
$path = 'images/'.$fName;
}
$item->picture = $path;

Related

Image saving as tmp in database in Laravel 8

Blade File
<form action="{{route('sproduct.store')}}" method="post" enctype="multipart/form-data"
class="w-75 mx-auto mt-5 mb-5">
#csrf
<div class="form-group mt-2 mb-2">
<label for="formgroupexampleinput">Select Product</label>
<select name="products" id="products">
<option>Select Product</option>
#foreach ($products as $product)
<option value="{{$product->id}}">{{$product->title}}</option>
#endforeach
</select>
</div>
<div class="form-group mt-3 mb-3">
<label for="formgroupexampleinput">Display Images</label>
<input type="file" name="file[]" multiple>
</div>
<input type="submit" class="form-control btn-primary w-25" value="Submit">
</form>
Fuction
public function store(Request $request)
{
if ($request->hasfile('file')) {
foreach ($request->file as $file) {
$fileExt = $file->getClientOriginalExtension();
$image_name = "img_".rand(123456,999999). "." .$fileExt;
$destination_path = public_path('/uploads/products_images/display_images');
$file->move($destination_path, $image_name);
$image = new Image();
$image->image = $file;
$image->product_id = $request->products;
$image->save();
}
return "done";
}
}
Question
hi, I am trying to add images to my images table but the image is saving as a tmp file in the database but saving as a jpg in the given folder. I don't know the solution to this. Please let me know why this is happening and how to fix it.
Thankyou in Advance.
mikenenter code here in comment told me I needed to save the name of the file.
public function store(Request $request)
{
if ($request->hasfile('file')){
foreach ($request->file as $file) {
$fileExt = $file->getClientOriginalExtension();
$image_name = "img_".rand(123456,999999). "." .$fileExt;
$destination_path = public_path('/uploads/products_images/display_images');
$file->move($destination_path, $image_name);
$image = new Image();
$image->image = $image_name;
$image->product_id = $request->products;
$image->save();
}
return "done";
}
}

CRUD Laravel Update in database one to many relationship

I have a CRUD app with cars and for every car I have fields to create a car and images to upload. Everithing is ok, I have one to many relationship and for a car I can have multiple images . When I create a car and I upload photos these photos are stored correctly in database and in my public/images director. The problem is when I want to make the UPDATE par, I don't know how to update the photos in database.
Here is my code, I have update function where I don't know exactly how to make the update .
My cars table:
Schema::create('cars', function (Blueprint $table) {
$table->id();
$table->string('model');
$table->integer('seats');
$table->string('fuel');
$table->integer('year');
$table->string('color');
$table->string('gearbox');
$table->integer('price');
$table->string('coinType');
$table->timestamps();
});
My images table
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->integer('car_id');
$table->string('file_name');
$table->timestamps();
});
My Car model:
class Car extends Model
{
protected $fillable = [
'model',
'seats',
'fuel',
'year',
'color',
'gearbox',
'price',
'coinType',
];
public function images()
{
return $this->hasMany(Image::class);
}
use HasFactory;
}
My Image model:
class Image extends Model
{
protected $fillable = [
'car_id',
'file_name'
];
public function car()
{
return $this->belongsTo(Car::class);
}
use HasFactory;
}
My edit.blade:
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-sm-8 offset-sm-2">
<h2 class="display-3">Edit a car</h2>
<div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div><br />
#endif
<form method="post" action="{{ url('cars/'. $res->id) }}" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group">
<legend>
<label for="model" class="col-form-label">Model :</label>
<input type="text" class="form-control" id="model" name="model" value="{{ $res->model }}">
</legend>
</div>
<div class="form-group ">
<legend>
<label for="seats" class="col-form-label">Seats :</label>
</legend>
<select name="seats" id="seats" value="{{ $res->seats }}">
<option value="2">2</option>
<option value="4" selected="selected">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
</div>
<div class="form-group ">
<legend>
<label for="fuel" class="col-form-label">Fuel :</label>
</legend>
<select name="fuel" id="fuel" value="{{ $res->fuel }}">
<option value="benzine+gpl">benzine+gpl</option>
<option value="gpl" selected="selected">diesel</option>
<option value="diesel">gpl</option>
<option value="diesel+gpl">diesel+gpl</option>
<option value="benzine">benzine</option>
</select>
</div>
<div class="form-group ">
<legend>
<label for="year" class="col-form-label">Year :</label>
</legend>
<input type="text" name="year" id="year" value="{{ $res->year }}">
</div>
<div class="form-group">
<legend>
<label for="color" class="col-form-label">Color :</label>
</legend>
<input type="text" class="form-control" id="color" name="color" value="{{ $res->color }}">
</div>
<div class="form-group ">
<legend>
<label for="gearbox" class="col-form-label">Gearbox :</label>
</legend>
<select name="gearbox" id="gearbox" value="{{ $res->gearbox }}">
<option value="manual">manual</option>
<option value="automatic" selected="selected">automatic</option>
<option value="semiautomatic">semiautomatic</option>
</select>
</div>
<div class="form-group ">
<legend>
<label for="price" class="col-form-label">Price :</label>
</legend>
<input type="text" class="form-control" id="price" name="price" value="{{ $res->price }}">
</div>
<div class="form-group ">
<legend>
<label for="coinType" class="col-form-label">CoinType :</label>
</legend>
<select name="coinType" id="coinType" value="{{ $res->coinType }}">
<option value="EUR">EUR</option>
<option value="LEI" selected="selected">LEI</option>
<option value="USD">USD</option>
</select>
</div>
<div class="form-group ">
<legend>
<label for="image" class="col-form-label">Upload images :</label>
</legend>
<input type="file" class="form-control" name="images[]" multiple />
</div>
<hr style="height:2px;border-width:0;color:gray;background-color:gray">
<div class="col-xs-12 col-sm-12 col-md-12 ">
<button type="submit" class="btn btn-primary">Add car</button>
<a class="btn btn-primary" href="{{ route('cars.index') }}"> Back</a>
</div>
</div>
</form>
#endsection
My Controller:
public function store(Request $request)
{
$request->validate([
'model' => 'required',
'year' => 'required',
'color' => 'required',
'price'=> 'required',
'file_name.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
$destionationPath = public_path('/images');
$images = [];
$car = new Car();
$car->model = $request->model;
$car->seats = $request->seats;
$car->fuel = $request->fuel;
$car->year = $request->year;
$car->color = $request->color;
$car->gearbox = $request->gearbox;
$car->price = $request->price;
$car->coinType = $request->coinType;
$car->save();
if ($files = $request->file('images')) {
foreach ($files as $file) {
$fileName = $file->getClientOriginalName();
$file->move($destionationPath, $fileName);
$images[] = $fileName;
}
}
foreach ($images as $imag) {
$image = new Image();
$image->car_id = $car->id;
$image->file_name = $imag;
$image->save();
}
return redirect()->route('cars.index')->with('success', 'Car saved !');
}
public function update(Request $request, $id)
{
$request->validate([
'model' => 'required',
'year' => 'required',
'color' => 'required',
'price'=> 'required',
'file_name.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
$destionationPath = public_path('/images');
$images = [];
$car = Car::find($id);
$car->model = $request->model;
$car->seats = $request->seats;
$car->fuel = $request->fuel;
$car->year = $request->year;
$car->color = $request->color;
$car->gearbox = $request->gearbox;
$car->price = $request->price;
$car->coinType = $request->coinType;
$car->update();
if ($files = $request->file('images')) {
foreach ($files as $file) {
$fileName = $file->getClientOriginalName();
$file->move($destionationPath, $fileName);
$images[] = $fileName;
}
}
foreach ($images as $imag) {
//here I don't know what to do
}
return redirect()->route('cars.index')->with('success', 'Car updated !');
}
Thank you.
Looking at your blade file, you don't really show the original images, so the user can't update the images, he can insert new images and the old ones should be deleted, right?
If you want to update the image, you have to show the original images on the blade file with it's ID, so in the backend you can find the image based on the ID
But this should work for you now:
you can just check if the user uploaded any file
if ($files = $request->files('images')) {
...
delete all the old images
$car->images->each(function($image) {
// You probabily should be using Storage facade for this
// this should be the full path, I didn't test it, but if you need add extra methods so it returns the full path to the file
if (file_exists($destionationPath . '/' . $image->file_name) {
unset($destionationPath . '/' . $image->file_name);
}
$image->delete();
});
And then recreate the images like you do on store
foreach ($files as $file) {
$fileName = $file->getClientOriginalName();
$file->move($destionationPath, $fileName);
$images[] = $fileName;
}
foreach ($images as $imag) {
$image = new Image();
$image->car_id = $car->id;
$image->file_name = $imag;
$image->save();
}

Call to a member function getClientOriginalName() on array

I have a multiple image. When I upload multiple image. I get this error.
Call to a member function getClientOriginalName() on array
my blade
<form action="{{ route('design-studios.store') }}" method="post" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="title" value="{{ old('title') }}">
</div>
//......
<div class="form-group">
<fieldset class="border p-3">
<legend>Slideshow</legend>
<div class="form-group">
<label for="sliders">Image</label>
<input id="sliders" name="sliders[]" type="file" multiple class="form-control">
</div>
</fieldset>
</div>
</form>
My Controller.
public function store(Request $request)
{
$design_studio = new DesignStudio;
$design_studio->user_id = 1;
$design_studio->title = $request->title;
$design_studio->lang = $request->lang;
$design_studio->body = $request->body;
if($request->has('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/design-studio'), $filename);
$design_studio->image = $request->file('image')->getClientOriginalName();
}
if ($sliders = $request->file('sliders')) {
foreach ($sliders as $slider) {
$filename = $slider->getClientOriginalName();
$slider->move(public_path('images/design-studio'), $filename);
$design_studio->sliders = $request->file('sliders')->getClientOriginalName();
}
}
$design_studio->save();
$design_studio->categories()->attach($request->category);
return redirect()->route('design-studios.index');
}
My Model
protected $casts = [
'sliders' => array()
];
I get this error.
Call to a member function getClientOriginalName() on array
Here you need to call that method on the file you are looping through instead of trying to call it on the array of files.
$design_studio->sliders = $slider->getClientOriginalName();

I got this error "Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type

When i try to insert data into my table this error occurs
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\xampp\htdocs\Portal\vendor\laravel\framew...
view
<form method="post" action="{{ route('notice.store') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="Select Group to Post Notice">Select Group to Post Notice </label>
<select class="bg-white text-danger form-control " name='GroupID[]' multiple>
#foreach ($users as $user)
<option value="{{ $user->GroupID }}">{{ $user->GroupID }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="Enter Notice">Enter Notice</label>
<input class="bg-white text-danger p-2 form-control form-control-sm" type="text" name="Notice" placeholder="Enter Notice">
</div>
<input class="btn btn-danger btn-lg px-5" type="submit" name="submit">
</form>
controller
public function store(Request $request)
{
$member = $request->input('GroupID');
foreach($member as $value) {
$storeInfo = new notice();
$storeInfo->GroupID = $request->input('GroupID');
$storeInfo->Notice = $request->input('Notice');
$storeInfo->save();
}
return redirect('/notice');
}
I would imagine the reason you're getting this error is because of:
$storeInfo->GroupID = $request->input('GroupID');
$request->input('GroupID') will return an array (name='GroupID[]') and not an individual id.
Since you're already looping through the group ids you can instead use the value for the GroupId:
public function store(Request $request)
{
foreach ($request->input('GroupID') as $groupId) {
$storeInfo = new notice();
$storeInfo->GroupID = $groupId; //<--here
$storeInfo->Notice = $request->input('Notice');
$storeInfo->save();
}
return redirect('notice');
}
try changing controller logic
public function store(Request $request)
{
//
$member=$request->input('GroupID');
foreach($member as $value){
$storeInfo = new notice();
$storeInfo->GroupID = $value;
$storeInfo->Notice = $request->input('Notice');
$storeInfo->save();
}
return redirect('/notice');
}

Foreach in Yajra DataTable Laravel

I'm trying to put a foreach loop inside my datatable but it wont work,
P.S. if I remove the foreach everything works fine already,
attached here is my code
$Product = Product::query();
$colors = Color::all();
return Datatables::eloquent($Product)
->addColumn('category_name', function($row) {
$category = Category::select('name')->where('id', $row->category_id )->pluck('name')->toArray();
return $category;
})
->addColumn('add_color', function($row) {
$return =
'<form class="form-inline" method="post" action="/procurement/add-product" style="max-width: 170px;">
<input type="hidden" name= "product_id" value="' . $row->id . '">
<div class="form-group">
<select name="color_id" class="form-control" required autofocus>
'.foreach ($colors as $color){.'
<option value="test">test</option>'.}.'
</select>
</div>';
return $return;
});
That won't work, you're attaching a foreach into a string
What you may do is perform the foreach first to prepare the items you want to attach in that string.
E.g.,
<option>something</option>
<option>something more</option>
Before setting the $return do the foreach:
->addColumn('add_color', function($row) {
$options = ''
// here we prepare the options
foreach ($colors as $color) {
$options .= '<option value="test">$color</option>';
}
$return =
'<form class="form-inline" method="post" action="/procurement/add product" style="max-width: 170px;">
<input type="hidden" name= "product_id" value="'.$row->id.'">
<div class="form-group">
<select name="color_id" class="form-control" required autofocus>' . $options . '</select>
</div>';
return $return;
})
You need to perform foreach outside your return. and then you also need no use or import the $color variable inside your data table. something like this ..
$Product = Product::query();
$colors = Color::all();
return Datatables::eloquent($Product)
->addColumn('category_name', function($row) {
$category = Category::select('name')->where('id', $row->category_id )->pluck('name')->toArray();
return $category;
})
->addColumn('add_color', function($row) use ($colors) {
$options = '';
foreach ($colors as $color) {
$options .= '<option value="test">$color</option>';
}
$return =
'<form class="form-inline" method="post" action="/procurement/add-product" style="max-width: 170px;">
<input type="hidden" name= "product_id" value="' . $row->id . '">
<div class="form-group">
<select name="color_id" class="form-control" required autofocus>
</select>
</div>';
return $return;
});

Resources