how to add new category in post page laravel - php

when i want to make a post i have to first create a category on a different page, namely the category page, not the post page. how to make it like wordpress which can add categories and tags on the post page.
i have tried but i don't know how to make validation so that the category name and category slug (Str::slug) are unique.
after I tried to successfully add a category on the post page but when I added an existing category it still worked, not unique.
PostController.php
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required',
'categories' => 'required',
]);
$slug = Str::slug($request->name);
$post = new Post();
$post->user_id = Auth::id();
$post->name = $request->name;
$post->slug = $slug;
$post->save();
// get old categories name
$existingCategory = $video->categories->pluck('name')->toArray();
// find new categories
foreach($request->categories as $category) {
if(! ( in_array($category, $existingCategory) ) ) {
//create a new category
$newCategory = new Category();
$newCategory->name = $category;
$newCategory->slug = Str::slug($category);
$newCategory->save();
}
}
$attachableCategory = [];
foreach($request->categories as $category) {
$attachableCategory[] = Category::where('name', $category)->pluck('id')->first();
}
$video->categories()->sync($attachableCategory);
return redirect()->route('admin.post.index');
}

It seem that your categories are an array. I they are. Problably your are looking for array validation
Validator::make($input, [
// validates there is an array
'categories' => 'required|array|min:1',
// validates values inside array
'categories.*' => 'required|string|min:4'
]);

Related

how to retrieve image and imagess data using one to many relationship

i'm working on a E-commerce like project where users can upload multiple images of a particular product, i have a one-to-many relation where many images has one description and price. so on the homepage i need to call a single image out of the total uploaded and also fetch the descrition of the image, so when view button is clicked the user can see the rest of the image sliding.
upload Controller
public function store(Request $request)
{
$request->validate([
'farm_name'=> 'required',
'farmer_name'=>'required',
'min_order'=>'required',
'qty'=>'required',
'product_package'=>'required',
'descr'=>'required',
'images' => 'required',
'images.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
//'' => 'image|mimes:jpeg,png,jpg,gif,svg|max:6048',
]);
$input = new Product;
$input->farm_name = $request->farm_name;
//$input->user_id = Auth::user()->id;
$input->farmer_name = $request->farmer_name;
$input->tel = Auth::user()->tel;
$input->farm_adrs = Auth::user()->adrs;
$input->state = Auth::user()->state;
$input->email = $request->email;
$input->qty = $request->qty;
$input->descr = $request->descr;
$input->product_package = $request->product_package;
$input->catr = $request->catr;
$input->lga = $request->product_name;
$input->amount = $request->amount;
//$input->img = $request->images;
//dd($input);
$input->save();
foreach($request->file('images') as $imageFile){
$image = new Image;
$imageName = time().rand(1,99).'.'.$imageFile->extension();
$imageFile->move(public_path('images'), $imageName);
$image->images_id = $input->id;
$image->name = $imageName;
$image->save();
}
return back()
->with('success','Your Product is succesfully Uploaded.');
}
//show image this where i have problem
public function index(){
$products = Product::all();
foreach($products as $product){
$product_id = $product->id;
$images = Image::find($product_id);
}
return view('app.index', compact('products', 'images'));
}
First of all u re saving product id as image_id on image table. Is this your correct column? if you are using image_id to save related product id, then change the index code to
public function index(){
$images=[];
$products = Product::all();
foreach($products as $product){
$product_id = $product->id;
$images[$product_id] = Image::where('image_id',$product_id)->get();
}
return view('app.index', compact('products', 'images'));
}
it will give you all the images from all product set upped with index of product id.
in a better way you can directly join the table. it will reduce the no. of executing query.

Passing posts view id to another controller so I can access table records in Laravel

So I got posts page, where I have my post. The post has ID which leads to posts table. For it I have PostsController where everything is accessible thru $post->id, $post->name etc.
But I have another table named orders and on submit button, it needs some of the information from posts table by id.
I have post with url posts/{id} and there's submit button in the view
The button needs to get $post information to OrdersController with submit information.
I've tried this in OrdersController
public function store(Request $request)
{
$this->validate($request, [
]);
$post = Post::all();
$order = new Order;
$order->client = Auth::user()->name;
$order->phone = Auth::user()->phone;
$order->vehicle = $post->id;
$order->date_from = $request->input('date_from');
$order->save();
return redirect('/posts')->with('success', 'Rezervēts');
}
$posts = Post::all(); is getting all array information from table but cannot use specific id.
And I have this in PostsController
public function show($id)
{
$post = Post::find($id);
$images = Image::All();
return view('posts.show')->with(compact('post', 'images'));
}
Route for show function
Error that I'm getting with shown code: Property [id] does not exist on this collection instance.
Expected results:
orders table
Error: Property [id] does not exist on this collection instance.
Problem: Can't get exact id from view to another controller, so I can access posts table records that I need from OrdersController.
Ps. I've been thru most of the pages on this error, but can't seem to find the one that would help.
I would modify the route so that the affected post.
Route:
Route::post('/order/{post}/store', 'OrderController#store')->name('order.store');
Controller (store method):
public function store(Post $post, Request $request){
$this->validate($request, [
]);
$order = new Order;
$order->client = Auth::user()->name;
$order->phone = Auth::user()->phone;
$order->vehicle = $post->id;
$order->date_from = $request->input('date_from');
$order->save();
return redirect('/posts')->with('success', 'Rezervēts');
}
Okay, thanks.
Solved: {{ Form::hidden('id', $post->id) }} in view.
Controller: $post = Post::findorfail($request->id);
Then: $post->(whatever you need from table)

Pass parent id to relationship factory?

I have a factory of posts and a factory of posts_images, a post can have many images, I did create a seeder for posts that look like this.
$posts = factory(App\Models\Post::class, 100)->create()
->each(function($post) {
$post->images()->saveMany(factory(App\Models\PostsImage::class, 3)->make());
});
I want to create 100 posts and that each post have 3 images, this kind of work, the problem is when the image is created
I want the image to be created from a base_64 string and saved in certain directory but I need the id of the post so I can create the folder where it will be created.
$factory->define(App\Models\PostsImage::class, function (Faker $faker) {
//this does not get me the id
$id = factory(\App\Models\Post::class)->make()->id;
$name = time();
$b64_image = \Config::get('constants.seed_image');
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $b64_image));
if(!file_exists(public_path('images/eventos/'.$id.'/'))) {
mkdir(public_path('images/noticias/'.$id.'/'));
}
file_put_contents(public_path('images/noticias/'.$id.'/'.$name.'.jpg'), $data);
return [
//
'image' => $name,
'order' => $id + 1
];
});
The only line that seems not to be working is
$id = factory(\App\Models\Post::class)->make()->id;
I did try using create instead of make, but this create more rows in the post table, and I don't want that.
Is there a way to pass the post id to the images factory?
The best option will be to create the directory in the posts seeder, since you have the access to the $post object when a Post is created. Try something like this:
$posts = factory(App\Models\Post::class, 100)->create()
->each(function($post) {
//id is available on the $post object created
$id = $post->id;
$name = time();
$b64_image = \Config::get('constants.seed_image');
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $b64_image));
if(!file_exists(public_path('images/eventos/'.$id.'/'))) {
mkdir(public_path('images/noticias/'.$id.'/'));
}
file_put_contents(public_path('images/noticias/'.$id.'/'.$name.'.jpg'), $data);
$post->images()->saveMany(factory(App\Models\PostsImage::class, 3)->make());
});
What I do usually in this situation is to get the last record of the created parent object.
$factory->define(App\Models\PostsImage::class, function (Faker $faker) {
// get last record of Post
$id = \App\Models\Post::orderBy('id', 'desc')->first()->id;
$name = time();
$b64_image = \Config::get('constants.seed_image');
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $b64_image));
if(!file_exists(public_path('images/eventos/'.$id.'/'))) {
mkdir(public_path('images/noticias/'.$id.'/'));
}
file_put_contents(public_path('images/noticias/'.$id.'/'.$name.'.jpg'), $data);
return [
//
'image' => $name,
'order' => $id + 1
];
});

Product Variants Data updated with last record Every time in Laravel 5.3

I'm trying to update my products with dynamic variants. There is no issue in adding or deleting but when I trying to update, every time it updates with the last record on every field.
I'm trying to update dynamic variants where I have inserted...
color - red
shape -square
It's about dynamic form-fields, inserts work properly but when I am trying to update it, it updates the first value with both the filled and the second value with both the field and I won't be able to distinguish the field because of the Blade file returns in form of an array.
When I am trying to update with any value it repeats the last value in both the fields, so the output looks like...
shape-square
shape-square
Controller
<?php
public function updateProducts($id, Request $request)
{
$featured = Input::has('featured') ? true : false;
$product = Product::findOrFail($id);
$product->update(array(
'product_name' => $request->input('product_name'),
'product_qty' => $request->input('product_qty'),
'product_sku' => $request->input('product_sku'),
'price' => $request->input('price'),
'reduced_price' => $request->input('reduced_price'),
'cat_id' => $request->input('cat_id'),
'brand_id' => $request->input('brand_id'),
'featured' => $featured,
'description' => $request->input('description'),
'product_spec' => $request->input('product_spec'),
));
$product->update($request->all());
$variants = VariantsOption::where('products_id', $id)->get();
$test = $request->all();
foreach ($variants as $v) {
$x = $v->id;
foreach ($test['variants_id'] as $key => $attrib) {
$var_name = $test['txt'][$key];
$varid = $attrib;
$variants = new VariantsOption;
$data = array(
'variants_id' => $varid,
'variants_name' => $var_name
);
$variants->where('id', '=', $x)->update($data);
}
}
return redirect('/admin/products/');
}
use following in your class
use Illuminate\Database\Eloquent\ModelNotFoundException;
OR add exception handler within your function
// Will return a ModelNotFoundException if no user with that id
try
{
$product = Product::findOrFail($id);
}
// catch(Exception $e) catch any exception
catch(ModelNotFoundException $e)
{
dd(get_class_methods($e)) // lists all available methods for exception object
dd($e)
}
It looks like product with that $id is not found so on update it is adding new product in database.

Inserting array using Laravel Eloquent

I have an array which I combined using array_combine. I am trying to store each array KEY AND VALUE into a child table. However, I can't figure out to do this. Please help!
HERE'S AN EXAMPLE OF A RETURNED ARRAY
array:2 [▼
"Design" => "Pattern"
"Brand" => "Sony"
]
PRODUCT MODEL
public function productAttributes()
{
return $this->hasMany('App\ProductAttribute');
}
PRODUCT ATTRIBUTE MODEL
protected $fillable = [
'attribute_name', 'attribute_value', 'used_as_filter'
];
public function product()
{
return $this->belongsTo('App\Product');
}
PRODUCT CONTROLLER
$product = new Product();
$product->category_id = $request->category_list;
$product->name = $request->name;
$product->price = $request->price;
$product->save();
/**Optional Data**/
if ($request->has('attribute_name')){
$attributes = array_combine($request->input('attribute_name'), $request->input('attribute_value'));
$product->productAttributes()->create($attributes);
}
When I run this, I get one row inserted into the product table, and one row inserted into the product_attribute table. However, the columns attribute_name and attribute_value is blank.
$attributes = array_combine($request->input('attribute_name'), $request->input('attribute_value'));
collect($attributes)->each(function ($value, $name) use ($product) {
$product->productAttributes()->create([ 'attribute_name' => $name, 'attribute_value' => $value, ]);
});

Categories