I work on a project to learn Laravel.
So, I create a database with users. Each user got a profile with information like: city,country,etc.
I create a table named 'profiles' where I have the following columns: id / user_id / country / city.
My routes file looks like:
Route::get('editProfile', 'ProfileController#editProfileForm');
My ProfileController looks like:
public function editProfileForm() {
return view('profile.editProfile')->with('data', Auth::user()->profile);
}
And when I call the view as: {{ $data }}in editProfile.blade, it should return the authenticate user data from database and also I need the row form 'profiles' table where my user_id corresponds to id form 'users' table.
The problem is that my view returns an empty string, not what I expect form the database.
Why is users and profiles two separate tables?
I solve it.
This is how I do it:
In ProfileController I made the following function:
public function editProfileForm() {
$userData = DB::table('users')
->leftJoin('profiles', 'profiles.user_id','users.id')
->where('users.id',Auth::user()->id)
->get();
return view('profile.editProfile')->with('userData', $userData);
}
And that's how I got my info from db in $userData var. And I can display on inputs my data like {{$userData->city}} as an example.
Thanks to all !
Related
I have 2 tables. (1) being users, and (2) being foods.
In the [users] table, there is a food_id bracket that is linked as a foreign key to an item/column's id in the other table [foods]. I want to be able to go fetch data in that other column, via my user_id link in my [users] table.
I don't know how I am supposed to do this, despite reading the documentation, I am very beginner at this and it is part of my assignment. I've tried a few possible solutions, only to fail.
I have a controller, in which I refer the function into a view, that will display the food item/column related to the current logged user, with an if statement verifying if a certain data is present in order to display the HTML block object.
public function browseReserved()
{
//
$user = User::find(auth()->user());
$foodId = $user->where('food_id')->id;
}
So, how do I go fetch data from an item located in another table, from a data key that is linked to the id of said item?
add relationship in user model
public function food(){
return $this->belongsTo(Food::class);
}
then in controller
public function browseReserved()
{
$user = User::with('food')->find(auth()->id());
dd($user->food);
}
same like #john lobo answer but u can add foreign key on user model
public function food(){
return $this->belongsTo(Food::class,'food_id');
}
I am new to Laravel so please be patient with me.
i have to implement search functionality in Laravel. i am able to send form data to the search controller and receiving it as well
public function search_results(Request $request){
if ($request->has('gender')) {
echo $request->gender;
}
.....
}
Now when i am following tutorials on the web i am getting such examples :
public function filter(Request $request, User $user)
{
// Search for a user based on their name.
if ($request->has('name')) {
$user->where('name', $request->input('name'))->get();
}
}
or
public function index()
{
$users = User::where('status', 1)
->where('is_banned', 0)
->get(['name']);
dd($users);
}
now i am not sure from where this User:: or User $user is being added, and what should i do in my scenario.
Your suggestions would be helpful ..
Thanks
The $user represents an object belonging to the model(User::) associated with a table(users) in the mvc structure. If you want to "Search for a user based on their name." That means you already have a users table in database , and a User model .
You can use eloquent queries to retrieve data from database.
A User.php file should be generated when you create a new laravel project with composer. You can check for that.
So when you declare a variable like this :
$user = User::first();
You are actually getting the first element in 'users' table by using your User model.
If you can configure your User model and users table correctly , you should be able to get all records from your users table like this :
$users = User::all();
Then you can filter it like :
$users = $users->where('gender','male') // where('gender',$request->gender) in your situation
If you dont have a users table or a model , you can look to the document about how to create models using artisan commands Laravel API Doc. Generating Model Classes
I have create a form that will show the user the event title, event description. And the code
public function show($id){
$showevents = Events::findOrFail($id);
return view('events.show',compact('showevents'));
}
This data i pass dont have the user data, but it will give me the specific event data. My question is how to pass the user data along with this?
Because my event table dont have the user information, it all in the user table.
I try {{$showevents->user->name}} in the view form, but it doesnt give me the information of the user.
You will be needing the user-id too if you want to retrieve infomation about the user and send it to the view file..
One thing you can do for getting the user-id anywhere is setting the user-id in Session variable when the user logs-in as:
\Session::set('user_id',$userID);
Then you can get the user-id anywhere in any controller using
$id = \Session::get('user_id');
For example in your case
public function show($id){
$user_id = \Session::get('user_id');
$user = User::findOrFail($user_id);
$showevents = Events::findOrFail($id);
return view('events.show',compact('showevents','user));
}
Now where you will set the Session variable depends entirely upon your code..I used to set after a user has logged in successfully and also rembember to remove session data when logging out as..
\Session::flush();
First of all {{$showevents->user->name}} of course will fail because you don't have any connection between events and user.
If you want to make connection between it you going to need user_id inside event table.
If you want to get user data but don't have any connection, you need to do in seperate query.
$user = User::findOrFail($user_id);
return view('events.show',compact('showevents', 'user'));
You should use Eloquent's relationship functions or wrap them inside you Models relationship functions and use them.
check the Laravel documentation
I have 3 models: User, Campagin, CampaginType
User has many Campagin
Campagin belongs to CampaginType
(User does not related with CampaginType)
When I save a campaign (contains user_id and campaign_type_id field), how can I save 2 fields with a single command.
I try like:
$user->campaigns()->campaignType($campaignType)->create($campaignInfo);
But It not work! :(
public function store(Request $request)
{
Campaign::create($request->all());
$campaign = Campaign::where('unique_field', '=', $request->uniquefield)->first();
$campaign->users()->attach($request->user_id);
$campaign->campaign_type()->attach($request->campaign_type_id);
return redirect('your_destination_view');
}
Note
uniquefield is something that is unique to your campaign database table. You should also modify the models by adding the respective methods to make the eloquent relationship.
I am using Laravel, and I have a projects table and a users table. Each project can have multiple users. I have set up a project_user pivot table.
I am trying to create a multiselect box that lists all users, and has the users currently assigned to the project selected. Here is my ProjectsController function that creates the view.
public function edit($id)
{
$project = Project::findOrFail($id);
$users = User::lists('name','id');
return View::make('projects.edit', compact('project','users'));
}
And here is the relevant blade form:
{{ Form::select('user_id[]', $users, ??? , array('multiple')) }}
Where I think ??? should be an array of user_ids associated with the project. Can I get that from
$project->user...something here?
Or, do I need to create the array in function edit()?
You are almost there. First make sure you have the correct relationship defined in your project model
public function users(){
return $this->belongsToMany('User');
}
Now to get the ids of all assigned users:
$assignedUserIds = $project->users()->lists('id');
And just pass that to the view...
Accepted answer is right except on Laravel 5.2 I had to do ->toArray():
$assignedUserIds = $project->users()->lists('id')->toArray();
to get this to work with Form::select.
I would have commented original answer, but I don't have enough reputation.
$assignedUserIds = $project->users()->lists('id');
to
// I used in laravel 5.7
$assignedUserIds = $project->users()->pluck('id');