I cannot fetch the value of the data after I store or created it .. I want to fetch the data in edit.blade.php after I created it. I used the old function method but it didnt work
My Controller
public function edit($id)
{
$movieSubs = MovieSubtitle::find($id);
$movieTitle = Movie::all()->pluck('title','id');
return view('admin.subtitles.edit', compact('movieSubs','movieTitle'));
}
My edit.blade.php - here is where I want to fetch the data that I use old
{{ Form::select('movieTitle', $movieTitle, old('movie_id', $movieTitle) ,['class' => 'form-control'])}}<br>
Do you have an input with the name movie_id? In my understanding, the dropdown movieTitle already contains the ID of the movie so this might work better.
{{ Form::select('movieTitle', $movieTitle, old('movieTitle'), ['class' => 'form-control'])}}
Related
On my index.blade i like to show two different sets of data, one is a list of computers the other one list of categories. two different type of data. in my controller I have
public function index()
{
// Select data from table assets
$assets = DB::table('assets')->where('status', '1')->orderBy('type', 'asc')->paginate(10); //pass all the employees to a Var and send it to our page using "with" also puts page # at the bottom
//select data from table assetrefs
$categories = DB::table('assetrefs')->get();
//bind data and send using 'with'
return view('assets.index')->with('assets', $assets)->with('categories', $categories);
}
on my view side I have these two loops
#if(count($categories) > 1)
#foreach($categories as $row)
{!!Form::open(['action' => ['AssetsController#showType', $row->title], 'method'=> 'POST', 'class'=> 'pull-left'])!!}
{{Form::submit('Show '. $row->title, ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
#endforeach
#else
<p> no content </p>
#endif
This shows a bunch of button with categories
<div class="well"></div>
<!-- lists all the available assets in a div all types are mixed but sorted -->
#if(count($assets) > 1)
#foreach($assets as $asset)
<div class="well">
<h3>{{$asset->type}}, {{$asset->make}} - {{$asset->model}} </h3>
<small> {{$asset->sn}}</small>
<small> View this account</small>
</div>
#endforeach
{{$assets->links()}}
#else
<p> no content </p>
#endif
this shows a list of all computers
When I load the page I get
2/2 ErrorException
Undefined variable: categories (View: resources\views\assets\index.blade.php)
Can you please pass data to view by making use of array instead of with like below:
public function index()
{
// passing data without with
$data['assets'] = DB::table('assets')->where('status', '1')->orderBy('type', 'asc')->paginate(10);
$data['categories'] = DB::table('assetrefs')->get();
return view('assets.index',$data);
// in case if you want to use with then
// return view('assets.index')->with($data);
}
Or even if you don't want to use $data variable want to keep $asset and $categories variable as it is then
public function index()
{
// passing data without with
$assets = DB::table('assets')->where('status', '1')->orderBy('type', 'asc')->paginate(10);
$categories = DB::table('assetrefs')->get();
return view('assets.index',compact('assets','categories'));
}
You can check how compact function is working in php by url -> http://php.net/manual/en/function.compact.php
Try this in your Controller in place of your view() call:
return view('assets.index', compact('assets', 'categories'));
Have a look at Passing Data To Views and you'll see view()->with() is suited for passing individual pieces of data whereas passing an array as a second argument is required for multiple variables. Here we're passing an array of your already defined assets and categories via PHP's compact function.
I have a form and I want to give user ability to choose user in a drop down, but when I return the data it gives me an object, How can I make a drop down select for each user in array.
This is my code
view
{!! Form::select('users', array($users),null, ['placeholder' => 'Pick a user']) !!}
controller
$users = User::lists('name');
return view('view')->with('users', $users);
now it returns
Placeholder
["user1", "user2"]
You need to add ID to the list to make it work:
$users = User::pluck('name', 'id');
Also, use pluck() instead of lists() because lists() is depricated.
I am working in a Laravel 5 application. I try to save a comment for a projet and I don't know how to get the $comment->project_id value.
Here is my simplified controller
public function store( CommentsFormRequest $request )
{
$comment = new Note;
$comment->message = Input::get('message');
$comment->project_id = $note->project->id;
$comment->user_id = Auth::id();
$comment->save();
return Redirect::back();
}
and here is my simplified form
{!! Form::open(array('route' => 'notes.store')) !!}
{!! Form::textarea('message', '', array('placeholder'=>'Message')) !!}
{!! Form::submit('Ajouter la note') !!}
{!! Form::close() !!}
When I try to save, I get this error:
Trying to get property of non-object
I guess it's because it tries to get the sollicitation_id of the new object wich is null. How should I get the current project_id value?
Update
Conclusion: I used an hidden field and followed #tommy 's recommendation.
My controller now uses
$note->project_id = $request->input('project_id');
and my hidden field is
{!! Form::hidden('project_id', $project->id ) !!}
Only IF the table primary column name is 'id':
$model->id;
Regardless of primary column name:
$model->getKey();
In the store method, you try to get the property project of the variable $note, which does not exist. You should pass the project ID to the store method by either adding it to the route or adding a hidden field project_id to your form.
Then, your store method should look something like this:
public function store($project_id, CommentsFormRequest $request )
{
$project = Project::find($project_id); // $project_id is transmitted over the URL
$comment = new Note; // I'd alias Note as 'Comment', or rename '$comment' to '$note' because this can be confusing in the future
$comment->project_id = $project->id;
$comment->save();
return Redirect::back();
}
If you want to add a hidden field with the project ID to the form, you can access its value by calling $request->input('project_id');
I feel the above answer is far from perfect as you're not only exposing Unique ID's to users but it's also long winded, and would fail if two users were to load the same page at the same time, instead you should do
public function store(CommentsFormRequest $request )
{
$comment = new Note([
// your fields here
]};
$comment->save();
//$comment now contains a unique ID!
return redirect($comment->id);
}
You can get last id by using insertGetId() Query Builder method
If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:
$id = DB::table('users')->insertGetId(
['email' => 'john#example.com', 'votes' => 0]
);
for more info check document
I have a Laravel PHP Photo Gallery application that allows the user to create an album and then insert photos into an album of their choice from a drop down list. This requires the user to create the album first since the drop down list is dynamic based on which albums actually exist. The drop down list is currently functioning properly but it does not display albums (array elements) alphabetically.
I've tried using the 'sort()' method but the problem is that the array element ids then become switched/re-sorted when doing so. I do not want the array ids to be re-sorted since this will put photos into the wrong albums.
So I want to know if there is a way to display the albums alphabetically in a drop down list while not re-sorting their array element ids.
Controller:
public function create()
{
$stoneArray = $this->stone->all()->toArray();
if (empty($stoneArray)) {
$dropdown[0] = 'There are no stones';
}
foreach ($stoneArray as $stone) {
$dropdown[$stone['stone_id']] = $stone['stone_name'];
// sort($dropdown); /* This re-sorted the array element ids */
}
$data = array('type' => 'stone_photo', 'dropdown' => $dropdown);
$this->layout->content = \View::make('forms.stones.new-photo', $data);
}
View:
<div class="form-group">
{{ Form::label('stone_id', 'Stone: ') }}
{{ Form::select('stone_id', $dropdown, array('class' => 'form-control')) }}
</div>
Any help is greatly appreciated! Thank you!
EDIT
I'm making some assumptions here about your file structure, but it should give you the idea.
In: app\Acme\repositories\Eloquent\EloquentStoneRepository.php or whatever you called it, there's a function:
public function all()
{
return Stone::all();
}
(I think that's what it looks like, but I'm not positive). Try changing that to:
public function all()
{
return Stone::orderBy('column_name', 'asc')->get();
}
If you use all() somewhere else and don't want it sorted, consider adding another function:
public function all_sorted()
{
return Stone::orderBy('column_name', 'asc')->get();
}
Hope that helps!
Edit:
Changed ->all() to ->get() so eloquent can properly sort it.
in MySql database i have systemSetting table and this have any data.
in this table i have this fields :
id - site_copyright - date
i want to fill form with this single row.
{{ Form::model($siteSettings) }}
{{ Form::label('site_copyright' , 'copy right') }}
{{ Form::text('site_copyright', null , array('id'=>'site_copyright' )) }}
{{ Form::label('site_copyright' , 'copy right') }}
{{ Form::text('site_copyright', null , array('id'=>'site_copyright' )) }}
{{ Form::close() }}
UPDATE:
in CONTROLLER of that i have this:
public function getIndex()
{
$siteSettings = SystemSetting::all();
return View::make('back_end.layouts.manageHeaders')->with('siteSettings', $siteSettings);
}
in Result, form could not parse data to input fields.
i get error for this :
$siteSettings->site_copyright
This is a duplicate of many questions, such as this: Laravel display table record using query
The short answer is: use first() method to get a single entry.
You're using form model binding, so change your controller to:
public function getIndex()
{
$siteSettings = new SystemSetting;
return View::make('back_end.layouts.manageHeaders', compact('siteSettings'));
}
Query work on 5.1 or more
return DB::table('users')->where('id', $id)->value('field_name');
//if lower version of laravel like 4.2
DB::table('users')->where('id', $id)->pluck('field_name');