Answer the question
In order to leave comments, you need to log in
How to implement image in laravel by default?
Hello to all!
help guys. really the brain is exploding already just literally searched everything.
but i need to implement such thing.
In the admin panel, there are fields to select a picture - 5 such fields
in laravel validation removed the required items - so that pictures are optional.
but when I save the post - just not choosing pictures, it gives me Call to a member function store() on null .
and I know that in html input it is impossible to make this image the default.
but the boss says I need the picture if you don't choose to be set by default (for example, some branded picture) I looked at the entire dock. but there is no such looked Google. stack overflow. was everywhere. couldn't find an answer. I'm banging my head against the wall because I'm sure there is a solution but I can't find it.
The bottom line is how to make what method how to structure the table so that if I do not want to select a picture, it would be the default.
I will appreciate your answer and help. I didn't just write this. I really was everywhere! and that I just did not write.
PS - my nightmarish method. because I want to write code, but I understand that this is trash and not code.
/**
* ATENTION to the code needs refactoring DRY
* Validate/method add
* return array
*/
public function add(Request $req)
{
/*rules ready */
$rules = [
'name' => 'required|string|max:150',
'seo_desk' => 'required|string|max:150',
'brand' => 'required|string|max:150',
'selecter' => 'required',
'image1' => 'mimes:jpg,jpeg,bmp,png|max:2000',
'image2' => 'mimes:jpg,jpeg,bmp,png|max:2000',
'image3' => 'mimes:jpg,jpeg,bmp,png|max:2000',
'image4' => 'mimes:jpg,jpeg,bmp,png|max:2000',
'image5' => 'mimes:jpg,jpeg,bmp,png|max:2000',
'price' => 'required|numeric',
'oldPrice' => 'required|numeric',
'count' => 'required|numeric',
'desc_short' => 'required|string|max:500',
'desk_large' => 'required|string|max:5000',
'feature' => 'alpha_dash|string|max:50',
'color' => 'alpha_dash|string|max:50'
];
//start API
$val_api = Validator::make($req->all(), $rules);
//cheking
if ($val_api->fails()) {
return redirect('add')
->withInput()
->withErrors($val_api);
} else {
$data = $req->input(); // get data
try {
// save Post
$post = new Product();
$post->name = $data['name'];
$post->seo_desk = $data['seo_desk'];
$post->brand = $data['brand'];
$post->selecter = $data['selecter'];
$post->price = $data['price'];
$post->oldPrice = $data['oldPrice'];
$post->count = $data['count'];
$post->desc_short = $data['desc_short'];
$post->desk_large = $data['desk_large'];
$post->color = $data['color'];
$post->lenght = $data['lenght'];
$post->height = $data['height'];
$post->width = $data['width'];
$post->feature = $data['feature'];
$post->scope = $data['scope'];
$post->example_one = $data['example_one'];
$post->example_two = $data['example_two'];
$post->save();
/**
* save relationships image
*/
$image = new Image(
[
'img1' => $req->file('image1')->store('uploads','public'),
'img2' => $req->file('image2')->store('uploads','public'),
'img3' => $req->file('image3')->store('uploads','public'),
'img4' => $req->file('image4')->store('uploads','public'),
'img5' => $req->file('image5')->store('uploads','public'),
]
);
// get last id and store by id and save images
$post = Product::latest()->first();
$post->image()->save($image);
// redirect
return redirect('/add')->with('status', 'Успех данные сохранены');
} catch (Exception $e) {
return redirect('/add')->with('status', 'провал - данные не сохранены');
}
}
// end
}
Answer the question
In order to leave comments, you need to log in
try like this
$image = new Image(
[
'img1' => $request->has("image1") ? $req->file('image1')->store('uploads','public'): null,
'img2' => $request->has("image2") ? $req->file('image2')->store('uploads','public'): null,
'img3' => $request->has("image3") ? $req->file('image3')->store('uploads','public'): null,
'img4' => $request->has("image4") ? $req->file('image4')->store('uploads','public'): null,
'img5' => $request->has("image5") ? $req->file('image5')->store('uploads','public'): null,
]
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question