Welcome ! I have a problem . I try to connect via relations two models. If i create note it belongs to only one user. Problem is that in form options don't know why it doesn't display me users from database.
NotesController :
public function create()
{
$data['pilots'] = Pilot::all();
return view('uwagi.create',$data);
}
Create blade:
<label>Pilot</label>
<select class="form-control" id="pilot" name="pilots[]">
#foreach($pilots as $pilot)
<option value="{!! $pilot->id !!}"
{!! $pilot->name !!}
</option>
#endforeach
</select>
Regards and thank u for help
You are sending variable named $data but your foreach loop doesnot contain data variable, why?
public function create()
{
$pilots = Pilot::all();
return view('uwagi.create',$pilots);
}
Hope it's helps you.
pls modify your controller like this
public function create()
{
$pilots = Pilot::all();
return view('uwagi.create', compact('pilots'));
}
Related
I have a Livewire component like below
<div class="mr-3">
<input type="checkbox" name="milestoneMark" value="{{ $milestoneId }}" x-model="milestone" wire:click="mark()">
</div>
The component class have below code
class CheckMilestone extends Component
{
public $milestoneId;
public function mark()
{
$milestone = Milestone::where('id', $this->milestoneId)
->first();
$user = User::where('id', auth()->id())
->first();
$user->milestones()->attach($milestone);
}
public function unMark()
{
$milestone = Milestone::where('id', $this->milestoneId)
->first();
$user = User::where('id', auth()->id())
->first();
$user->milestones()->detach($milestone);
}
public function render()
{
return view('livewire.check-milestone');
}
}
My requirement is execute mark() method when the checkbox is unchecked and execute unMark() when the checkbox is checked
I tried below but didn't work
<div class="mr-3">
<input type="checkbox" name="milestoneMark" value="{{ $milestoneId }}" x-model="milestone" {!! 'checked' ? wire:click="mark()" : wire:click="unMark()" !!} >
</div>
I try to use as less JavaScript as possible so if you can please give me a solution without JS, but if there isn't any other way to fix this I'm okay to use JS. TIA!
You have nothing to base your checked situation from. I personally would do the following:
class CheckMilestone extends Component
{
public $milestoneId;
public bool $checked = false;
public function processMark()
{
if ($this->checked) {
$this->mark();
} else {
$this->unMark();
}
}
// Rest of your code here
}
<div class="mr-3">
<input type="checkbox" wire:model="checked" wire:change="processMark()" wire:loading.attr="disabled">
</div>
To explain; we use wire:model to link the value of the checkbox to the Livewire component. We use wire:change to detect the change event triggered when you (de)select the checkbox, and fire the processMark method. Purposely we don't change the HTML as to not cause unexpected behaviour. We use wire:loading to add the disabled attribute while the checked variable is being updated, so we can't quickly uncheck it while it's processing.
i am trying to create single row dynamic drop-down. give me reference or give me example.
my table
I NEED THIS ANSWER IN DROP DOWN
if I understand your question then I think this can ve the correct answer
public function data(){
$datas = Model::all();
return view('your view name',compact('datas'));
}
in your blade
<form action="{{url('YourUrlforFormSubmit')}}">
<select name="product">
foreach($datas as $data)
<option value="{{$data->id}}">{{$data->name}}</option>
</select>
</form>
in web.php
Route::post('YourUrlforFormSubmit','YourController#saveData')
in controller
public function saveData(Request $request){
$data = Model::find($request->id);
$save = new YournewModel; //whereyou want to save the data
$save->part_id = $data->part_id;
$save->name= $data->name;
$save->price= $data->price;
$save->save();
return view('your view name',compact('datas'));
}
I have a question regarding showing data from my database in Laravel.
I get the following error:
Trying to get property 'first_name' of non-object
It refers to this line of code:
#foreach ($contact as $c)
<h1 class="display-4">Bekijk details voor contact: {{ $c->first_name }} {{ $c->last_name }}</h1>
#endforeach
I get this data from my database by using Laravel's 'show' function as described below:
public function show($id)
{
$contact = Contact::find($id);
return view('contacts.show', compact('contact'));
}
My routing looks like this:
Route::resource('contacts', 'ContactController');
The reason I can't get my head wrapped around this error is because it seems to work just fine for other functions like Laravel's 'edit' function as described below:
public function edit($id)
{
$contact = Contact::find($id);
return view('contacts.edit', compact('contact'));
}
Any help would be appreciated, I would like to know why it is not working for my 'show' function whilst it is working for my 'edit' function, are there any differences I am not aware of?
Thanks in advance!
Kind regards,
Geert-Jan Knapen
Very likely, $contact is the object rather than collection, so you do not need to loop through it. you can access it directly.
<h1 class="display-4">Bekijk details voor contact: {{ $contact ->first_name }} {{ $contact ->last_name }}</h1>
Update it's better to use route model binding, so it handles if the contact does not exist.
public function show(Contact $contact)
{
return view('contacts.show', compact('contact'));
}
How to pass id from veiw into controller in then into another view again??
First of all this a basic question you should be able to search it anywhere on the internet. any how below is the solution.
web/routes.php:
Route::get('user/{id}','USerController#find')->name('user.get');
Or by passing user object in the route:
Route::get('user/{user}','USerController#find')->name('user.get');
UserController:
the below function accepts user object as a parameter in the route we defined above.
public function find(user $user)
{
return view('user.detail',compact('user'))
}
or
public function find($id)
{
$user = USer::find($id);
return view('user.detail',compact('user'))
}
resources/view/user/detail.blade.php:
{{ $user -> name }}
{{ $user -> email }}
and in order visit the route user.find use the below line:
View Detail
No you can use this code as reference to your project.
first solution is .
change route
Route::get('/show_vedio/{$id}', 'VedioController#show')->name(show_videos);
and use this {{ route('show_vedios', $vedio->id )}} in href
it's not entirely clear what you are trying to do, maybe i will help you:
public function show(int $id): View
{
$video = Video::find($id);
return view('video.show', ['video' => $video]);
}
I'm trying to pass a variable from foreach to my view. So I can access this using in my select form. Because I have two tables M:M relationship between departments and user. I need to get all the department_name where the user_id belong. For me able to send a data via department_name Here what I did please take a look.
DB Diagram:
department_user
As you can see here user_id is the id of the user and document_id is where the users belong.
Model:
Department:
public function users()
{
return $this->belongsToMany('\App\Models\User', 'department_user');
}
User:
public function departments()
{
return $this->belongsToMany('App\Models\Department', 'department_user');
}
Controller:
public function getDocuments()
{
$departmentRecipient = DB::table('departments')->get();
foreach ($departmentRecipient as $department)
{
$department->users = DB::table('department_user')
->where('department_id', '=', $department->id)
->pluck('user_id');
}
return view('document.create')->with('department', $department);
}
I'm getting all the users_id when I die and dump my variable departmentRecipient.
View:
<div class = "form-group">
<label for = "recipient" class = "control-label">Recipient:</label>
<select name = "recipient[]" multiple class = "form-control select2-multi" id = "myUserList">
#foreach ($department as $list)
<option value = "{{ $list->user_id }}">{{ $list->department_name }}</option>
#endforeach
</select>
</div>
I wanted to foreach the $department in my Controller to my select form. But it always says.
Trying to get property of non-object (View: C:\Users\JohnFrancis\LaravelFrancis\resources\views\document\create.blade.php)
Goal:
Use the following loop to iterate through the department users and add them to pivot table.
foreach($request->department as $departmentId)
{
foreach(Department::find($departmentId->id)->users()->get() as $user1) //find the users belonging to the current department
{
$document->sentToUsers()->sync([ $user1->id => ['sender_id' => $user->id]],false );
}
}
Also remove the following code form your getDocuments() as it is redundant:
foreach ($departmentRecipient as $department)
{
$department->users = DB::table('department_user')
->where('department_id', '=', $department->id)
->pluck('user_id');
}
I don't see user_id property in your dumped value of $departmentRecipient object, that is why you are getting the error you mentioned. However, there is a users array inside of $departmentRecipient object, which you made inside your foreach loop. You are plucking every user_id which are in individual department and setting in a property named users of $departmentRecipient object, and so you are getting an array inside users property. Here I have a solution for you,
public function getDocuments()
{
$departmentRecipient = DB::table('departments')->get();
$departmentUsers = array();
foreach ($departmentRecipient as $department)
{
$users = DB::table('department_user')
->where('department_id', '=', $department->id)
->pluck('user_id');
foreach ($users as $userId) {
$departmentUsers[$userId] = $department->department_name;
}
}
return view('document.create')->with('department', $department)->with('departmentUsers', $departmentUsers);
}
and inside of your view loop through the variable $departmentUsers, like this,
#foreach ($departmentUsers as $userId => $departmentName)
<option value = "{{ $userId }}">{{ $departmentName }}</option>
#endforeach
This will work but as your department contains multiple users so you will get individual department name multiple time in your select2 dropdown. If you share more of what is your goal by select2 then may be I can help you to solve your problem in other way.
Moreover if you are interested to use of Eloquent then you can get rid of lots of foreach looping.
In your case you can have multiple users against each department. So to make it work correctly with your forearch code. You need to make sure you are getting one user record against each depart. So modify following line of code in controller.
$department->users = DB::table('department_user')->where('department_id', '=', $department->id)->pluck('user_id');
But you want to display all users of department then you have to change foreach loop code into view.
Try This Code
App/Department
public function users()
{
return $this->belongsToMany('App\Entities\User', 'department_user', 'user_id', 'department_id');
}
App/User
public function departments()
{
return $this->belongsToMany('App\Models\Department', 'department_user');
}
Controller
use App/User;
public function getDocuments($userId,User $User)
{
$getSpecificUser = $User->with('departments')->find($userid);
return view('document.create')->compact('getSpecificUser');
}
View
#foreach ($getSpecificUser as $getUser)
#if(empty($getUser->departments) === false)
#foreach ($getUser->departments as $departments)
<option value = "{{ $getUser->id }}">{{ $departments->department_name }}</option>
#endforeach
#endif
#endforeach