Laravel tagging system - laravel-5

I am developing an app where each post has number of tags. And another user subscribes to a tag and the user gets the tags only they have subscribed on. Currently I have made a
Tags table
-ID
-name
-slug
And another tags_users table
-ID
-user_ID
-tag_ID
So how do I get the subscribed post.Is there better way to manage the tags

While storing the datas
//Get the current user id
$user = Auth::user()->id;
//Getting the tag id from the form
$tagId = $request->get('tag_id')
$tag = new TagUser();
$tag->user_id = $user;
$tag->tag_id = $tagId;
$tag->save()
While fetching the datas
$tagusers = TagUser::get()

Related

What is use of attach() method in Laravel?

i dont have a idea what is use of this method i saw something on someone code like this.
$user = JWTAuth::parseToken()->authenticate();
$new_car = new Car();
$new_car->name = $request->name;
$new_car->age = $request->age;
$new_car->model = $request->model;
$new_car->save();
$time = new Reservation();
$time->from_date = $request->from_date;
$time->to_date = $request->to_date;
$time->from_time= $request->from_time;
$time->to_time = $request->to_time;
$time->save();
// Attach the reservation to the car's reservations
$new_car->Reservation()->attach($time->id);
// Attach the car to the user cars
$user->cars()->attach($new_car->id);
hopefully someone can explain me well.
Attaching / Detaching
Attach is used mainly Eloquent Relationship in Many To Many Relationships. It mainly uses intermediate table data insert or update. For example, let's imagine a user can have many roles and a role can have many users. You may use the attach method to attach a role to a user by inserting a record in the relationship's intermediate table:
For more details

how to retrieve the id just created in the database in laravel? [duplicate]

This question already has answers here:
Get the Last Inserted Id Using Laravel Eloquent
(32 answers)
Closed 3 years ago.
how do I retrieve the id that was just created in the database, when I press the save button, the data is created, and I want to retrieve the id from that data
this my controller code
$cart= new cart;
$cart->user_id = $request->user_id;
$cart>vendor_name = $request->vendor_name;
$cart->save();
I want to retrieve the id of the data just created
After saving you can access id like:
$cart->id
This will be the id of the card you have recently created.
$cart= new cart;
$cart->user_id = $request->user_id;
$cart>vendor_name = $request->vendor_name;
$cart->save();
$cart->id; //here you still have access to the data and you can access the id like this.
Same thing goes to the user_id and any other field, you can access them like this: $cart-> user_id;
All information you cation access by this $cart.after saving this you just
$cart->id (to get id)
$cart->user_id(to get user_id)

How to save record in database using belongs to many relation in laravel

I have three tables: restaurant_location, cuisine_restaurant_location and cuisines.
There is a list of all cuisines in my cuisines table. I have all the details of the restaurant in my restaurant_location table. A restaurant can have many cuisines so I made a table cuisine_restaurant_location in which there are two columns cuisine_id and restaurant_id. I have created a belongs to many relation in my restaurant_location model.
restaurant_location model
public function cuisines()
{
return $this->belongsToMany(Cuisine::class, 'cuisine_restaurant_location', 'restaurant_id', 'cuisine_id');
}
I have a form in which all the details of the restaurant along with cuisines is supposed to be added. Right now I am adding all the details of the restaurant. Now the question is how can I insert the cuisines in "cuisine_restaurant_location".
Controller
$rest = new restaurant_location;
$rest->name = $request->input('name');
$rest->description = $request->input('desc');
$rest->save();
$cuisine = new cuisine_restaurant_location
$cuisine_lists = $request->input('cuisines');
foreach ($cuisine_lists as $cuisine_list) {
$cuisine->name = $cuisine_list;
}
You can use the sync and attach methods, described in the Many to many section of the eloquent documentation, for that:
$rest->cuisines()->attach([$cuisineIds])
Where cuisineIds is the list of ids of the cuisines you want to relate.
The difference between sync and attach is that sync will remove all id's not present on the array of ids you are passing.
Try sync() method:
$h = [];
if (isset($input["cuisine_id"])) {
$h = $input["cuisine_id"];
}
$restaurant_location->cuisines()->sync($h);

HasMany relationship attach model without saving directly

The following is just an example: Take the following two models with their relationships:
User => hasMany('Address', 'user_id')
Address => => belongsTo('User', 'user_id')
I want to be able to link an address with an user with out directly saving it.
something like:
$user = new User();
$user->name = "John";
$address1 = new Address();
$address1->street = "Foo street";
$address2 = new Address();
$address2->street = "Bar street";
$user->addresses()->push($address1);
$user->addresses()->push($address2);
//do some stuff to maybe validate some things or pass the user object to some other controllers that handels it
//Save the user and the addresses here.
$user->save()
So this different that $user->addresses()->saveMany([$address1, $address2]);
Hope you anyone give me an elegant solution.
You should distinguish your validation action from the save action. You can achieve this by creating different methods in the Object Controller. One for the validations that you will check for duplicates,similar etc and then if validation passes you should redirect to a store route where you 'll create and save the objects.

Laravel 4 get the id of created field

I am developing a booking form that has data that will fill two tables, users and booking table. In the controller I am getting the input fields and inserting the user fields first, then I do the insert for the booking but I need the id of the user I've just inserted, I have tried many methods without success, this is what I have done in my controller:
$User = new User;
User::create(array(
'lastname'=>Input::get('lastname'),
'name'=>Input::get('name'),
'address'=>Input::get('address'),
'cell_phone'=>Input::get('cell_phone'),
'email'=>Input::get('email')
));
// I try to get the inserted user id here
$userInserted=$User->id;
// And here I insert the booking with the user_id
$Booking = new Booking;
Booking::create(array(
'apartment_id'=>Input::get('apartment_id'),
'user_id'=>$userInserted,
'date_ini'=>Input::get('date_from'),
'date_fin'=>Input::get('date_to'),
'stay_cost'=>Input::get('stay_cost'),
'stay_days'=>Input::get('stay_days')
));
The problem is I am not getting the user Id. Any idea way?
thanks in advance
You are assignin it wrong.
$newUser = User::create(...);
$insertedUserId = $newUser->id;
Your $User = new User; makes no sense, because it just initializes emty User Object, but User::create sends query to DB to store data and returns inserted Object.

Resources