I have a project which I am working on which requires full text searching on multiple tables in a database. I found a useful tutorial here:
http://creative-punch.net/2013/12/implementing-laravel-4-full-text-search
I have got it to work on one table, but now wish to extend to more than one. The issue I am having is that; if duplicate what is there for the remainder of the tables, when I hit submit, how do I get the button to check more than one controller ? or modify existing controller to check multiple tables ?
code
default view form
<div class="search">
{{ Form::model(null, array('route' => array('search'))) }}
{{ Form::text('query', null, array( 'placeholder' => 'Search query...' )) }}
{{ Form::submit('Search') }}
{{ Form::close() }}
</div>
returned view
<div class="search">
{{ Form::model(null, array('route' => array('search'))) }}
{{ Form::text('query', null, array( 'placeholder' => 'Search query...' )) }}
{{ Form::submit('Search') }}
#foreach($posts as $post)
<li>{{ $post->instance_id }}</li>
<li>{{ $post->instance_type }}</li>
<li>{{ $post->availability_zone }}</li>
<li>{{ $post->status_checks }}</li>
<li>{{ $post->alarm_status }}</li>
<li>{{ $post->public_dns }}</li>
<li>{{ $post->key_name }}</li>
#endforeach
{{ Form::close() }}
</div>
controller class
<?php
class PostsController extends BaseController {
public function postSearch(){
$q = Input::get('query');
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();
View::share('posts', $posts);
return View::make('ec2_instance', compact('posts'));
}
}
?>
my initial thought is to modify the controller such that you add :
$ip_address = public_ip::whereRaw("MATCH(ip_address) AGAINST(? IN BOOLEAN MODE)",
array($q))->get();'
then in the view add
#foreach($ip_address as $ip)
<li>{{ $ip->ip_address}}</li>
#endforeach
however variable ip_address could not be found
I was returning 2 views which is not possible do as per my initial thoughts ensuring you only return 1 view ie
$posts = ec2_instance::whereRaw("MATCH(instance_id,instance_type,availability_zone, status_checks,alarm_status, public_dns, key_name ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
$ip_address =
public_ip::whereRaw("MATCH(ip_address ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
$s3_bucket =
s3_bucket::whereRaw("MATCH(name, storage_class, size ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
$cloudfront_distribution = cloudfront_distribution::whereRaw("MATCH(delivery_method, cloudfront_id, domain_name,comment, origin, c_name, status, state ) AGAINST(? IN BOOLEAN MODE)", array($q))->get();
return View::make('ec2_instance', compact('ip_address', 'posts','s3_bucket', 'cloudfront_distribution'));
Related
I have file "header.html" in partial folder, here my code
<ul class="sf-menu" id="mainMenu">
{{ shoesmart:showMenu group="header" indent="tab"}}
<li>{{ name }}
{{ if children }}
<ul>
{{ children }}
<li>{{ name }}</li>
{{ /children }}
</ul>
{{ endif }}
</li>
{{/shoesmart:showMenu}}
</ul>
and I have file "shoesmart.php" in plugins folder, here my code :
function showMenu()
{
$return = '';
$menu_list = $this->db->select('*,CONCAT(pua.type,"/",pua.keyword) as path',false)
->from('menu mn')
->join('product_url_alias pua','mn.url_alias_id=pua.url_alias_id','LEFT')
->where('status',1)
->get()
->result_array();
foreach ($menu_list as $result) {
$return[] = array(
'name' => $result['name'],
'url' => BASE_URL.'home/'.$result['path']
);
}
return $return;
}
and my table name in database is "default_menu" that have structure:
menu_id url_alias_id name status parent_id
1 868 Men 1 0
2 869 woman 1 0
but when I refresh in browser, just show main menu, its mean that the submenu didn't show. Should I change my code ? or I also add new table for submenu in my database ?So please help me .. I have deadline project tomorrow T.T
I have a edit form with some checkboxes that I am trying to make checked when the associated many to many relationship is established.
Distributors belong to many beers
Beers belong to many distributors
in my controller I have:
$breweries = Brewery::lists('name', 'id');
$all_dist = Distributor::all();
$beer = Beer::find($id);
$distributions = [];
foreach ($beer->distributors as $distributor)
{
$distributions[$distributor->id] = BeerDistribution::where('beer_id', '=', $beer->id)
->where('distributor_id', '=', $distributor->id)->first()->price;
}
return View::make('beers.edit', ['beer' => $beer, 'distributors' => $all_dist, 'distributions' => $distributions, 'breweries' => $breweries, 'styles' => $styles]);
and I in my edit form I have:
{{ Form::model($beer, ['route' => ['beers.update', $beer->id], 'method' => 'PATCH']) }}
#foreach ($distributors as $distributor)
<?php $carried = in_array($distributor->id, array_keys($distributions)) ? true : false ?>
{{ Form::checkbox('distributors[]', $distributor->id, $carried); }}
{{ Form::label($distributor->name) }}
{{ Form::label('price' . $distributor->id, 'Retail:') }}
<?php $price = $carried ? $distributions[$distributor->id] : null ?>
{{ Form::text('price' . $distributor->id, $price ) }}
#endforeach
{{ Form::submit('Save') }}
{{ Form::close() }}
Basically I am passing an associated array of each distributor_id => price. This array also tells me which distributors the beer already belongs to so that I can mark those checked off in my edit form.
Here's where things get wonky. When I load this form, all the checkboxes will be checked no matter what. If I change my controller loop to this:
foreach ($beer->distributors()->lists('distributor_id') as $distributor_id)
Then I can do create my array.
Why does calling $beer->distributors in the controller would result in all the checkboxes being checked?
The problem is with the last argument : $carried
{{ Form::checkbox('distributors[]', $distributor->id, [ "checked" => $carried ]); }}
Pass the last parameter as array, and tell it exactly which attribute to modify and the attribute value.
I have a problem and I can't resolve it, please help me. So I have my form :
{{ Form::open(array('url'=>'/administration/student/addMarks','method' => 'post')) }}
#foreach($aObjectsInGroupe as $object)
{{ Form::hidden('id_object[]',$object->id) }}
{{ Form::label($object->name) }}
{{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}
<br />
#endforeach
{{ Form::hidden('id',$aOneStudent['id']) }}
{{ Form::submit('Add mark',array('class'=>'btn btn-primary')) }}
{{ Form::close() }}
In my StudentController I have a method for get the mark from student_id and object_id:
public function getMarkByStudentAndObject($nIdStudent, $nIdObject){
$aMark = \Mark::where('student_id', '=', $nIdStudent)
->and('object_id', $nIdObject)
->get()
->toArray();
}
$aMarsks it's a table :
$aMarks = array(
'0'=>'0',
'1'=>'1',
'2'=>'2',
'3'=>'3',
'4'=>'4',
'5'=>'5',
'6'=>'6',
'7'=>'7',
'8'=>'8',
'9'=>'9',
'10'=>'10',
);
It's possible to call the method getMarkByStudentAndObject in :
{{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}
to get the selected value?
Help me please. Thx in advance.
maybe you need to do this:
<select name="note[]" class="form-control">
#foreach($aMarks as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
</select>
You should also check your Eloquent query, instead of ->and use another ->where
Try the lists() function of the Query Builder
\Mark::where('student_id', '=', $nIdStudent)
->and('object_id', $nIdObject)
->lists('column_1', 'column_2');
You then get the values of column_1 as the keys of the array and column_2 as the values.
http://laravel.com/docs/4.2/queries#selects
You should be able to directly call this from the Form::select. A call to
Mark::getMarkByStudentAndObject($aOneStudent['id'], $object->id)->note
would give you the Mark object and then you would need to get the note column which stores the value in the Mark object. Resulting in a call like this:
{{
Form::select(
'note[]',
$aMarks,
Mark::getMarkByStudentAndObject(
$aOneStudent['id'],
$object->id
)->note, array('class'=>'form-control')
)
}}
At the moment I can't verify if this is working because I have no possibility to test it but it should work as Blade is just a wrapper for PHP calls.
Should it work in Silex with symfony/twig-bridge ?
{{ render(controller('MyController')) }}
Now I have message like this:
Twig_Error_Syntax: The function "controller" does not exist in "...
You can use it this way:
{{ render(path('your_route_id', {'id': id, 'anotherParam': param})) }}
I've found this working:
{{ render(controller('services.controller:action', {[params]}) }}
And you can define the controller as a service:
$app['services.controller'] = function() use ($dependecy1, .., $dependencyN){
return new \\PathToYourControllerClass($dependecy1, .., $dependencyN);
}
I've found this working :
{{ render(controller('Full\\Namespace\\To\\Your\\Controller::listAction')) }}
please don't forget a double slash '\\'
Example:
{{ render(controller('Acme\\ProductController::listAction')) }}
In Your ProductController (I'm using Doctrine 2 in this example) :
public function listAction(Application $application)
{
$em = $application['orm.em'];
$produits = $em->getRepository('Acme\Entity\Produit')->findAll();
return $application['twig']->render('list.html.twig', array(
'products' => $products
));
}
Then in your list.html.twig
{% for product in products %}
<h2> {{ product.name }} </h2>
{% endfor %}
I have a simple blog with Post resource and Comment nested resource.
Until now I can see all the comments belonging to a post and create a new comment for a post.
I want to give the possibility to delete a specific comment, but somehow I am making some mistakes.
This is the view comments.index with all the comments:
#extends('master')
#section('blog')
#foreach($comments as $comment)
<div class="span11 well">
<ul>
<li><strong>Body: </strong> {{ $comment->body }} </li>
<li><strong>Author: </strong> {{ $comment->author }}</li>
</ul>
{{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $post_id), $comment->id)) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}
</div>
#endforeach
{{ link_to_route('posts.index', 'Back to Post index') }}
This is the error i get running the index: Parameter "comments" for route "posts.comments.destroy" must match "[^/]++" ("" given) to generate a corresponding URL.
This is the Index method inside CommentsController:
public function index($post_id)
{
$comments = Post::find($post_id)->comments;
return View::make('comments.index', compact('comments'))->with('post_id', $post_id);
}
And this is the Destroy method inside CommentsController:
public function destroy($post_id, $comment_id)
{
$comment = $this->comment->find($comment_id)->delete();
return Redirect::route('posts.comments.index', $post_id);
}
Someone can tell me please where I am making the mistake?
This the routes:
Route::resource('posts', 'PostsController');
Route::resource('posts.comments', 'CommentsController');
You have put a regexp tester on your route, to check your comments parameter.
This error message says that parameter that you give to Laravel isn't good.
If your parameter is only a decimal id, use \d+ regexp instead.
Without your routes.php file - I cant be sure, but I think this might be the problem.
Change
{{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', $post_id), $comment->id)) }
to
{{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', array ($post_id, $comment->id))) }
If this does not work - please post your routes.php file.
Edit: You have defined your route as a "resource". This means your destroy route is defined with only one variable. You dont actually need the $post included, so just define this:
{{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $comment->id))) }}
and change your destroy method to this - there is no need for the $post to delete a $comment:
public function destroy($comment_id)
{
$comment = $this->comment->find($comment_id)->delete();
return Redirect::back();
}